我正试图绕过ServiceStack
并利用它来公开 RESTful 服务。
我目前正在使用 MVC/Service/Repository/UnitOfWork 类型模式,其中获取客户的基本操作可能如下所示:
MVC Controller Action --> Service Method --> Repository --> SQL Server
我的问题是:
- 我的 SS 服务会返回什么?域对象?还是我返回一个包含客户集合的 DTO?如果是这样,客户是什么?领域对象或视图模型或??
- SS 服务是否应该取代我的服务层?
- 我在这里采取了完全错误的方法吗?
我想我有点困惑如何让所有这些并排生活。
域对象
public class Customer
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
}
查看模型
public class CustomerViewModel
{
public int Id {get;set;}
public string FirstName {get;set;}
....
}
控制器
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);
}
}
服务
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));
}
}