10

我喜欢保持项目的结构尽可能干净。样本:

--BlogApp.sln
  --BlogApp.Data
      BlogModel.edmx (the EF mappings)
      Post.cs (I end up having partial classes in here with attributes)
  --BlogApp.Domain
    --Entities
        Post.cs (I would like to have my POCOs here with all its additional logic)
    --Repositories
        PostsRepository.cs
  --BlogApp.Ui
      (standard MVC structure)

当我使用 EF 作为我的 ORM 时,我最终陷入了混乱。有人可以建议一些“干净”的方式来构建项目吗?或者,也许您可​​以建议一些最常用的标准项目结构。

4

2 回答 2

22

我的首选结构是:

Solution
  -- Common
       - Shared features used accross all layers
       - You can also place interfaces for repositories and uow here
  -- Entities - shared among DataAccess, Business (and UI in small projects)
       - T4 template + partial classes + custom enums  
       - partial classes can contain methods with domain logic => domain objects 
  -- DataAccess - all EF dependent code here
       - EDMX or code first mapping
       - Repositories
       - UnitOfWork
  -- Business - not every project needs this assembly
       - Business services 
       - Logic like workflows
       - DTOs exposed to UI
  -- UI
       - Controllers
       - Views
       - ViewModels
于 2011-03-12T16:59:55.190 回答
2

查看这篇关于T4 模板和实体框架的文章。您可以为通过 EF 生成的实体属性编写自定义属性。我已经这样做了好几次,在弄清楚如何做之后,它现在节省了很多时间。正如您所提到的,我之前尝试过使用部分类,但是我的 EF 生成的类最终会用自定义属性覆盖另一个类。也许我做错了什么,但无论如何,我现在更喜欢 T4 模板解决方案,因为它对我来说似乎更干净 - 最大限度地减少项目中的类数量。

此外,当您从 DB 更新 EF 模型并重新生成类时,您的自定义属性仍然存在。英尺!

添加:顺便说一下,我们通常在数据层中定义我们的模型对象,以使它们由 EF 的实体映射/填充。或者(甚至更简单)使用 EF 生成的实体一直到 UI 层,而无需自定义定义的 POCO。

于 2011-03-12T14:48:04.933 回答