Entity framework 如何在MVC应用程序中将ASP标识与通用存储库和UoW模式结合使用

Entity framework 如何在MVC应用程序中将ASP标识与通用存储库和UoW模式结合使用,entity-framework,asp.net-identity,repository-pattern,asp.net-mvc-5,Entity Framework,Asp.net Identity,Repository Pattern,Asp.net Mvc 5,我正在MVC应用程序中使用通用存储库和UoW模式。它符合我的要求。然而,我发现很难将ASP标识与这些模式集成,因为ASP标识与MVC紧密耦合。我的代码如下: // Repository Interface public interface IRepository<TEntity> where TEntity : class { TEntity Get(int id); IEnumerable<TEntity> GetAll();

我正在MVC应用程序中使用通用存储库和UoW模式。它符合我的要求。然而,我发现很难将ASP标识与这些模式集成,因为ASP标识与MVC紧密耦合。我的代码如下:

// 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);
        }
}
//存储库接口
公共接口假定,其中tenty:类
{
TEntity-Get(int-id);
IEnumerable GetAll();
IEnumerable Find(表达式谓词);
无效添加(潜在实体);
无效删除(潜在实体);
}
//存储库类
公共类存储库:IRepository,其中tenty:class
{
//数据库上下文定义
//间接实施
}
//域类
公共类作者
{
公共int AuthorId{get;set;}
公共字符串名称{get;set;}
}
//DbContext类
公共类AppContext:DbContext
{
public AppContext():base(“name=MyContext”){}
公共虚拟数据库集作者{get;set;}
}
//域接口
公共接口IAuthorRepository:IRepository
{//实现自定义方法}
//具体的类实现
公共类AuthorRepository:Repository,IAuthorRepository
{
公共AuthorRepository(AppContext上下文):基础(上下文)
{}
公共AppContext AppContext
{
获取{返回上下文作为AppContext;}
}
}
//工作单元接口
公共接口IUnitOfWork:IDisposable
{
IAuthorRepository作者{get;}
int Complete();
}
//工人阶级单位
公共类UnitOfWork:IUnitOfWork
{
私人语境;
公共UnitOfWork(AppContext上下文)
{
this.context=上下文;
作者=新作者或报告(上下文);
}
公共IAuthorRepository作者{get;set;}
公共int Complete()
{
返回context.SaveChanges();
}
//处理上下文
}
公共类HomeController:控制器
{
私人只读IUnitOfWork uow;
公共HomeController(IUnitOfWork uow)//使用依赖项注入
{
this.uow=uow;
}
公共行动结果索引()
{
var authors=uow.authors.GetAll();
返回视图(作者);
}
}
有人能给我指点一下如何将ASP身份与上述结构集成


提前谢谢。

简短回答-您不需要这样做

详细回答:默认模板为您提供了一个工作解决方案,所有位都可以很好地协同工作。为什么要费心将工作解决方案捆绑到您的体系结构中,尤其是当您意识到您的体系结构不适合通过身份解决的问题时


存储库和UoW模式很好,但不适合标识。标识处理身份验证、cookie、角色和权限。存储库是关于持久化数据的,UoW是关于事务的。这些东西不相配。因此,不要试图将一个圆钉装入一个方孔。

trailmax的可能副本,那么如何将Identity与我的应用程序集成。在我的结构中,DbContext只能通过存储库访问,但另一方面,标识与DbContext紧密耦合。唯一的解决方案是将标识码移出它自己的存储库。您正在使用DI,对吗?好的,将
DbContext
注入
IUserStore
——这是唯一需要它的地方(主要是),其余的标识通过
ApplicationUserManager
ApplicationRoleManager
(如果您有)。