Registration with Async Instance Creator Factory method
See original GitHub issueIdeally I’d like a Register overload like the following:
Register<TService>(Func<Task<TService>> instanceCreator, Lifestyle lifestyle)
So I could write the following:
container.Register<SqlConnection>(async () =>
{
var cn = new SqlConnection("myConnString");
await cn.OpenAsync();
return cn;
},
Lifestyle.Scoped);
Would this feature make sense for Simple Injector?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Top Results From Across the Web
Autofac: Registering an Async Factory method
I'm using Autofac and need to invoke async factory methods which look like this: class AppModel { public static async Task<AppModel> CreateAsync ......
Read more >injectable - Dart API docs
injectable will automatically register it as an asynchronous factory because the return type is a Future.
Read more >Async Programming - Patterns for Asynchronous MVVM ...
The first time the AsyncLazy<T> instance is awaited, it will start the asynchronous factory method once on a thread pool thread.
Read more >How to Write an Async Class Constructor in TypeScript or ...
The init method is one in which you define an async instance method, performing all your async setup as an additional step, after...
Read more >Async calls in a factory? : r/dartlang
I have a class with a factory as constructor as I need to some initialization logic which from my understanding cannot be done...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yup that makes sense. Thanks for the explanation. I’d like to inject the same open connection into all my objects per request and have the container worry about managing the lifetime. So would you suggest my SqlConnectionProvider implement IDisposable and look something like below?
btw, you can also let Simple Injector manage the lifetime of the
SqlConnection
; this prevents you from having to dispose it. But to be honest, I’m not sure that would make the solution more elegant. But just for completeness, here’s how to do it: