简体   繁体   English

如果Entity Framework / DbContext是DAL / Repository,它在哪里适合3层架构?

[英]If Entity Framework / DbContext is the DAL / Repository, where does it fit within 3-tier architecture?

I've been reading articles on StackOverflow and other sites all day about best architecture practices and there are just so many conflicting ideas and opinions. 我一直在阅读关于StackOverflow和其他网站的关于最佳架构实践的文章,并且存在许多相互矛盾的想法和观点。

I've finally settled on an approach, but I am having a really hard time deciding where to place the EF objects (DbContext, Fluent APIs, Seeding data, etc). 我最终确定了一种方法,但我很难确定放置EF对象的位置(DbContext,Fluent API,种子数据等)。 Here is what I currently have: 这是我目前拥有的:

ASP.NET MVC Project : The actual web project. ASP.NET MVC项目 :实际的Web项目。 Contains the standard views, controllers and View Models (inside a Models folder). 包含标准视图,控制器和视图模型(在Models文件夹中)。

Domain Model Project : Contains all POCO classes that define the database (domain) objects. 域模型项目 :包含定义数据库(域)对象的所有POCO类。 Currently, does not mention or reference any EF objects. 目前,没有提及或引用任何EF对象。

Service Layer Project : Contains service objects for each type of domain object (eg, IProductService, IOrderService, etc). 服务层项目 :包含每种类型的域对象的服务对象(例如,IProductService,IOrderService等)。 Each service references EF objects like DbSets and handles business rules - eg, add a Product, fetch a Product, append a Product to an Order, etc. 每个服务都引用像DbSets这样的EF对象并处理业务规则 - 例如,添加产品,获取产品,将产品附加到订单等。

So the question is, in this configuration, where do EF classes go? 所以问题是,在这个配置中,EF类会去哪里? Initially I thought in the Service Layer, but that doesn't seem to make sense. 最初我想在服​​务层,但这似乎没有意义。 I then thought to put them in the Domain Model Layer, but then it ties the Domain Models to EF, which is essentially a DAL / Repository. 然后我想把它们放在域模型层中,但是它将域模型绑定到EF,它本质上是一个DAL / Repository。 Finally, I thought about creating a separate DAL Project just for EF, but it seems like a huge waste considering it will likely have 3-4 files in it (DbContext and a few other small files). 最后,我考虑为EF创建一个单独的DAL项目,但考虑到它可能有3-4个文件(DbContext和一些其他小文件),这似乎是一个巨大的浪费。

Can anyone provide any guidance? 有人可以提供任何指导吗?

There is no need for Domain Model since it will be redundancy. 不需要域模型,因为它将是冗余。 EF classes directly can act as Domain Model and they are converted to View Models while sending it to View. EF类直接可以充当域模型,并在将其发送到View时将它们转换为View Models。 EF can be separated into different class library. EF可以分成不同的类库。 Most of them use repository pattern along with any ORM incase it would be easy if they go for replacement. 他们中的大多数都使用存储库模式以及任何ORM,如果他们要替换它们会很容易。 But I've seen criticism over using repository pattern, check this out. 但我已经看到了使用存储库模式,请批评这一出。

Here is what I do: 这是我做的:

Data: 数据:

  • Has one class inheriting from DbContext. 有一个类继承自DbContext。
    • It has all the db sets. 它拥有所有数据库集。
    • Overrides OnModelCreating. 覆盖OnModelCreating。
    • Mapping primary keys and relationships. 映射主键和关系。

Entities: 实体:

  • Has every POCO classes. 有每个POCO课程。
    • Each property is decorated with needed data annotations. 每个属性都装饰有所需的数据注释。

Services: 服务:

  • Each service has common methods (GetList(), Find(), Create(), etc.). 每个服务都有常用的方法(GetList(),Find(),Create()等)。

Business: 商业:

  • Called from clients, orchestrate using services to perform a specific task UserChangePassword (this will check if this can be performed, then perform the task, or return error/unauthorized statuses among many others to make the client shows the correct information regarding the task. This on my case is where I log. 从客户端调用,使用服务协调执行特定任务UserChangePassword(这将检查是否可以执行此任务,然后执行任务,或返回错误/未授权状态以使客户端显示有关任务的正确信息。 在我的情况下是我记录的地方。

Clients (Desktop/Web/Wpf/etc). 客户端(桌面/ Web / Wpf /等)。

I'm not saying this is the best approach, I'm just sharing what's been working for me. 我不是说这是最好的方法,我只是分享一直在为我工作的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM