0

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.

0

What about creating a tree where each Node contains the entity/entityId and the corresponding service. Node can acsses its parent and children.

Write the cascading deletion: before deleting the node, call delete In the service. The delete in the service will only delete from the DB.

Didn't understand if there is another reason to notify the parent about the deletion of a child. If so add it in the deletion algorithm.

0

It might be worth separating the tree out into it's data and the behaviors that are performed on that data.

With the behavior separated it will be possible to delete parent, siblings and child nodes from the tree without each individual node needing to know and recognize it's place and relationship to other nodes in the tree.

There is a design pattern called the Visitor pattern that allows behaviors to be applied to trees of nodes. The idea is a visitor (implemented as an interface so it can be any sort of visitors) visits every node and through that decisions can be made about whether the behaviors of the visitor apply. In this case a DeleteGuardroomVistor could take care of deleting relevant nodes.

This might be an alternative approach.

If you need more information on this leave a comment and I will expand. Happy coding.

0

I'm not familiar with C# specifically but it looks like an Observer design pattern would be useful here (or also Signals and Slots).

In your scenario, each of your Services would provide a way to register a generic handler to be triggered when an object is deleted. The CustomerService would register as an observer of SiteService, SiteService would register as an observer of LocationService, and LocationService would register as an observer of GuardroomService.

Then when a Guardroom is deleted, GuardroomsService would notify all the observers of the guardroom that was deleted. The LocationService would receive it (as typically a call into a callback function) and could then check if the location now needs to be deleted. If so then the LocationService deletes the location and notifies all of its observers, and so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.