I am working on a project in which I have a tree with 4 layers and the hierarchy is like this. Customer -> Site -> Location -> Guardroom
In DB each entity has its own table and the child knows its parent's Id.
public class Guardroom{
public int Id {get; set;}
public string Name {get; set;}
public int LocationId {get; set;}
}
A rule is that guardroom should be the only leaf. What I mean is that
Customer must have at least one child (site)
|
|--> Site must have at least one child (location)
|
|--> Location must have at least one child (guardroom)
My challenge is that if I delete a guardroom I must delete the location only if the guardroom has no siblings and the grandparent if location has no siblings and so on. In other words, If I delete the guardroom I must delete the nodes above in cascade only if the node to delete has only 1 child.
I do not want to have a highly coupled structure so I do not want the guardroom to know about the location. I was thinking in using events but I am not sure what patter or how to implement it.
My infrastructure looks like this.
I implemented dependency injection and Domain Driven Design. I have a service for each layer in the tree: i.e.
public class GuardroomService: IGuardroomService {
private readonly IGuardroomService _guardroomService;
//Here dependency is injected.
public GuardroomService(IGuardroomDataAccess guardroomService){
_guardroomService = guardroomService;
}
public void Delete(int guardroomId){
//Here I want to raise an event to notify parent (location) and take further action.
}
//...
//The remaining CRUD operations and functions.
}
What patter/structure should I implement to let the other services I am deleting a child without allowing the same service to know about the other services.
Any help will be much appreciated. Thank you in advance.
must
state it very clear that they MUST be treated as a whole