How to implement a Unit of work containing the new IdentityUser

不羁的心 提交于 2020-01-22 22:38:09

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/19571131/how-to-implement-a-unit-of-work-containing-the-new-identityuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!