[英]How to use the repository pattern in ASP.NET Core MVC together with Entity Framework Core?
[英]How to use repository pattern in ASP.NET Core MVC while IdentityDbContext has been used?
我在我的項目中使用了 IdentityDbContext。 我的數據庫有一些相互連接的表(關系)。 我想使用存儲庫模式。 我聲明了我所有的接口。 然后,我嘗試實現它們。 問題是我無法創建 IdentityAppContext 的實例,因為構造函數需要一個輸入參數“選項”。 我該如何實施它們?
IdentityAppContext.cs:
public class IdentityAppContext: IdentityDbContext<AppUser, AppRole, int>
{
public IdentityAppContext(DbContextOptions<IdentityAppContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
public DbSet<AppUser> Users { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<PM> PMs { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<FileRepository> Files { get; set; }
}
IPmRepository.cs:
public interface IPmRepository
{
IEnumerable<PM> GetAllPMs();
PM GetPmById(int pmId);
bool InsertPM(PM pm);
bool UpdatePM(PM pm);
bool DeletePM(int pmId);
bool DeletePM(PM pm);
void Save();
}
PmRepository.cs:
public class PmRepository : IPmRepository
{
IdentityAppContext db = new IdentityAppContext();
public IEnumerable<PM> GetAllPMs()
{
}
public PM GetPmById(int pmId)
{
throw new NotImplementedException();
}
public bool InsertPM(PM pm)
{
throw new NotImplementedException();
}
public bool UpdatePM(PM pm)
{
throw new NotImplementedException();
}
public bool DeletePM(int pmId)
{
throw new NotImplementedException();
}
public bool DeletePM(PM pm)
{
throw new NotImplementedException();
}
public void Save()
{
throw new NotImplementedException();
}
}
假設 DbContext 已在 Startup.cs 文件中使用 DI 容器注冊,使用如下內容:
services.AddDbContext<IdentityAppContext>(opt =>
{
opts.UseSqlServer(myConnectionString);
})
然后以與上述相同的方法添加以下內容,將在同一個 DI 容器中注冊您的存儲庫:
services.AddScoped<IPmRepository, PmRepository>();
然后在您的存儲庫中:
public class PmRepository : IPmRepository
{
readonly IdentityAppContext _context;
// 'context' parameter is automagically provided by the container.
public PmRepository(IdentityAppContect context)
{
_context = context;
}
public bool InsertPM(PM pm)
{
_context.PMs.Add(pm);
}
}
然后,在您的 controller(也由容器提供)中,您應該能夠在構造函數中從容器請求 IPmRepository:
public class MyController : Controller
{
readonly IdentityAppContext _context;
readonly IPmRepository _pmRepository;
// Both 'context' and 'pmRepository' are automagically provided by the container
public MyController(
IdentityAppContext context,
IPmRepository pmRepository)
{
_context = context;
_pmRepository = pmRepository;
}
[HttpPost]
public async Task DoSomething(MyRequest request)
{
_pmRepository.InsertPM(new PM() { Id = request.Id });
await _context.SaveChangesAsync();
}
}
注意,由於 IdentityAppContext 和 IPmRepository 與 MyController 注冊在同一個容器中,因此框架可以自動為 MyController 構造函數提供參數。
此外,當為 Controller 創建 PmRepository 時,容器可以查看 PmRepository 的構造函數參數並看到它需要一個 IdentityAppContext,它可以自動提供,因為它注冊在同一個容器中。
參考我的示例項目,我已經使用工作單元實現了存儲庫模式。 項目還沒有完全完成,但我希望你能得到一個想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.