Reputation: 2414
I'm setting up a new ASP.NET Core project and in my MVC project, I wish to use Identity for user registration/login etc.
I could write my own, but I really want to make use of the authorization attributes in my controllers and actions.
However, I want to extract the Identity part to a separate project so my MVC project doesn't have a direct dependency to Entity/DBContexts.
When creating a new MVC project with Identity, it creates:
As well as having references to:
Is there any easy way for me to extract all Identity code to a separate project, where my Controllers just use the services from that project (injected in the constructor of the controllers)?
Upvotes: 3
Views: 4082
Reputation: 36736
The way to keep project references separated between layers so that the UI/MVC layer does not directly reference the Db/Storage layer is to make extension methods on IServiceCollection in the other layers of your project.
So the UI references the Business/Services layer and calls extension methods of IServiceCollection that live in the Business/Service layer, those methods in turn can call extension methods in the Db/Storage layer.
Similar question with example here
But when you move the EF code into a separate class library, there some known issues where running EF commands to generate migrations don't work. You can work around that by making the class library an application/console app with its own Startup class. My understanding is this will be fixed later.
Upvotes: 1