Should a service call service OR How to do multiple stuff in a service?

Hello everyone,

i currently use a "controller calls service" approach to seperate my code. (I do not currently use a repository pattern or better explained i have repository and service all in one.)

It took me a while to find a good way on how to access the service but in the meantime I like the approach to pass a create-view-model to the service like this. 

public ActionResult Create(MembershipCreateViewModel membershipCreateViewModel)
{
     Membership membership = MembershipService.Create(membershipCreateViewModel);
     ...

This works quite well but sometimes I have more complicated forms where there is more than "one thing" to do. As an example think of a form where the user can book a course. The form lets the user select if he wants to either choose the participant (via
select) or create one instantly (a bunch of fields the come to display). Addionally there are some other course related fields. 

The CreateViewModel looks like this:

public int CourseId { getset; }
public int PriceId { getset; }
 
public bool CreateCustomer { getset; }
      
//If CreateCustomer = false
public int CustomerId { getset; }
 
//If CreateCustomer = true
public string CustomerFirstname { getset; }
public string CustomerLastname { getset; }
public string CustomerStreet { getset; }

So now to the question :)

A) One Service fits all

I could pass this createviewmodel to a BookingService and the BookingService could then create the Booking AND if necessary create the customer. But this would violate integrity because the creation of a customer belongs to the CustomerService. This would
lead me to the second option which is:

B) One Service fits all with the help of another service

The BookingService could call the CustomerService. The question here is. Should one service talk to another? I feel this would be a bad design (and one try ended in endless loop of the DI)

C) Controller calls both services

The Controller first passes the CreateViewModel to the CustomerService and then to the BookingService?

What do you thinks? Whats best?

Hi,

I would say you viewmodel creation should be separated from service. Why service is need to create the viewmodel when ur controller is capable of doing it.?

 

 

Hi AndiRudi,

For that scenario, since CreateViewModel and CustomerService are independent, I think the better way is
Controller calls both Services.

On the other hand, in my opinion, the create-view-model is not the good design.

Best Regards

Starain

Leave a Reply