I am wondering what is the best way to add a UserManager to my Unit of Work. Should I use the IUstrore interface and add a new UserManager to my controller? Should I just use the UserManager in my UnitOfWork or do I need to do something else?
Here are two ideas that 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;
}
}
}
public interface IUnitOfWorkPds
{
void Save();
void Dispose();
IUserStore<ApplicationUser> UserStore { get; }
}
Controller:
var Umanager = new UserManager<ApplicationUser>(unitOfWorkPds.UserStore);
Umanager.Dispose();
Option 2 will create a usermanager in a 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 use Asp.net MVC5, C #, Entity Framework 6. I also have other repositories in my part of the work, but I left them to focus on the user implementation.
, , , this.userStore.Dispose(); Dispose. userStore . userStore , dispose userManager .
CA2213 . 'UnitOfWorkPds' 'UnitOfWorkPds.userManager', IDisposable: 'UserManager. Dispose 'UnitOfWorkPds' Dispose Close . 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);
}