visual-studio 如何处理循环引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3719730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:27:14  来源:igfitidea点击:

How to deal with circular references?

.netvisual-studioarchitecturecircular-reference

提问by Eduardo

If I have those two projects:

如果我有这两个项目:

MyCompany.ERP.Billing
MyCompany.ERP.Financial

Billingasks/sends information to Financialand 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?

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

采纳答案by devnull

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

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

This only allows you to passobjects 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.

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

I would extract the business logic that needs to share the data back and forth between Billingand Financialinto 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.

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

回答by PhilB

Having too large of a project shouldn't be an issue. You can keep your code structured with namespaces and different folders for the source code. In this case circular references are no longer an issue.

项目太大应该不是问题。您可以使用命名空间和源代码的不同文件夹来保持代码的结构。在这种情况下,循环引用不再是问题。

回答by Basic

The answer mentioning interfaces is correct - but if you need to be able to create both types from both projects, you'll either need to farm a factory out into yet another project (which would also reference the interfaces project but could be referenced by both of your main projects) or change the structure you're using significantly.

提到接口的答案是正确的 - 但是如果您需要能够从两个项目中创建这两种类型,您要么需要将工厂转移到另一个项目中(该项目也将引用接口项目,但两者都可以引用)您的主要项目)或显着更改您正在使用的结构。

Something like this should work:

这样的事情应该工作:

Finance: References Billing, Interfaces, Factory
Billing: References Finance, Interfaces, Factory
Factory: References Interfaces

Factory would have a BillingFactory.CreateInstance() As Interfaces.IBillingand also the abstract Billing class which implement Interfaces.IBilling.

工厂将有一个BillingFactory.CreateInstance() As Interfaces.IBilling和实现 Interfaces.IBilling 的抽象 Billing 类。

The only issue I can see is if you need to do something clever when instantiating an object and don't want that logic to end up in a separate project - but as you haven't mentioned any clever logic to instantiate, this should be sufficient

我能看到的唯一问题是,如果您在实例化对象时需要做一些聪明的事情并且不希望该逻辑最终出现在单独的项目中 - 但正如您没有提到任何要实例化的聪明逻辑,这应该就足够了

回答by TTT

This solutioncould end up as a workaround for the circular reference problem. Basically you use #if logic around the code that doesn't compile unless the reference exists, and you use conditional compilation in the project file to define a variable only if the needed assembly exists. As a result, on first download from source, or after a solution clean, you must compile twice. Subsequent builds/rebuilds only require 1 build as normal. The nice thing about this is you never have to manually comment/uncomment #define statements.

此解决方案最终可能会成为循环引用问题的解决方法。基本上,除非引用存在,否则不会编译的代码周围使用#if 逻辑,并且仅当需要的程序集存在时才在项目文件中使用条件编译来定义变量。因此,在第一次从源代码下载时,或在解决方案清理后,您必须编译两次。后续的构建/重建只需要 1 次正常构建。这样做的好处是您永远不必手动注释/取消注释 #define 语句。