Ric*_*ard 7 entity-framework unit-of-work repository-pattern
让我说,我已经得出结论(经过大量试验),使用实体框架时,存储库和工作单元是错误的,错误的,错误的,这就说明了为什么这么好.
但我真的很讨厌这些嵌入式查询.问题是,如果我是如此反对存储库等,我可以在哪里放置它们?(只有请干净的答案,非常感谢的例子).
我刚刚完成了两个包含我的存储库,工作单元和数百个文件接口的项目,因为回报无处可见.我认为很多人,包括我自己,只是跳上了Repository的潮流,因为这是其他所有人都在做的事情,但回想起来,我认为这真是一个无处可去的地方.
/叹
理查德
您希望将它们放在哪里?你只有几个选择:
IQueryable且不得接受Expression作为参数=整个查询逻辑必须包装在该方法中。但这将使您的类覆盖相关方法,就像存储库(唯一可以模拟或伪造的)。此实现接近于存储过程使用的实现。自定义扩展方法示例:
public static IQueryable<TEntity> GetByName(this IQueryalbe<TEntity> query, string name)
where TEntity : IEntityWithName
{
return query.Where(e => e.Name == name);
}
Run Code Online (Sandbox Code Playgroud)
自定义类公开方法的示例:
public class QueryProvider
{
public QueryProvider() {}
public IEnumerable<TEntity> GetByName(IYourContext context, string name)
where TEntity : IEntityWithName
{
return context.CreateObjectSet<TEntity>().Where(e => e.Name == name).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2333 次 |
| 最近记录: |