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

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

问题描述

我遇到了一点架构问题.在我的项目中,我有一个业务逻辑层 (BLL),其中包含我所有的业务规则、模型和接口的 OO API.每个对象都有像 getById 这样的静态方法,它们返回所述对象的一个​​实例.每个对象还具有保存和删除等方法.这是非常简单的面向对象代码.

现在我有一个数据访问层 (DAL),包含在一个单独的命名空间中,对于每个 BLL 对象,我都有一个数据类或存储库",它执行 getById 和保存命令.所以在某种程度上,BLL save 和 getById 方法是围绕 DataClass 方法的一个薄层.

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

为了让 DataClasses 返回 BLL 对象,它们需要知道 BLL.所以现在我们有:

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

DataFactory 只返回实现接口的对象,因此我可以隐藏OracleNewsItemRepository"等实现细节.

但是现在对于自从我开始面向对象编程以来一直困扰着我的事情.在我当前的解决方案中,BLL 和 DAL 都需要相互了解.这是一个循环依赖,最好避免循环依赖.此外,我只想公开接口(和我的 DataFactory)而不是我的类.这可以通过将 DAL 层放置在单独的程序集中来完成.这是有道理的.但是,Visual Studio 不允许两个程序集相互引用.关于此的另一个问题:C# 内部访问修饰符

不知怎么的,我觉得我的整个数据访问模式都错了.感觉就像我正在将 ActiveRecord 模式与 DataMappers 之类的其他东西进行卷积.我在 Martin Fowler 的网站上花了很多时间,但这些模式的描述非常通用,并用非常抽象的 UML 图进行了说明.

他们没有解决我的问题.也许我有点肛门,没有完美的数据访问模式"这样的东西.我现在所做的似乎并没有错.但是我现在做事的方式似乎不对......

有什么想法吗?

解决方案

我认为您的数据访问模式很好.您没有做的是将您的 BLL 耦合到 OracleDAL.您正在耦合到 DAL 接口.一定程度的耦合是绝对需要的,否则您将永远无法完成任何事情.

我假设您的 DataFactory 和 INewsItemRepository 类存在于您的 DAL 层之外.以下是我的解决方案组织方式的示例.我不使用 ActiveRecord,所以这可能不适合你.

<前>核心(项目)领域商业实体数据存储库接口**您的数据工厂**OracleData(项目)数据Oracle 信息库实现SqlData(项目)数据Sql 存储库实现用户界面(项目)

希望这会有所帮助.

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.

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);
}

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

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

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

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: C# internal access modifiers

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…

Any ideas?

解决方案

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.

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)

Hope this helps.

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

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