【发布时间】:2013-12-06 16:49:27
【问题描述】:
我有项目
Domain.Model (contains code first POCOs)
Data.Context (contains the context & migrations only)
Data.Access (contains IGenericRepository & GenericRepository)
Service (contains BL service classes and UnitsOfWork)
Presentation.Admin (an Asp.Net Webforms web application)
我将我的 POCO 用作所有层的业务对象。我知道对此存在一些争论,但这也被广泛接受。
所以我有 Presentation 调用服务 > 通过存储库获取 POCO > 返回到 Presentation 并显示,例如 HTML 表并将编辑保存回 DB - 很棒。
现在我有一个更复杂的页面,我认为它需要一个业务对象。这是一个类似的例子。
POCO
public class Book
{
BookId
string ExternalReference
}
public class Movie
{
int MovieId
string ExternalReference
}
建议的业务对象
public MovieAdaptation
{
Book book;
Movie movie;
}
所以 ExternalReference 是外部的,不能是我数据库中的公共外键,因此我不能只使用导航属性来做 Book.Movie。我需要做一个 LINQ 连接(可能)。
所以我的问题是:
1) 我应该在哪里定义这个业务对象。目前它只是在服务层,因为只有引用服务层的东西才会使用它。
2) 我应该在哪里构建这个业务对象?它应该位于 Data.Access 中的存储库中还是更高级的存储库中?
3) 如何使用 LINQ 构建它。这是迄今为止我最好的镜头,但它似乎效率很低,特别是如果我要返回这些列表。
namespace MyProject.Services
{
public class AdaptationsService
{
AdaptationUnitOfWork _unitOfWork;
public AdaptationService
{
unitOfWork = new AdaptationUnitOfWork();
}
public Adaption GetAdaptations(string externalReference)
{
//Can anyone improve this maybe using LINQ join (as maybe it won't be getting books/movies by SingleOrDefault but by where
Book book= _unitOfWork.BookRepository.Get.SingleOrDefault(b=>b.ExternalReference==externalReference);
Movie movie= _unitOfWork.MovieRepository.Get.SingleOrDefault(m=>m.ExternalReference==externalReference);
Adaptation adaptation = new Adaptation();
adaptation.Book=book;
adaptation.Movie=movie;
}
}
}
【问题讨论】:
标签: linq entity-framework ef-code-first