业务对象和数据层

发布于 2024-09-19 17:14:25 字数 801 浏览 7 评论 0原文

该网站为我提供了许多有用的答案,但是经过一个小时的搜索,我没有找到任何可以具体满足我的需求的内容。所以这里是...

我工作的公司正在设计一个新的业务对象层和数据访问层 - 这些将驻留在单独的程序集中。

问题是我很难理解这两层之间的交互 ​​- 具体来说,如果 DAL 了解 BOL,我读过很多文章,这些文章都说依赖顺序应该是这样的:

GUI / 演示 -->玻尔 ---> DAL

但据我所知,DAL 需要对 BOL 的引用,以便能够将对象“返回”到 BOL 层。

我将在 BOL 和 DAL 之间建立一个中间组件,它基本上是一个薄层,其中填充了用于解耦这两个 DLL 的接口,因此如果需要,框架可以使用不同的 DAL。

这让我想到引入另一个薄层,其中包含 BO 实现的一堆接口,然后当 BOL 调用 DAL 接口时,它会向其传递一个实现这些 BO 接口之一的对象,然后 DAL 继续填充目的。这消除了 BOL 和 DAL 之间的所有依赖关系 - 然而,老实说,我发现很难证明它是合理的。

理想情况下,我们希望使用 ORM,因为它只是消除了编写 CRUD 内容的需要,但我们的客户习惯于摆弄数据库上的列长度,这是我们迄今为止使用强类型 DataTable 时出现的大多数错误的原因。我听说 Linq2SQL 也在编译时存储列长度,不确定 NHibernate 是否这样做(但是,我不确定我们的数据库架构是否为 NHibernate 设计得足够干净,处理遗留系统的陷阱)。

所以,是的,任何关于 BOL 和 DAL 之间关系的见解都将非常受欢迎 - 如果上述内容写得不好,我深表歉意,如果有人需要澄清,我将很乐意提供更多细节。

马龙

This site has provided me with many useful answers, however after a hours search I haven't found anything that specifically answers my needs. So here goes...

The company I'm working for is in the process of designing a new Business Objects Layer and a Data Access Layer - these will reside in separate assemblies.

The problem is I'm having a hard time getting my head around the interaction between these two layers - specifically, should the DAL know about the BOL, I've read numerous articles that have said the dependency order should go something like this:

GUI / Presentation --> BOL ---> DAL

But as far as I can see, the DAL needs a reference to the BOL in order to be able to 'return' objects to the BOL layer.

I'm going for a intermediate assembly between the BOL and DAL which will be basically a thin layer filled with interfaces to decouple those two DLL's, so the framework can use different DALs if the need arises.

This lead me to the idea of introducing another thin layer with a bunch of interfaces that the BOs implement, then when the BOL calls the DAL interface, it passes it an object which implements one of these BO interfaces and then the DAL proceeds to populate the object. This removes all dependencies between the BOL and the DAL - however, I'm finding it hard to justify it to be honest.

Ideally we would like to use an ORM as it just removes the need to write CRUD stuff but our clients have a habit of fiddling with column lengths on their database and this is the cause of most of our errors to-date using the strongly typed DataTables. I've heard Linq2SQL also stores column lengths at compile time, not sure if NHibernate does or not (but, I'm not sure if our Database Schema is designed cleanly enough for NHibernate, pitfalls of dealing with legacy systems).

So yea, any insight on the relationship between a BOL and a DAL would be very much welcome - apologies if the above is poorly written, if anyone needs clarification I'll be happy to provide more detail.

Marlon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

合约呢 2024-09-26 17:14:48

这些是我的发现,
1. 使用接口
2.在DAL和DAL之间使用DTO[数据传输对象] BLL
3.将BLL一分为二,
<代码>a. BLL
<代码>b.服务层
4. 使用控制反转 (IoC) 容器来保持尽可能低的耦合。

These are my findings,
1. Use interfaces
2. Use DTOs [Data Transfer Objects] between DAL & BLL
3. Split BLL into two,
a. BLL
b. Service Layer
4. Use Inversion of Control (IoC) container for keep coupling as low as possible.

芯好空 2024-09-26 17:14:46

