如果实体框架/的DbContext是DAL /存储库,在那里它在3层架构合适? [英] If Entity Framework / DbContext is the DAL / Repository, where does it fit within 3-tier architecture?

查看:540
本文介绍了如果实体框架/的DbContext是DAL /存储库,在那里它在3层架构合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读了一整天关于最佳架构实践文章,计算器和其他网站,也有只是这么多相互矛盾的想法和意见。

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.

我终于选定了一种方法,但我有一个很艰难的时期决定在何处放置EF对象(的DbContext,流利的API,播种数据等)。以下是我目前有:

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). Here is what I currently have:

ASP.NET MVC项目:实际的Web项目。包含标准视图,控制器和视图模型(里面的模型的文件夹)。

ASP.NET MVC Project: The actual web project. Contains the standard views, controllers and View Models (inside a Models folder).

域模型项目:包含所有POCO类定义数据库(域)的对象。目前,并没有提及或引用任何EF对象。

Domain Model Project: Contains all POCO classes that define the database (domain) objects. Currently, does not mention or reference any EF objects.

服务层项目:包含服务对象为每种类型的域对象(例如,IProductService,IOrderService等)。每个服务引用EF对象,如DbSets和处理业务规则 - 比如,添加一个产品,获取一个产品,附加产品以订单等

Service Layer Project: Contains service objects for each type of domain object (e.g., IProductService, IOrderService, etc). Each service references EF objects like DbSets and handles business rules - e.g., add a Product, fetch a Product, append a Product to an Order, etc.

所以,问题是,在这种配置下,你在哪里EF班去了?起初我还以为在服务层,但是这似乎并没有什么意义。转念一想将它们放在域模型层,但它关系的领域模型到EF,它本质上是一个DAL /存储库。最后,我想到了创建一个独立的DAL项目只为EF,但它似乎是一个巨大的浪费考虑到它可能将不得不在它(的DbContext和其他几个小文件)3-4文件。

So the question is, in this configuration, where do EF classes go? 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. 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).

任何人都可以提供任何指导?

Can anyone provide any guidance?

推荐答案

有没有必要的域模型,因为这将是冗余。 EF类直接可以充当域模型和它们转换查看模式,同时把它发送到视图。 EF可以分离成不同的类库。他们大多使用存储库模式与任何ORM柜面如果他们去更换它会很容易。通过使用存储库的模式,但我见过的批评,检查<一href="http://ayende.com/blog/4784/architecting-in-the-pit-of-doom-the-evils-of-the-repository-abstraction-layer"相对=nofollow>此内容了。

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 can be separated into different class library. Most of them use repository pattern along with any ORM incase it would be easy if they go for replacement. But I've seen criticism over using repository pattern, check this out.

这篇关于如果实体框架/的DbContext是DAL /存储库,在那里它在3层架构合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