5

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

1 回答 1

0

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

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

  • 该错误指向从 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);
    }
}
于 2013-10-25T15:11:40.167 回答