业务逻辑层和数据访问层:循环依赖 [英] Business Logic Layer and Data Access layer: circular dependency

查看:175
本文介绍了业务逻辑层和数据访问层:循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小建筑的问题。在我的项目我有一个包含所有的业务规则,模型和面向对象的API接口业务逻辑层(BLL)。每个对象具有静态方法一样getById返回所述对象的一个​​实例。每个对象也有喜欢保存和删除的方法。这是非常简单的OO code。

I’m having a little Architecture problem. In my project I have a Business Logic Layer (BLL) that contains all my business rules, models and OO API for the interface. Each object has static methods like getById that return an instance of said object. Each object also has methods like save and, delete. This is very straightforward OO code.

现在我有一个数据访问层(DAL),包含在一个单独的命名空间,每个BLL对象我有一个数据类或执行getById和保存命令信息库。因此,在某种程度上,对BLL保存和getById方法都是围绕着数据类方法的薄层。

Now I have a DataAccess layer (DAL), contained in a separate namespace, for each BLL object I have a DataClass or "Repository" which executes the getById and save commands. So in a way, the BLL save and getById methods are a thin layer around the DataClass methods.

public static NewsItem GetByID(int id)
{
       return DataFactory.GetNewsItemRepository().GetNewsItemById(id);
}

为了使数据类返回BLL对象,他们需要知道BLL。所以现在我们有:

In order for the DataClasses to return BLL objects, they need to know the BLL. so now we have:

GUI ---> BLL< ----> DAL

GUI ---> BLL <---->DAL

本的DataFactory只返回实现接口的对象,所以我可以隐藏,如OracleNewsItemRepository的实施细则。

The DataFactory only returns objects that implement an Interface, so I can hide implementation details like "OracleNewsItemRepository".

但现在已被窃听我自从我开始面向对象编程的东西。在我目前的解决方案,无论是BLL和DAL需要了解对方。这是一个循环依赖,这是为了避免循环依赖的最佳做法。此外,我只想公开的接口(和我的DataFactory),而不是我的课。这可以通过将DAL层在单独的程序来完成。这将使意义。但是,Visual Studio不允许两个程序集引用海誓山盟。这个另一个问题:<一href=\"http://stackoverflow.com/questions/457044/c-internal-access-modifiers\">http://stackoverflow.com/questions/457044/c-internal-access-modifiers

But now for the thing that has been bugging me ever since I started Object Oriented programming. In my current solution, both BLL and the DAL need to know each other. This is a Circular Dependency, and it is best practice to avoid circular dependencies. Also I only want to expose the interfaces (and my DataFactory) and not my classes. This can be done by placing the DAL layer in a separate Assembly. Which would make sense. However, Visual Studio does not allow two Assemblies to refer eachother. Another question about this: http://stackoverflow.com/questions/457044/c-internal-access-modifiers

不知怎的,我觉得我得到了我的整个数据访问模式是错误的。这感觉就像我卷积像DataMappers其他的东西ActiveRecord的模式。我已经花了Martin Fowler的网站很多时间,但这些模式是通过一个很抽象的UML图来描述非常通用的,说明。

Somehow I think I got my whole data access pattern wrong. It feels like I am convoluting the ActiveRecord pattern with other stuff like DataMappers. I have spent a lot of time on Martin Fowler’s site, but those patterns are described very generic and are illustrated by a very abstract UML diagram.

他们不解决我的问题。也许我是有点肛门,并有一个完美的数据访问模式没有这样的事。而我现在做的似乎并不可怕的错误。但是,我现在该怎么做的事情,似乎离...

They don’t solve my problem. Maybe I’m a bit anal, and there is no such thing as a "perfect data access pattern". And what I do now doesn’t seem terribly wrong. But how I do things now, seems off…

任何想法?

推荐答案

我觉得你的数据访问模式是好的。你不这样做是你连接到BLL的OracleDAL。您连接到该DAL接口。耦合的某一位是绝对必需的,或者你永远无法得到任何事情。

I think your data access pattern is fine. What you are not doing is coupling your BLL to the OracleDAL. You are coupling to the DAL interfaces. A certain bit of coupling is absolutely required or you could never get anything done.

我假定你的DataFactory和INewsItemRepository类的DAL层之外。以下是我的解决方案是如何组织的一个例子。我不使用ActiveRecord,所以这可能不适合你完美。

I assume that your DataFactory and the INewsItemRepository classes exist outside your DAL Layer. The following is an example of how my solutions are organized. I don't use ActiveRecord, so this may not suit you perfectly.


Core (Project)
  Domain
    Business Entities
  Data
    Repository Interfaces
    **Your DataFactory**

OracleData (Project)
  Data
    Oracle Repository Implementations

SqlData (Project)
  Data
    Sql Repository Implementations

UI (Project)

希望这有助于。

这篇关于业务逻辑层和数据访问层:循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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