Lets say I have a set of conditional requirements that can be nested inside of each other in a tree like structure, but all of the requirements need to communicate with different subsystems to find out if they are fulfilled. For instance one requirement might check if a certain inventory item exists, one might check an object for a certain attribute, one might check an event system to see if a certain event has fired etc. How could you model this so that you can traverse the tree as a part-whole hierarchy but also allow each node to communicate with the subsystem it needs to?
Asked
Active
Viewed 69 times
1 Answers
0
It seems like you should model the aggregation and later use dependency injection (see What is dependency injection?) to require whichever object that should fulfill the desired functionalities.
Each node would be like a domain model.
Something like this:
public class A
{
// You inject some service IServiceX
public A(IServiceX serviceX)
{
ServiceX = serviceX;
}
private IServiceX ServiceX { get; }
// B is associated to A
public B B { get; set; }
// So you can do some stuff calling the whole ServiceX
public void DoStuff()
{
ServiceX.DoWhatever();
}
}
public class B
{
public B(IServiceY serviceY)
{
ServiceY = serviceY;
}
private IServiceY ServiceY { get; }
// C is associated to B
public C C { get; set; }
public void DoStuff()
{
ServiceY.DoWhatever();
}
}
public class C
{
public C(IServiceZ serviceZ)
{
IServiceZ = serviceZ;
}
private IServiceZ IServiceZ { get; }
public void DoStuff()
{
IServiceZ.DoWhatever();
}
}
Obviously, you could inject more than a service into some of these aggregates. Since each aggregate gets associated with other one, you should be able to traverse the tree too.

Community
- 1
- 1

Matías Fidemraizer
- 63,804
- 18
- 124
- 206
-
I meant the coupling of the subsystems to the node not the nodes in the tree. I think dependency injection looks promising. Does dependency injection allow for injecting an object that has already been instantiated or will it inject a new instance for each constructor or property? – Adam1980 Feb 25 '17 at 16:32
-
@Adam1980 You can inject existing instances. Caslte Windsor can do it, I don't know if others also own this capability. For example `container.Register(Component.For
().Instance(instance))` – Matías Fidemraizer Feb 25 '17 at 16:52 -
@Adam1980 BTW probably you don't want to create the instance yourself, but use a bound lifestyle like Castle Windsor already offers too. See this https://github.com/castleproject/Windsor/blob/master/docs/lifestyles.md#bound-to-nearest – Matías Fidemraizer Feb 25 '17 at 16:56