How to deal with circular references?

拟墨画扇 提交于 2019-12-18 03:03:30

问题


If I have those two projects:

MyCompany.ERP.Billing
MyCompany.ERP.Financial

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?


回答1:


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.

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.




回答2:


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.




回答3:


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.IBilling and also the abstract Billing class which implement Interfaces.IBilling.

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




回答4:


This solution could 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.



来源:https://stackoverflow.com/questions/3719730/how-to-deal-with-circular-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!