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

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

问题描述

我想知道什么是对的UserManager添加到我的工作单元的最佳途径。我应该使用IUstrore接口,并在我的控制器中添加新的UserManager?如果我只是使用的UserManager在我的UnitOfWork或者我应该做不同的事情?

下面是两个概念我对我的工作和控制器实现的单位。

 公共类UnitOfWorkPds:IUnitOfWorkPds,IDisposable接口
{
    私人ApplicationDbContext语境=新ApplicationDbContext();
    私人IUserStore< ApplicationUser> userStore;    公共IUserStore< ApplicationUser> UserStore
    {
        得到
        {
            如果(this.userStore == NULL)
            {
                this.userStore =新UserStore< ApplicationUser>(上下文);
            }            返回userStore;
        }
    }
}//接口
公共接口IUnitOfWorkPds
{
    无效保存();
    无效的Dispose();    IUserStore< ApplicationUser> UserStore {搞定; }
}

控制器:

  VAR Umanager =新的UserManager< ApplicationUser>(unitOfWorkPds.UserStore);
 Umanager.Dispose();

选项2创建工作单位的UserManager。

 公共类UnitOfWorkPds:IUnitOfWorkPds,IDisposable接口
{
    私人ApplicationDbContext语境=新ApplicationDbContext();
    私人的UserManager< ApplicationUser>的UserManager;    公众的UserManager< ApplicationUser>的UserManager
    {
        得到
        {            如果(this.userManager == NULL)
            {
                this.userManager =新的UserManager< ApplicationUser>(新UserStore< ApplicationUser>(上下文));
            }            返回的UserManager;
        }
    }
}公共接口IUnitOfWorkPds
{
   无效保存();
   无效的Dispose();
   的UserManager< ApplicationUser> {的UserManager获得; }
}

控制器:

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

请注意:我使用的Asp.net MVC5,C#,实体框架6.也是我在工作单位等信息库,但我离开他们去关注用户执行

无论哪种方式,工作在我第一次收到此警告,但为了摆脱我叫this.userStore.Dispose错误的();下面的错误是我的执行处置。目前,我打电话给我的工作单位处置的userStore。我也创建了用户管理器为这个userStore在我的控制器,我也呼吁处置的UserManager我的控制器内。


  

CA2213一次性领域应释放'UnitOfWorkPds包含
  现场UnitOfWorkPds.userManager这是IDisposable的类型:
  的UserManager。改变Dispose方法
  UnitOfWorkPds'调用Dispose或关闭这个
  领域。 UnitOfWorkPds.cs 96


 私人BOOL处置= FALSE;    受保护的虚拟无效的Dispose(BOOL处置)
    {
        如果(!this.disposed)
        {
            如果(处置)
            {
                如果(this.userStore!= NULL)
                {this.userStore.Dispose(); }
                context.Dispose();            }
        }
        this.disposed = TRUE;
    }    公共无效的Dispose()
    {
        处置(真);        GC.Sup pressFinalize(本);
    }


解决方案

您需要查看的UnitOfWork模式。
工作模式和持久性的无知单位

有关错误的问题,确保你有以下code片段。


  • 的错误点从UnitOfWorkPds处分的类中调用Dispose。

  • 您在呼唤userStroe其中应的UserManager

  • 以下片段缺失实施IUnitOfWorkPds的


 公共类UnitOfWorkPds:IUnitOfWorkPds,IDisposable接口
{
    私人ApplicationDbContext语境=新ApplicationDbContext();
    私人的UserManager< ApplicationUser>的UserManager;
    公众的UserManager< ApplicationUser>的UserManager
    {
        得到
        {            如果(this.userManager == NULL)
            {
                this.userManager =新的UserManager< ApplicationUser>(新UserStore< ApplicationUser>(上下文));
            }
            返回的UserManager;
        }
    }    受保护的虚拟无效的Dispose(BOOL处置)
    {
        如果(处置)
        {
            如果(this.userManager!= NULL)
            {this.userManager.Dispose(); }
            context.Dispose();
        }
        this.disposed = TRUE;
    }    公共无效的Dispose()
    {
        处置(真);
       GC.Sup pressFinalize(本);
    }
    //一次性类型实现一个终结。
    〜UnitOfWorkPds()
    {
        处置(假);
    }
}

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; }
}

Controller :

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

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; }
}

Controller :

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

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.

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 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);
    }

解决方案

You need to review the UnitOfWork Pattern. The Unit Of Work Pattern And Persistence Ignorance

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

  • The error points to calling Dispose from inside the Dispose of UnitOfWorkPds class.
  • You are calling userStroe which shall be userManager
  • Following snippets is missing implementation of 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天全站免登陆