How can I split the properties, functionality and classes out of the default ASP.Net Mvc / Identity 2.0? I am battling with a few things:
- by default, it wants to use OWIN's context to wire up some kind of dependency injection, and control the Managers
- it puts the ApplicationDbContext at the application level, where my architecture requires it be available at "lower" levels.
- It requires that I declare any properties in the same class as the functionality that acts on those properties (which doesn't fit in with my architecture)
- The ApplcationUser model has dependencies on Asp.Net, which I would like to break if I am to move the POCO to a non-MVC layer of the solution
Application architecture:
I have a solution that has several tiers :
- Api - defines interfaces for services
- Domain - stores POCO models representing a business domain
- Business - stores logic for interacting with domain objects, and consumes services
- Service - implementation of services, including Entity Framework, and the structure maps for the domain objects
- Application - in this case, an MVC application.
My business layer only knows about service interfaces, not implementation, and I am using dependency injection to wire everything up.
I have some interfaces defining read/write/unit of work operations for a data service, and an implementation of these that inherit from DbContext (in my Service layer). Instead of having a series of DbSet<MyPoco> MyPocos {get;set;}
, I am wiring it up by passing a series of type configurations that define relationships, then accessing my types through Set<Type>()
. All that works great.
This stack has been in place for an existing application, and works well. I know it will transition to an MVC application, and am only having issues with the "out of the box" ASP.Net Identity-2.
My solution to this was to: Abstract all the things
I got around this by abstracting most of the functionality of identity into its own project which allowed for easier unit testing and reuse of the abstraction in other projects.
I got the idea after reading this article
Persistence-Ignorant ASP.NET Identity with Patterns
I then fine tuned the idea to suit my needs. I basically just swapped out everything I needed from asp.net.identity for my custom interfaces which more or less mirrored the functionality provided by the framework but with the advantage of easier abstractions and not implementations.
IIdentityUser
IIdentityManager
IIdentityResult
In my default implementation of the identity manager, which also lives in its own project, I simply wrapped the
ApplicationManager
and then mapped results and functionality between my types and the asp.net.identity types.The application layer is only aware of the abstractions and the implementation is configured at startup. I don't have any
using Microsoft.AspNet.Identity
at the higher level as they are all using the local abstractions.the tiers can look like this :
In MVC Application layer the
AccountController
thus only neededThis assumes you are using some DI framework. It is only in the configuring of the IoC that any mention is made of the layer that implements identity completely abstracting it away from those that have need of using identity.