如何實現包含新IdentityUser的工作單元

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

我想知道將UserManager添加到我的工作單元的最佳方法是什麼。我應該使用IUstrore接口並在控制器中添加新的UserManager嗎?我應該在UnitOfWork中使用UserManager還是應該做些不同的事情?

這是我的工作單元和控制器實現的兩個想法。

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

控制器:

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

選項2在工作單元中創建用戶管理器。

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

控制器:

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

注意:我使用的是Asp.net MVC5,c#,Entity Framework 6.此外,我的工作單元中還有其他存儲庫,但我將它們留在了用戶實現上。

無論哪種方式起作用,我都會收到此警告,但要擺脫錯誤,我調用了this.userStore.Dispose();錯誤下面是我對Dispose的實現。目前我正在我的工作單元中調用userStore上的dispose。我還在我的控制器中為這個userStore創建了一個用戶管理器,我也在我的控制器內的userManager上調用dispose。

應該處理CA2213一次性字段'UnitOfWorkPds'包含IDisposable類型的字段'UnitOfWorkPds.userManager':'UserManager'。更改“UnitOfWorkPds”上的Dispose方法以在此字段上調用Dispose或Close。 UnitOfWorkPds.cs 96

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

熱門答案

您需要查看UnitOfWork模式。 工作單位模式和堅持無知

對於錯誤問題,請確保您有以下代碼段。

  • 該錯誤指向從Dispose of UnitOfWorkPds類中調用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);
    }
}



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因