设计问题 POCO 对象/DAL 访问
我想实现典型的三层架构。我当前的方法如下所示:
- DAL - 使用 EF 4.0 和每个实体的存储库。通过接口访问
- 我正在考虑使用 POCO 对象。我的第一个问题是我应该把它们放在哪里 文件?在所有其他项目引用的程序集中?
- BLL - 如何将数据从 DAL 获取到 BLL,然后最终获取到 GUI 如果我在 BLL 中拥有一大堆管理器类(例如 CustomerManager),这是一个好方法吗?这些类将访问 BLL 中相应的存储库,然后将对象传递给 GUI
或者您认为最好将存储库放在 BLL 中并直接从我所说的 ButtonEventHandler 访问它们?
希望你能为黑暗带来一丝光明
i would like to implement the typical three layer architecture. My current approach looks as follows
- DAL - with EF 4.0 and repositories for each of my entities. access through interfaces
- I was thinking of using POCO objects. My first question would be where should i put those
files? In an assembly which is referenced by all other projects? - BLL - How do i get the data from the DAL to the BLL and then finally to the GUI
Is it a good way if i would have a whole bunch of manager classes there like CustomerManager in the BLL. These classes would access the corresponding repository in the BLL and then pass the objects to the GUI
Or do you think it is better to put the repository in the BLL and access them directly from my say buttoneventhandler?
Hopefully you can bring some light into the darkness
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们在 DAL 中有存储库。 BLL 通过接口引用存储库 - 因此存储库绑定到 DAL,但与 BLL 解耦。我不知道存储库不能直接位于 BLL 中的任何原因。我们将它们放在 DAL 中,因为我们没有在其中放入任何逻辑。然后,我们在 BLL 中有“管理器”,它包装存储库并处理特定于实体的逻辑。
FWIW 实际上我们有一个通用的 Repository(Of IEntity) 并使用 unity 根据需要实例化适当的存储库 - 它非常紧凑且非常优雅。我们所有的 POCO 实体都实现 IEntity,其中包含我们所有实体所共有的
Id
、CreatedDate
等。当您需要一般处理任何类型的实体时,这会带来一些其他好处 - CreatedDate 是在调用 CreateInstance() 时由存储库设置的,ModifiedDate 是由存储库设置的> 当提交状态为“已修改”的实体时,由上下文本身设置。我们将实体保存在单独的项目中 - DAL 需要能够引用它们,BLL 也是如此。您不希望它们出现在 DAL 中,因为换出 DAL 会导致问题。您不能将它们放入 BLL 中,否则会出现循环引用。实体的配置可以存在于 DAL 中,因为它是特定于数据源的。
我们尝试坚持采用 BLL 接收原语并返回实体。请注意不要将实体在 UI 中保留太长时间,尤其是在 Web 应用程序中,因为当您将实体返回到 BLL 进行处理时(即跨存储在会话或类似内容中的请求),DAL 下可能会出现不同的上下文。可能会导致各种有趣的从上下文中附加/分离实体的过程,并失去一些好处,例如更改跟踪。
希望有帮助,但如果您需要任何说明,请告诉我
We have the repos in the DAL. The BLL references the repositories via an interface - so the repositories are tied to the DAL but decoupled from the BLL. I don't know of any reason why the repositories couldn't be directly in the BLL. We've got them in the DAL as we don't put ANY logic in them. We then have "Managers" in the BLL which wrap the repositories and handle entity-specific logic.
FWIW we actually have a generic
Repository(Of IEntity)
and use unity to instantiate the appropriate repository as required - it's very compact and quite elegant. All our POCO entities implement IEntity which containsId
,CreatedDate
, etc. which are common to ALL our entities. This gives some other benefits when you need to handle any type of entity generically -CreatedDate
is set by the repository whenCreateInstance()
is called,ModifiedDate
is set by the context itself when it commits an entity with a state ofModified
We keep entities in a separate project - The DAL needs to be able to reference them, as does the BLL. You don't want them in the DAL as swapping the DAL out would cause issues. You can't put them in the BLL or you get a circular reference. Configuration for the entities can live in the DAL as it's data source-specific.
We try to stick to the BLL taking in primitives and returning entities. Be careful about keeping entities in the UI for too long, especially in a web app as you may have a different context under the DAL by the time you return the entity to the BLL for processing (ie across requests stored in session or similar) this can result in all kinds of fun attaching/detaching entities from contexts and loses you some benefits like change tracking.
Hope that helps but if you want any clarification, let me know
这是我们的设置:
ICollection
或T
。 参考存储库(通过IRepository
通用接口)和 POCO。IQueryable
。 参考 POCO,参考 BLL。最终结果是类似堆栈的方法,并且因为基本上一切都是通过接口(和 DI 注册表)进行的,所以灵活性是巨大的。
我们有通过 DI 注入到测试项目中的模拟存储库,以及通过 DI 注入到 BLL 中的实体框架存储库。
UI 中的示例流程 -> DB:
如你所见,我是 DI 的粉丝。
This is our setup:
ICollection<T>
orT
. References Repositories (viaIRepository<T>
generic interface) and POCOs.IRepository<T>
to provide basic persistence against underlying store. Find, Add, Remove, etc. ReturnsIQueryable
. References POCO's, Referenced by BLL.The end result is a like stack-like approach, and because basically everything is via interfaces (and DI registries), the flexibility is enormous.
We have Mock Repositories which get injected into the test projects via DI, and Entity Framework Repositories which get injected into the BLL via DI.
Example flow from UI -> DB:
As you can see, i am a DI fanboy.
您可以通过将存储库和 POCO 保留在同一个项目中来保持这一点非常简单。这本质上就是您的数据域模型。您的 POCO 是公共的,您的存储库接口也是公共的。您应该将具体存储库保留在该项目内部。
您的 BLL 可以是经典的外观或服务定位器。该项目将处理您的数据并应用任何相关的业务规则,然后再将其移交给 UI。它还负责在将数据发送到 DAL 之前验证来自 UI 的数据。
You can keep this really simple by keeping your repositories and POCOs in the same project. This would essentially be your Data Domain Model. Your POCOs are public and so are your repository interfaces. You should keep your concrete repositories internal to this project.
Your BLL could be a classic Facade or Service Locator. This project would massage your data and apply any relevant business rules before handing it over to the UI. This would also be responsible for validating data coming in from the UI before sending it over to the DAL.