在 Entity Framework 中的存储库 Class 中使用 DbContext
Using DbContext inside a Repository Class in Entity Framework
我尝试编写 MVC n 层应用程序。我在连接到数据库期间使用了存储库模式。在我的存储库 class 中,存储库 class 中有一个上下文变量。我不确定这种方法是否正确。这是我的代码:
public class TTPDbContext : DbContext
{
public TTPDbContext() : base("TTPContext")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TTPDbContext>());
Database.Log = s => Debug.WriteLine(s);
Configuration.AutoDetectChangesEnabled = true;
}
public DbSet<Kisi> Kisiler { get; set; }
public DbSet<BakanlikBirim> BakanlikBirimleri { get; set; }
public DbSet<DisBirim> DisBirimler { get; set; }
public DbSet<Kullanici> Kullanicilar { get; set; }
public DbSet<Talep> Talepler { get; set; }
public DbSet<UnvanPozisyon> UnvanPozisyonlar { get; set; }
public DbSet<TalepDurum> TalepDurumlar { get; set; }
public DbSet<TalepKagidi> TalepKagidi { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("OsiTtp");
}
}
这是存储库 class:
public class TTPRepository : ITTPRepository,IDisposable
{
private IErrorHandling _errorHandling;
private TTPDbContext _context;
public TTPRepository()
{
this._errorHandling = new ErrorHandling();
this._context = new TTPDbContext();
}
// other sections has been dismissed for brevity.
public List<DisBirim> GetAllExternalInstitutions()
{
List<DisBirim> result = null;
DbSet<DisBirim> intermediaryresult = null;
try
{
result = new List<DisBirim>();
intermediaryresult = this._context.DisBirimler;
if (intermediaryresult != null)
{
foreach (DisBirim institution in intermediaryresult)
{
result.Add(institution);
}
}
}
catch (Exception Hata)
{
this.yazHata(Hata);
}
return result;
}
public void Dispose()
{
this._context.Dispose();
}
}
我不确定这是最佳方法。你有什么建议吗?提前致谢。
使用存储库模式和接口时,最佳做法是使用 IoC 容器将 DbContext 注入存储库构造函数。
如果您使用的是 IoC 容器,则可以控制 DbContext 的生命周期以确保 Repository 的所有实例都获得相同的上下文。
您必须使用 Unity、Ninject、Autofac、...
等 IoC 容器之一
unity 使用文档Dependency Injection in ASP.NET MVC - An Introduction
我建议阅读 msdn documentation
Instead of this
public TTPRepository()
{
this._errorHandling = new ErrorHandling();
this._context = new TTPDbContext();
}
试试这个
public TTPRepository(TTPDbContext context,ErrorHandling errorHandler)
{
this._errorHandling = errorHandler;
this._context = context;
}
至此,您的存储库已准备好使用 context
和 Error Handler
。我总是建议使用 IErrorHandler
和 IDbContext
之类的东西,而不是具体的 类。
所以你可以自由地像这样初始化。即使您使用 IoC 容器,您也可以控制上下文的生命周期。
var yourRepo = new TTPRepository(new TTPDbContext());
我尝试编写 MVC n 层应用程序。我在连接到数据库期间使用了存储库模式。在我的存储库 class 中,存储库 class 中有一个上下文变量。我不确定这种方法是否正确。这是我的代码:
public class TTPDbContext : DbContext
{
public TTPDbContext() : base("TTPContext")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TTPDbContext>());
Database.Log = s => Debug.WriteLine(s);
Configuration.AutoDetectChangesEnabled = true;
}
public DbSet<Kisi> Kisiler { get; set; }
public DbSet<BakanlikBirim> BakanlikBirimleri { get; set; }
public DbSet<DisBirim> DisBirimler { get; set; }
public DbSet<Kullanici> Kullanicilar { get; set; }
public DbSet<Talep> Talepler { get; set; }
public DbSet<UnvanPozisyon> UnvanPozisyonlar { get; set; }
public DbSet<TalepDurum> TalepDurumlar { get; set; }
public DbSet<TalepKagidi> TalepKagidi { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("OsiTtp");
}
}
这是存储库 class:
public class TTPRepository : ITTPRepository,IDisposable
{
private IErrorHandling _errorHandling;
private TTPDbContext _context;
public TTPRepository()
{
this._errorHandling = new ErrorHandling();
this._context = new TTPDbContext();
}
// other sections has been dismissed for brevity.
public List<DisBirim> GetAllExternalInstitutions()
{
List<DisBirim> result = null;
DbSet<DisBirim> intermediaryresult = null;
try
{
result = new List<DisBirim>();
intermediaryresult = this._context.DisBirimler;
if (intermediaryresult != null)
{
foreach (DisBirim institution in intermediaryresult)
{
result.Add(institution);
}
}
}
catch (Exception Hata)
{
this.yazHata(Hata);
}
return result;
}
public void Dispose()
{
this._context.Dispose();
}
}
我不确定这是最佳方法。你有什么建议吗?提前致谢。
使用存储库模式和接口时,最佳做法是使用 IoC 容器将 DbContext 注入存储库构造函数。
如果您使用的是 IoC 容器,则可以控制 DbContext 的生命周期以确保 Repository 的所有实例都获得相同的上下文。
您必须使用 Unity、Ninject、Autofac、...
等 IoC 容器之一unity 使用文档Dependency Injection in ASP.NET MVC - An Introduction
我建议阅读 msdn documentation
Instead of this
public TTPRepository()
{
this._errorHandling = new ErrorHandling();
this._context = new TTPDbContext();
}
试试这个
public TTPRepository(TTPDbContext context,ErrorHandling errorHandler)
{
this._errorHandling = errorHandler;
this._context = context;
}
至此,您的存储库已准备好使用 context
和 Error Handler
。我总是建议使用 IErrorHandler
和 IDbContext
之类的东西,而不是具体的 类。
所以你可以自由地像这样初始化。即使您使用 IoC 容器,您也可以控制上下文的生命周期。
var yourRepo = new TTPRepository(new TTPDbContext());