如何处理循环引用? [英] How to deal with circular references?

查看:42
本文介绍了如何处理循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这两个项目:

MyCompany.ERP.Billing
MyCompany.ERP.Financial

账单财务询问/发送信息,反之亦然.两者都太大了,所以我不想把它们放在一个项目中.Visual Studio 不允许循环引用.你会怎么处理?

Billing asks/sends information to Financial and vice-versa. Both are too big so I don't want to put them in a single project. Visual Studio doesn't allow circular references. How would you deal with that?

推荐答案

从您的类中提取接口,并将它们放入从 BillingFinancial 项目引用的核心项目中.然后,您可以使用这些接口在程序集之间共享数据.

Extract interfaces from your classes and put them into a core project referenced from both Billing and Financial projects. You can then use those interfaces to share data between assemblies.

这仅允许您在这两个程序集之间传递对象,但您不能从另一个程序集创建对象,因为您实际上没有一个引用.如果您希望能够创建对象,您需要一个工厂,在这两个项目之外,处理对象创建.

This only allows you to pass objects between those 2 assemblies, but you can't create objects from the other since you don't actually have a reference to begin with. If you want to be able to create objects you need a factory, external to those 2 projects, that handles object creation.

我会将需要在 BillingFinancial 之间来回共享数据的业务逻辑提取到另一个项目中.这将使事情变得容易得多,并且可以使您免于诉诸各种使可维护性成为噩梦的技巧.

I would extract the business logic that needs to share the data back and forth between Billing and Financial into another project. This would make things a lot easier and would save you from resorting to all sort of tricks that make maintainability a nightmare.

这篇关于如何处理循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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