-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Simple Injector in ASP.NET 5 with ASP.NET Identity #93
Comments
There are a several possible paths to walk here, depending on to what extend you would like the SOLID principles guide you here. If you're just interested to get this working quickly, you can add the following configuration to vNext's IServiceProvider provider = app.ApplicationServices;
container.Options.AllowOverridingRegistrations = true;
container.Register<AccountController>(provider.GetRequiredService<AccountController>);
container.Options.AllowOverridingRegistrations = false; What this does is simply telling Simple Injector to delegate the construction of the This does mean however that you will have to register the So instead, you can let the IServiceProvider provider = app.ApplicationServices;
container.Register<IEmailSender, AuthMessageSender>();
container.Register<ISmsSender, AuthMessageSender>();
container.Register(provider.GetRequiredService<UserManager<ApplicationUser>>);
container.Register(provider.GetRequiredService<SignInManager<ApplicationUser>>);
container.Register(provider.GetRequiredService<ApplicationDbContext>); The decision we made with the first option is to see the With the second option, we decided that the
So the third option is to actually refactor the I won't be able to post an example of how a SOLID |
I see what you are suggesting there and I agree with you on the fact that it is problematic that Identity Framework, by default, uses a strong dependency on the managers in the Let's ignore your first suggestion for a minute and assume I am going down the proper route. If I understood you correctly, it is essentially going to be based on the second solution, but with an additional set of wrappers for which I control the interfaces. This solves the issue about injecting the right dependencies inside I still need to inject a custom DAL inside my IServiceProvider provider = app.ApplicationServices;
container.Register<IEmailSender, AuthMessageSender>();
container.Register<ISmsSender, AuthMessageSender>();
container.Register(provider.GetRequiredService<UserManager<ApplicationUser>>);
container.Register(provider.GetRequiredService<SignInManager<ApplicationUser>>);
container.Register<IDataAccessLayer, FooDataAccessLayer>();
class FooUserStore : ...
{
FooUserStore(IDataAccessLayer dal)
{
...
}
} How should I register this custom store in such a situation? |
I'm sorry, I had to reread your question a couple of times and had to dive a bit into Identity Framework to understand what the problem is. This is where you start to see the cracks in the new vNext DI eco system. The problem is not so much with the DI implementation itself, but with all the assumptions around it, and especially how other tools will start building upon this new API (as Identity is already doing). This causes trouble for people who like to keep application code isolated from framework code. The AddScoped(typeof(IUserStore<FooIdentityUser>), typeof(FooUserStore)) I would view this services.AddScoped<IUserStore<FooIdentityUser>>(
_ => new FooUserStore(this.container.GetInstance<IDataAccessLayer>)); Note that I'd advice to change |
Wow, ok thanks a lot. This pretty much answers all of my questions. So the solution is pretty much to setup forwarding between the two containers to make the various services accessible. Regarding your SOLID Is there a sample library for SimpleInjector? Once I'm done setting up this project, I could publish this code as a sample that other people could follow. |
Yes. There will probably be just a few services where this forwarding is required, although we can expect this will happen more in the future, since every many framework developers will invalidly assume that their framework needs to integrate with the vNext container.
I will. Last days I was quite busy finalizing v3, but no that it is out the door, I'll try to create a SOLID |
I've been trying to implement a vnext website with SimpleInjector using the pattern recommended in issue #41. I really like the separation it brings between first-party and third-party services, but I'm hitting a wall when trying to add support for Asp.Net Identity v3 using a custom UserStore.
You see, Asp.Net Identity normally gets registered through the DNX DI system during service configuration.
This means that the entire Asp.Net Identity dependencies, including the custom FooUserStore and FooRoleStore, class will be registered on the the default DNX container. Already, this breaks the nice separation between the two containers, but the main issue comes from the AccountController class. Since this class needs access to Asp.Net Identity's UserManager and SignInManager, then it needs to access classes registered on the DNX container.
With the suggested integration method, controller activation is completely handled by SimpleInjector through the SimpleInjectorControllerActivator. There is no place to inject DNX registered services from there.
I thought about registering the Asp.Net Identity types in the DNX container and use SimpleInjector as a forwarder to the DNX's container.
The main problem with this solution is that the DNX container now have to know about my data access layer which means more forwarding; and in both direction this time.
To me, this feels like a road to hell. I'm sure there must be a simpler way to get this working. What do you recommend to do in this case?
The text was updated successfully, but these errors were encountered: