Ivan Debono
Ivan Debono

Reputation: 941

Should a service access other services or just repositories?

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?

Upvotes: 2

Views: 646

Answers (1)

Mikko Viitala
Mikko Viitala

Reputation: 8404

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);
    }
}

Upvotes: 1

Related Questions