圆依赖的最佳实践 [英] Circular-dependency best practice
问题描述
我目前正在写一个web刮刀来检索来自互联网的信息。简化它看起来像这样。
I'm currently writing a web scraper which retrieves information from the internet. Simplified it looks like this.
数据访问项目
- 对象来获取原始数据
- 对象的原始数据解析为对象(!!)
- 的实体解析器回报。
现在,我创建实际的解析器,我要去使用它是这样的:
Now, I'm creating the actual parser, and I'm going to use it like this:
using Application.DataAccess;
using Application.DataAccess.Entities;
namespace Application{
public class TestScraper{
public static ScrapeIt()
{
var source = DataAcces.Retriever.Retrieve("http://example.com");
DataAccess.Entities.Entity entity = DataAccess.Parser.Parse(source);
//Do stuf with source here.
}
}
}
正如你所看到的,分析器返回一个实体。然而,这是在数据访问的命名空间,但是,它没有任何意义......一个汇入作业一圈,我不真的知道如何解决这个问题。嗯,我能想出一些事情,比如创建这些层之间的另一层。
As you can see, the Parser returns a Entity. However this is in the DataAccess namespace, yet, it makes no sense... it´s a circle, and I don´t really know how to fix this. Well I could come up with a few things, like creating another layer between those layers.
但是我只是想知道,你会如何解决这个问题。什么是一个很好的(或最佳做法)。
推荐答案
您可以修复由循环引用分解出两个类都引用到一个新的类,然后老班指的是新类的东西。
You can fix a circular reference by factoring out the things that both classes refer to into a new class, and then the old classes both refer to the new class.
所以你的情况,你可以移动的实体进行数据访问和进入也许一个新的实体的命名空间,通过双方数据访问和应用程序使用。
So in your case you could move the entities out of DataAccess and into perhaps a new Entities namespace, used by both DataAccess and Application.
这样做你开始使用
A <--> D
和
and end up with
A --> E
D --> E
这篇关于圆依赖的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!