Should a service access other services or just repositories?

591 Views Asked by At

As an example, the customer service has a method that cancels a particular order for that customer:

public class CustomerService : ICustomerService<Customer>
{
     public void CancelOrder(int orderNo) 
     {
         //customerRepository here...
     }
}

Then, another service used for batch processing can also cancel multiple orders:

public class BatchService : IBatchService
{
     public void CancelOrders(params int[] orderNos) 
     {
         //What here?
     }
}

The code for cancelling the customer's order is located in the CustomerService. Should the BatchService call CustomerService.CancelOrder or should it just use the customer repository too?

1

There are 1 best solutions below

0
On BEST ANSWER

I wouldn't write the cancelling logic twice. Where you have it is up to you, but given your current structure, I'd inject ICustomerService into BatchService. So class could look something like

public class BatchService : IBatchService
{
    public ICustomerService<Customer> CustService { get; set; }

    public void CancelOrders(params int[] orderNos)
    {
        foreach (int orderNo in orderNos)
            CustService.CancelOrder(orderNo);
    }
}