它还取决于您是否使用/使用什么库/ORM(对象关系映射器)。当使用(好的)ORM 时,DAL 应该是一个非常遥远的问题,因为它几乎完全被 ORM 隐藏;然而,最佳实践表明,即使如此,对于中型到大型应用程序,您也应该在 BOL 和 ORM 之间引入另一层,通常是 DTO(数据传输对象)。 DTO 也可以在没有 ORM 的情况下使用,因为它们只是在单独的库中定义的哑对象,DAL 可以负责持久化它们(将它们转换为数据库结构),而 BOL 可以查询 DAL 并接收这些对象对象。

可以通过多种方式实现层的解耦,最常见的是通过接口和/或 MEF 或其他 DI/IOC 框架。如果有效使用,任何此类技术都可以实现足够的解耦。

另外,根据所使用的技术,正如西西弗斯所说,分层架构模式之一将有助于很好地分离关注点:MVC、MVP、MVVM 等。我个人推荐 MVVM 与 WPF(桌面)或 Silverlight(Web),但我强烈推荐有偏见 - 即我爱死他们了:)

It also depends on if/what library/ORM (Object-Relational Mapper) you use. When using a (good) ORM, the DAL should be a very remote concern, because it is almost completely hidden by the ORM; however, best practices dictate that even then, for medium to large size applications, you should introduce another layer between the BOL and ORM, usually DTO (Data Transfer Objects). DTOs can also be used without an ORM, as they are just dumb objects defined in a separate library, and the DAL can be responsible for persisting them (transforming them from/to database structures), while the BOL can query the DAL and receive those objects.

Decoupling the layers can be achieved in a variety of ways, most commonly through interfaces and/or MEF or another DI/IOC framework. Any such technique achieves more than sufficient decoupling if used effectively.

Also, depending on the technology used, as Sisyphus said, one of the layered architectural patterns will help separate concerns nicely: MVC, MVP, MVVM etc. I personally recommend MVVM with WPF (desktop) or Silverlight (web) but I'm highly biased - i.e. I love both of them to death :)

紅太極 2024-09-26 17:14:44

“正确”的方法将根据业务需求而变化。老实说,在很多项目中,我觉得旧式 ado 记录集比现在的许多 ORM 花费的开发时间更少并且更容易维护。花一些时间来确定您的需求是什么,并记住开发时间和可维护性也是应该适当权衡的设计目标。

The 'correct' approach is going to vary depending on business needs. To be honest, there are many projects where I feel the old style ado recordsets incurred less development time and were easier to maintain than many of the ORM's out now. Take some time to identify what your needs are, and remember that development time and maintainability are design goals that should be properly weighed as well.

三五鸿雁 2024-09-26 17:14:42

DAL 需要对 BOL 的引用,以便它可以填充对象。您不希望有任何从 BOL 回到 DAL 的引用或耦合 - 这样做会导致您的 BOL 耦合到特定的数据库实现。当你想到这一点时,这是有道理的。您的 DAL 了解业务对象的详细信息,直至属性级别以及如何从数据库检索其数据 - 当然,DAL 本质上与 BOL 紧密耦合。所以这样的参考就可以了。如果你想一想,另一边是什么?数据库。从对象数据到数据库的“紧密耦合”?是的,它非常紧。这个概念甚至没有多大意义。

这是您需要解耦的所有其他方向。因此,是的,只要没有从 DAL 到 BOL 的直接耦合,您就可以随心所欲地更改数据平台。

在这种情况下,为 BO 创建接口并将其传递给 DAL 没有多大意义。然而,有时您可能需要走另一条路。通常,业务对象不必知道它们是如何创建或持久化的。

例如,在实践中,即使对于大多数 ORM,创建完全没有任何类型的持久性工件的业务层也可能变得非常困难,有时实际上是不可能的。因此,有时您会遇到一些难以解决的问题,并且您可能会发现严格避免在 BO 中拥有任何数据知识会导致您变得过于复杂,这会降低而不是增加价值。

如果您觉得没有更好的方法并且需要从 BOL 中保留某些内容,请创建一个简单的接口,以便可以将 DAL 功能传递到 BOL 中。这样您至少仍然可以保持 BOL 与特定数据库实现分离。

另外,虽然这是很多额外的工作,但除非这是一个非常简单的一次性应用程序,否则我强烈建议您在 UI 和 BOL 之间添加另一层。 MVP(模型-视图-演示者)模式是一种通用设计模式,用于减少核心应用程序和 UI 之间的耦合。 Presenter 有很多变体,不要太关注具体细节,如果您从未使用过,请从简单的 MVP 开始。

