How to implement a Unit of work containing the new IdentityUser

asp.net-mvc-5 c# entity-framework-6

Question

What is the best approach for me to include the UserManager into my work unit? Should I install a new UserManager to my controller and use the IUstrore interface? Should I use UserManager exclusively in my UnitOfWork, or should I use another approach?

Here are two concepts I had for the implementation of my work unit and controller.

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

Create the usermanager in the unit of work using option 2.

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'm utilizing Entity Framework 6 and Asp.net MVC 5 with C#. In my unit of work, I also have other repositories, but I left them out in order to concentrate on the User implementation.

Either way initially functions. I was given this warning, but I contacted this to fix the problem. userStore.Dispose(); The Dispose implementation is underneath the mistake. In my Unit of Work, I'm now calling discard on the userStore. In my controller, I also build a user manager for this userStore, and I also call dispose on userManager there.

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
5
10/25/2013 4:32:05 PM

Popular Answer

Reviewing the UnitOfWork Pattern is necessary.The Pattern Of The Unit Of Work And Persistent Ignorance

Make sure you have the following code snippets for the error issue.

  • The fault is caused by using the Dispose method inside the UnitOfWorkPds class' Dispose method.
  • You are contacting userStroe, which is supposed to be userManager.
  • IUnitOfWorkPds is not implemented in the following samples.

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);
    }
}
0
10/25/2013 3:11:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow