Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot circular dependency design problem

Yes, this problem again...

I have designed a batch system which calls notify method of ActionService and within notify method, depends on how it is called, there is a call to create method of batch service.

Example code blocks looks like this;

@Service
public class BatchService{
  private final BatchRepository batchRepository;
  private final ActionService actionService;

  public Batch create(Batch batch){
     return batchRepository.save(batch);
  }

  public void triggerNotifiers(){
     ...
     actionService.notify(...);
     ...
  }
} 


@Service
public class ActionService{
  private final ActionRepository actionRepository;
  private final BatchService batchService;

  public void notify(...){
     ...
     if(/*some special cases*/)
         batchService.create(...);
     ...
  }
} 

The functionality of batch service is to call notify method of Action service. Action service can create future notifiers depends on the incoming notification data.

So they are pretty dependent to each other.

Do you think is there a better way/logic/design to do this?

like image 444
ibrahim demir Avatar asked Jan 21 '26 09:01

ibrahim demir


1 Answers

While I was writing the question, I figured out a way to do it:

@Component
public class Batch{
  private final BatchService batchService;
  private final ActionService actionService;

  public void triggerNotifiers(){
     ...
     batchService.findProperBatch(...);
     actionService.notify(...);
     ...
  }
} 

@Service
public class BatchService{
  private final BatchRepository batchRepository;

  public Batch create(Batch batch){
     return batchRepository.save(batch);
  }

  public Batch findProperBatch(...){
     ...
     return batch;
  }

} 

@Service
public class ActionService{
  private final ActionRepository actionRepository;
  private final BatchService batchService;

  public void notify(...){
     ...
     if(/*some special cases*/)
         batchService.create(...);
     ...
  }
} 

Please dont hesitate to share your ideas, if you have better/alternative solutions.

like image 197
ibrahim demir Avatar answered Jan 22 '26 22:01

ibrahim demir