0

I am using the Generic Repository and UoW Patterns in my MVC application. It works well for my requirements. However, I am finding it very difficult to integrate ASP Identity with these patterns due to tight coupling of ASP Identity with MVC. My code is as follows:

// Repository Interface
public interface IRepository<TEntity> where TEntity : class
{
        TEntity Get(int id);
        IEnumerable<TEntity> GetAll();
        IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
        void Add(TEntity entity);        
        void Remove(TEntity entity);
}

// Repository class
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
        // db context defined
        // IRepository implementation
}

// Domain Class
public class Author
{
        public int AuthorId { get; set; }
        public string Name { get; set; }
}

// DbContext class
public class AppContext : DbContext
{
        public AppContext() : base("name=MyContext") {}
        public virtual DbSet<Author> Authors { get; set; }
}

// Domain Interface
public interface IAuthorRepository : IRepository<Author> 
{  // implement custom methods }

// Concrete class implementation
public class AuthorRepository : Repository<Author>, IAuthorRepository
{
        public AuthorRepository(AppContext context) : base(context)
        {}

        public AppContext AppContext
        {
            get { return Context as AppContext; }
        }
}

// UnitOfWork interface
public interface IUnitOfWork : IDisposable
{
        IAuthorRepository Authors { get; }
        int Complete();
}

// Unit of work class
public class UnitOfWork : IUnitOfWork
{
    private AppContext context;

        public UnitOfWork(AppContext context)
        {
            this.context = context;
            Authors = new AuthorRepository(context);
        }

        public IAuthorRepository Authors { get; set; }

        public int Complete()
        {
            return context.SaveChanges();
        }

        // dispose context
}

public class HomeController : Controller
{
        private readonly IUnitOfWork uow;

        public HomeController(IUnitOfWork uow) // using Dependency Injection
        {
            this.uow = uow;
        }

        public ActionResult Index()
        {
            var authors = uow.Authors.GetAll();
            return View(authors);
        }
}

Can someone provide me pointers on how can I integrate ASP Identity with above structure.

Thanks in advance.

Tech
  • 33
  • 6
  • Possible duplicate of [ASP.NET Identity with Repository and Unit of Work](https://stackoverflow.com/questions/23226140/asp-net-identity-with-repository-and-unit-of-work) – Steve Greene Jun 26 '17 at 20:20

1 Answers1

1

Short answer - you don't do this.

Long answer: default template gives you a working solution with all the bits working together nicely. Why bother roping working solution into your architecture, especially that you realise your architecture does not fit the problem that's solved by Identity.

Repository and UoW patterns are good, but are not for Identity. Identity deals with authentication, cookies, roles, permissions. Repository is about persisting data, UoW about transaction. These things don't fit each other. So don't try to fit a round peg into a square hole.

trailmax
  • 34,305
  • 22
  • 140
  • 234
  • trailmax, then how do I integrate Identity with my application. In my structure, the DbContext is accessible only through the Repository but Identity on the other hand is tightly coupled with DbContext. The only solution is to move out the Identity code to its own Repository. – Tech Jun 27 '17 at 07:56
  • You are using DI, right? Well, inject `DbContext` into `IUserStore` - that is the only place where it is needed (mostly) and the rest of Identity is working through `ApplicationUserManager` and `ApplicationRoleManager` (if you have it). – trailmax Jun 27 '17 at 08:59