0
我有一种情况,我通过包装使用上下文。包装器用于服务层。问题是如何在这种情况下模拟上下文?如何通过包装模拟上下文
语境
public class DbContext : System.Data.Entity.DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
base.OnModelCreating(modelBuilder);
}
public DbSet<Report> Reports { get; set; }
}
包装
public interface IDbContextWrapper<out TContext> where TContext : System.Data.Entity.DbContext, new()
{
T Call<T>(Func<TContext, T> func);
void Call(Action<TContext> action);
}
包装实施
public class DbContextWrapper<TContext> : IDbContextWrapper<TContext> where TContext : System.Data.Entity.DbContext, new()
{
public virtual TContext BuildContext(bool enableChangeTracking = true, bool enableLazyLoading = false, bool enableProxyCreation = false)
{
var result = new TContext();
result.Configuration.AutoDetectChangesEnabled = enableChangeTracking;
result.Configuration.LazyLoadingEnabled = enableLazyLoading;
result.Configuration.ProxyCreationEnabled = enableProxyCreation;
return result;
}
public virtual T Call<T>(Func<TContext, T> func)
{
using (var context = BuildContext())
{
return func(context);
}
}
public virtual void Call(Action<TContext> action)
{
using (var context = BuildContext())
{
action(context);
}
}
}
,服务和它的方法,我需要进行单元测试与嘲笑
public class ReportService : IReportService
{
private readonly IDbContextWrapper<DbContext> _contextWrapper;
public ReportService(IDbContextWrapper<DbContext> contextWrapper)
{
_contextWrapper = contextWrapper;
}
public Report GetPreviousReport(int currentReportId, int lineId)
{
return _contextWrapper.Call(
context => context.Reports
.Where(item => item.Id < currentReportId && item.LineId == lineId)
.OrderBy(item => item.Id)
.First());
}
public Report GetNextReport(int currentReportId, int lineId)
{
return _contextWrapper.Call(
context => context.Reports
.Where(item => item.Id > currentReportId && item.LineId == lineId)
.OrderBy(item => item.Id)
.First());
}
}