Saturday, 14 September 2013

How to make ServiceStack work with existing MVC/Service/Repository pattern

How to make ServiceStack work with existing MVC/Service/Repository pattern

I am trying to wrap my head around ServiceStack and utilizing it to expose
RESTful services.
I am currently using a MVC/Service/Repository/UnitOfWork type pattern
where a basic operation to get customers might look like this:
MVC Controller Action --> Service Method --> Repository --> SQL Server
My questions are:
What do my SS services return? Domain objects? Or do I return a DTO that
has a collection of customers? And if so, what are the customers? Domain
objects or view models or ??
Should the SS services replace my service layer?
Am I taking the totally wrong approach here?
I guess I am a little confused how to make all of this live side-by-side.
Domain Object
public class Customer
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
}
View Model
public class CustomerViewModel
{
public int Id {get;set;}
public string FirstName {get;set;}
....
}
Controller
public class CustomersController : Controller
{
ICustomerService customerService;
public CustomersController(ICustomerService customerService)
{
this.customerService = customerService;
}
public ActionResult Search(SearchViewModel model)
{
var model = new CustomersViewModel() {
Customers =
customerService.GetCustomersByLastName(model.LastName); //
AutoMap these domain objects to a view model here
};
return View(model);
}
}
Service
public class CustomerService : ICustomerService
{
IRepository<Customer> customerRepo;
public CustomerService(IRepository<Customer> customerRepo)
{
this.customerRepo = customerRepo;
}
public IEnumerable<Customer> GetCustomersByLastName(string lastName)
{
return customerRepo.Query().Where(x =>
x.LastName.StartsWith(lastName));
}
}

No comments:

Post a Comment