ASP.NET身份与工作信息库和单位 [英] ASP.NET Identity with Repository and Unit of Work

查看:98
本文介绍了ASP.NET身份与工作信息库和单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习工作模式库和单位的ASP.NET MVC应用5与实体框架6。

I'm learning Repository and Unit of Work patterns in ASP.NET MVC 5 application with Entity Framework 6.

我已经阅读了大量的教程和文章,但几乎所有的人都condradictory。那些说库和工作模式机组都好,别人说的DbContext是已经工作的一个仓库和单位,别人说类似的东西,而是提供了一个完全不同的方法。我尝试了所有这些不同的方法(好吧,也许不是所有的人),并仍在挣扎关于哪种方法是最正确的。​​

I had already read a lot of tutorials and articles, but almost all of them are condradictory. Ones say that Repository and Unit of Work patterns are good, others say DbContext is already a repository and unit of work, others say something similar, but offer a completely different approach. I tried all these different approaches (well, maybe not all of them) and still struggling regarding which approach is the most correct one.

我目前拥有的是:


  • IRepository和GenericRepository实现IRepository

  • IUnitOfWork和的UnitOfWork实施IUnitOfWork

  • IDbContext和MyDbContext从IdentityDbContext继承和实施IDbContext

不知道如果我需要粘贴它的代码,我认为这是相当通用的问题实际上是不是与存储库/的UnitOfWork这样。我的问题是使用ASP.NET身份类,结合我的库和工作单位。
我共享同一个数据库籍及所有其他数据 - 我认为这是一个常见的​​场景。我找不到很好的解决方案,我怎么可以实例使用我的资料库ASP.NET身份班​​

Not sure if I need to paste the code for it, I think it's pretty generic and the problem actually is not with Repository/UnitOfWork as such. The issue I have is with using ASP.NET Identity classes in combination with my Repositories and Unit of Work. I'm sharing same database for membership and for all other data - and I think it's a common scenario. I cannot find the good solution how can I instantiate ASP.NET Identity classes using my repositories.

UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(_DBCONTEXT_);
this.UserManager = new UserManager<ApplicationUser>(store);



我应该到位的的DbContext 的什么,因此,它也有同感同样的DbContext与我的UnitOfWork? ?或者它如何能以其他方式来完成,使ASP.NET身份与工作的UnitOfWork

What should I put in place of DBCONTEXT, so that it would share same DbContext with my UnitOfWork? Or how it can be done in some other way to make ASP.NET Identity to work with UnitOfWork?

我试图揭露的DbContext作为的UnitOfWork类的公共财产,是这样的:

I tried exposing DbContext as public property of UnitOfWork class, something like:

UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(this.unitOfWork.MyDbContext);



不过,我不认为这是正确的 - 它不使用自定义IDbContext接口工作,使该代码进行单元测试也不好

However I don't think it's right - it doesn't work with custom IDbContext interface, and makes the code not good for unit testing.

我也试图实现CustomUserStore和CustomRoleStore - 一般来说它的工作,但我在测试它,它被要求实施更多多方法。该解决方案看起来太复杂了 - 我真的希望应该有更简单的方法。

I also tried to implement CustomUserStore and CustomRoleStore - in general it worked, but as I was testing it, it was requiring to implement more and more methods. This solution looks too complicated - I really hope there should more simple way.

推荐答案

找到某种解决方案,它看起来很普通,但我仍然不知道这是否是真的好,不打破存储库/的UnitOfWork模式的原则

Found some sort of solution, which looks generic enough, but I'm still not sure if it's really good and doesn't break Repository/UnitOfWork pattern principles.

我加入通用GetDbContext()方法来我IUnitOfWork:

I added generic GetDbContext() method to my IUnitOfWork:

public interface IUnitOfWork : IDisposable
{
   void Save();    
   IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;    
   TContext GetDbContext<TContext>() where TContext : DbContext, IDbContext;
}



及其的UnitOfWork类的实现:

Its' implementation in UnitOfWork class:

public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new()
{
    private IDbContext dbContext;

    public UnitOfWork()
    {
        this.dbContext = new TContext();
    }

    public T GetDbContext<T>() where T : DbContext, IDbContext
    {
        return this.dbContext as T;
    }

    ...
}



它是如何在控制器中使用,初始化的UserManager:

How it's used in a Controller, initializing UserManager:

public class AccountController : ControllerBase
{
    private readonly IUnitOfWork unitOfWork;

    public UserManager<ApplicationUser> UserManager { get; private set; }

    public AccountController()
        : this(new UnitOfWork<MyDbContext>())
    {
    }

    public AccountController(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;    
        UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(unitOfWork.GetDbContext<MyDbContext>());
        this.UserManager = new UserManager<ApplicationUser>(store);
    }

    ...
}



我怀疑GetDbContext()将只是用来要解决与ASP.Identity一些困难,所以可能它不是那么坏..

I suspect GetDbContext() will be used just to workaround some difficulties with ASP.Identity, so might it's not so bad..

这篇关于ASP.NET身份与工作信息库和单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