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.