So ran into an issue with circular dependency that looked like this:
public class A { public List<B> Bs { get; private set;} public void AddB(B b) { Bs.Add(b); } public void RemoveB(B b) { Bs.Remove(b); } public int DoWork() { // Need details of B // Probably need to inject a service that gets B details... } } public class B { public A A1 { get; private set; } public A A2 { get; private set; } public B(A a1, A a2) { A1 = a1; A2 = a2; } public void ChangeA1(A a1) { A1.RemoveB(this); A1 = a1; A1.AddB(this); } public void ChangeA2(A a2) { A2.RemoveB(this); A2 = a2; A2.AddB(this); } } This creates a huge circular dependency and causes the entire object graph to be read into memory (multiple times). So my question is can/should this circular dependency be broken physically and still be fine logically?
For example, using IDs of the As and Bs? Obviously they will be circular dependent on each other logically, but physically it won't be required for the entire object graph to be read into memory.
I'm thinking this still isn't smart because my A class would need details of B to perform it's work so would need some type of service/repository injected. I assume the better refactoring would be to introduce a 3rd object to break the circular dependency.
I'm trying to wrap my head around solving this with a 3rd object or using value objects, but a use-case would be to delete a B from A (so I need to know which B to delete (i.e. have an identity)).
This is the general concept of what I'm trying to implement. It's for a double-entry accounting system that looks like this:
To clarify, A would be an Account and B would be a transaction.
So the user can view transactions from the account view and then delete a transaction or remove a different split. So it seems that a circular dependency is needed for this navigation ability.
