Skip to content
This repository was archived by the owner on Nov 2, 2018. It is now read-only.

Add support for binding to factory #64

Closed
victorhurdugaci opened this issue Apr 21, 2014 · 7 comments
Closed

Add support for binding to factory #64

victorhurdugaci opened this issue Apr 21, 2014 · 7 comments

Comments

@victorhurdugaci
Copy link
Contributor

Feature proposal:

Today you can do:

serviceCollection.AddInstance<T>( new SomeT() )

But it would be nice to have an overload for AddInstance that accepts a Func and late binds that instance. Something like:

serviceCollection.AddInstance<T>( () =>  new SomeT() )

The instance is resolved when it is requested for the first time.

A workaround is to bind bind to a factory object or Lazy but it would nicer to have this out of the box.

@Eilon
Copy link
Member

Eilon commented Apr 21, 2014

Instead of binding to an instance you can bind to a type. Then it'll get instantiated by DI along with injecting any dependencies.

Do you have a specific example of where you need a factory?

@victorhurdugaci
Copy link
Contributor Author

Yes, I have an example. See this pull request: https://github.com/aspnet/EndToEnd/pull/6

In Startup.cs

serviceCollection.AddInstance<UserManager<AppUser>>(IdentityFactory.CreateNewUserManager());

When I create the UserManager, I want to add a user to it (that's what CreateNewUserManager does):

UserManager<AppUser> userManager = new UserManager<AppUser>(new InMemoryUserStore<AppUser>());

await userManager.CreateAsync(
    new AppUser(DemoUserName),
    DemoUserPass);

return userManager;

If I would do something like:

serviceCollection.AddSingleton<UserManager<AppUserRole>, UserManager<AppUserRole>>()

there is no way of adding the user to the UserManager when the manager gets created.

@Eilon
Copy link
Member

Eilon commented Apr 21, 2014

So this is not only about having a custom factory pattern, but this also about having an async factory pattern? That is, does the factory need to be able to do async work, such as database calls?

@rustd rustd added this to the Alpha milestone Apr 28, 2014
@victorhurdugaci
Copy link
Contributor Author

Async would be nice but not necessary (at least initialy). If the function returns a task, the consumer could handle that. Maybe a GetServiceAsync later? :)

I was talking to Suhas and he has another scenario where this would be very beneficial. He has a class that requires a HttpContext instance as constructor argument. He wants to inject that class into a controller constructor. However, the container does not have the HttpContext and thus cannot resolve his class instance.

With support for Func we could resolve the current HttpContext when needed. Maybe @suhasj can post the actual code.

@davidfowl
Copy link
Member

That's not the way you do this particular scenario. Instead, you'd take an IContextAccessor in your class to get at the HttpContext

@suhasj
Copy link
Contributor

suhasj commented May 2, 2014

It worked ! I was able to inject HttpContext using the IContextAccessor property

@pranavkm
Copy link
Contributor

Duplicate of #104

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants