How to deal with circular references?
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?
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.
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.
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
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.
精彩评论