本文介绍了如何实现工作包含新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我的控制器内。

    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一股股的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 07:13
查看更多