如何实现包含新 IdentityUser 的工作单元 [英] How to implement a Unit of work containing the new IdentityUser

查看:23
本文介绍了如何实现包含新 IdentityUser 的工作单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将 UserManager 添加到我的工作单元的最佳方法是什么.我应该使用 IUstrore 接口并在我的控制器中添加一个新的 UserManager 吗?我应该只在我的 UnitOfWork 中使用 UserManager 还是应该做一些不同的事情?

I am wondering what is the best way to add the UserManager to my Unit of work. Should I use the IUstrore interface and add a new UserManager in my controller? Should I just use UserManager in my UnitOfWork or should I do something different?

这是我对工作单元和控制器实现的两个想法.

Here is two ideas I had for my unit of work and controller implementation.

public class UnitOfWorkPds : IUnitOfWorkPds, IDisposable
{
    private ApplicationDbContext context = new ApplicationDbContext();
    private IUserStore<ApplicationUser> userStore;

    public IUserStore<ApplicationUser> UserStore
    {
        get
        {
            if (this.userStore == null)
            {
                this.userStore = new UserStore<ApplicationUser>(context);
            }

            return userStore;
        }
    }
}

//interface
public interface IUnitOfWorkPds
{
    void Save();
    void Dispose();

    IUserStore<ApplicationUser> UserStore { get; }
}

控制器:

 var Umanager = new UserManager<ApplicationUser>(unitOfWorkPds.UserStore);
 Umanager.Dispose();

选项 2 在工作单元中创建用户管理器.

Option 2 create the usermanager in the unit of work.

public class UnitOfWorkPds : IUnitOfWorkPds, IDisposable
{
    private ApplicationDbContext context = new ApplicationDbContext();
    private UserManager<ApplicationUser> userManager;

    public UserManager<ApplicationUser> UserManager
    {
        get
        {

            if (this.userManager == null)
            {
                this.userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            }

            return userManager;
        }
    }
}

public interface IUnitOfWorkPds
{
   void Save();
   void Dispose();
   UserManager<ApplicationUser> UserManager { get; }
}

控制器:

 var UManager = unitOfWorkPds.UserManager
 Umanager.Dispose();

注意:我使用的是 Asp.net MVC5、c#、Entity Framework 6.我的工作单元中还有其他存储库,但我没有将它们集中在用户实现上.

Note: I am using Asp.net MVC5, c#, Entity Framework 6. Also I have other repositories in my unit of work but I left them out to focus on the User implementation.

任何一种方法一开始我都会收到这个警告,但是为了摆脱我称之为 this.userStore.Dispose(); 的错误.错误下方是我对 Dispose 的实现.目前我正在我的工作单元中的 userStore 上调用 dispose.我还在我的控制器中为此 userStore 创建了一个用户管理器,并且我还在我的控制器中调用了 userManager 上的 dispose.

Either way works at first I receive this warning but to get rid of the error I called this.userStore.Dispose(); Below the error is my implementation of Dispose. Currently i'm calling dispose on the userStore in my Unit of Work. I also create a user manager for this userStore in my controller and I am also calling dispose on userManager inside my controller.

CA2213 Disposable fields 应该被处理 'UnitOfWorkPds' 包含IDisposable 类型的字段UnitOfWorkPds.userManager":'用户管理器'.更改 Dispose 方法'UnitOfWorkPds' 在此调用 Dispose 或 Close场地.UnitOfWorkPds.cs 96

CA2213 Disposable fields should be disposed 'UnitOfWorkPds' contains field 'UnitOfWorkPds.userManager' that is of IDisposable type: 'UserManager'. Change the Dispose method on 'UnitOfWorkPds' to call Dispose or Close on this field. UnitOfWorkPds.cs 96

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                if(this.userStore != null)
                { this.userStore.Dispose(); }                  
                context.Dispose();

            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);

        GC.SuppressFinalize(this);
    }

推荐答案

你需要复习 UnitOfWork Pattern.工作单元模式和持久性无知

对于错误问题,请确保您有以下代码片段.

For the issue of error make sure you have following code snippets.

  • 错误指向从 UnitOfWorkPds 类的 Dispose 内部调用 Dispose.
  • 您正在调用 userStroe,它应该是 userManager
  • 以下代码段缺少 IUnitOfWorkPds 的实现
public class UnitOfWorkPds : IUnitOfWorkPds, IDisposable
{
    private ApplicationDbContext context = new ApplicationDbContext();
    private UserManager<ApplicationUser> userManager;
    public UserManager<ApplicationUser> UserManager
    {
        get
        {

            if (this.userManager == null)
            {
                this.userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            }
            return userManager;
        }
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if(this.userManager != null)
            { this.userManager.Dispose(); }                  
            context.Dispose();
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
       GC.SuppressFinalize(this);
    }
    // Disposable types implement a finalizer.
    ~UnitOfWorkPds()
    {
        Dispose(false);
    }
}

这篇关于如何实现包含新 IdentityUser 的工作单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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