这些模式并没有那么难,只是 UI 本身太混乱了,你可能需要至少几次主要的迭代/应用程序才能感觉到你随时编写的代码正在系统地、有条理地解耦用户界面。只要继续努力,开始掌握一系列技术,不要因为你还没有真正实现清晰的分离而纠结。你学到的任何东西以及你能做的任何事情,甚至有助于在 UI 上创建一个明确定义的边界,都是朝着正确方向迈出的一大步。

The DAL needs a reference to the BOL so that it can populate the objects. What you do not want to have is any reference or coupling from the BOL back to the DAL - doing so causes your BOL to be coupled to a specific database implementation. When you think about it this makes sense. Your DAL knows details about the business objects down to the level of properties and how to retrieve their data from the database - of course the DAL is inherently tightly coupled to the BOL. So the reference that way is fine. And if you think about it what is on the other side? The database. "Tightly coupling" going from your object data to your database? Yeah, it is pretty darn tight. The concept is not very meaningful even.

It is all the other direction where you need to decouple. So yes as long as there is no direct coupling from the DAL into the BOL you can change your data platform anyway you want.

Not much point in creating interfaces for BOs and passing them to DAL in this scenario. You might sometimes need to go the other way however. As a rule business objects should not have to know anything about how they are either created or persisted.

In practice even with most ORMs, for example, creating a business layer completely free of any sort of persistence artifacts can become very difficult, sometimes effectively not possible. So occasionally you have something that is just too difficult to work around though, and you might find that strictly avoiding having any data knowledge in BOs is leading you to over complexity that is degrading rather than adding value.

If you feel like there is no better way and you need to have something persisted from within the BOL, create a simple interface so that the DAL functionality can be passed into the BOL. That way you can still keep the BOL decoupled from the specific database implementation at least.

Also, although it is a lot of additional work, unless this is a very simple throwaway app, I strongly recommend that you also add another layer between the UI and the BOL. The MVP (Model-View-Presenter) pattern is a general purpose design pattern for reducing coupling between the core app and the UI. There are a lot of variants on presenters, don't get too caught up in the specific details, just start off with the simple MVP if you have never used it.

The patterns is not that hard, it is just that UI itself is so messy that it may take you at least a couple of major iterations / applications before you feel like the code you are writing at any time is systematically and methodically working to decouple the UI. Just keep working at it, start to acquire an arsenal of techniques, and don't get hung up on the fact that you really have not achieved a sharp clean separation yet. Anything and everything you learn and can do that even contributes a little to creating creating a well defined boundary at the UI is a big step in the right direction.

淡淡的优雅 2024-09-26 17:14:40

看看 SubSonic http://subsonicproject.com/ 它为您完成了大部分数据访问繁琐的工作它比大多数 ORM 更容易

take a look at SubSonic http://subsonicproject.com/ it does most of the data access tedious work for you and it's easier than most ORMs out there

走过海棠暮 2024-09-26 17:14:36

我这样做的方式是 BO 期望一个 DataReader 或 DataContext 或从 DAL 返回的任何内容,而不是实际形成的对象。然后 BO 层的工作就是从返回的对象中获取并填充自身。 DAL 不会返回已完成的 BO。要记住的主要事情是,更改 BO 层中的某些内容不应导致 DAL 层出现问题,但更改 DAL 层中的某些内容可能会导致 BO 层出现问题。

执行的操作的简短示例

我通常在 BO 层中

FillData(){
   DataReader dr = DataLayer.GetData("SomePropertyForAStoreProcedure");
   If dr.Read(){
    Property1 = dr.GetValue("Property1");
    //So on and so forth
   }
}

在 DAL 中

DataReader GetData(String SPProperty){

}

The way the I do this is the BO expects a DataReader or a DataContext or whatever back from the DAL, not the actual formed object. It is then the job of the BO layer to take and fill itself from the object that came back. The DAL isn't returning back a completed BO. The main thing to remember is that changing something in the BO layer shouldn't cause issues for the DAL layer, but changing something in the DAL layer could cause issues for the BO layer.

A short example of what I typically do

In the BO layer

FillData(){
   DataReader dr = DataLayer.GetData("SomePropertyForAStoreProcedure");
   If dr.Read(){
    Property1 = dr.GetValue("Property1");
    //So on and so forth
   }
}

In the DAL

DataReader GetData(String SPProperty){

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文