interface IGenericRepo<T> where T : class
{
IEnumerable<T> GetAll();
T GetById(object id);
void Insert(T obj);
void Update(T obj);
void Delete(T obj);
void Save();
}
interface ICustRepo : IGenericRepo<Cust>
{
IEnumerable<Cust> GetBadCust();
IEnumerable<Cust> GetGoodCust();
}
public class CustRepo : ICustRepo<Cust>
{
//implement method here
}
然后在我的控制器中:
public class CustController
{
private ICustRepo _custRepo;
public CustController(ICustRepo custRepo)
{
_custRepo = custRepo;
}
public ActionResult Index()
{
var model = _custRepo.GetAll();
return View(model);
}
public ActionResult BadCust()
{
var model = _custRepo.GetBadCust();
return View(model);
}
}
基本上我的架构设计如下:
View <-> Controller -> Repo -> EF -> SQL Server
但我看到很多人这样做:
View <-> Controller -> Service -> Repo -> EF -> SQL Server
所以我的问题是:
为什么和何时需要
service layer
? 难道这不是另外增加了一个不必要的层吗?因为每个非通用的方法已经实现在ICustRepo
中了。服务层应该返回
DTO
还是我的ViewModel
?服务层是否应该与我的存储库进行1:1映射?
我已经找了几天,但没有找到令我满意的答案。
任何帮助都将不胜感激,不好意思我的英语不是很好。
谢谢。
更新:
我已经阅读过这篇文章。我已经知道这两者之间的区别,但我想了解为什么和目的。因此,这并不能回答我的问题。
EF -> 数据库
而不是EF -> SQL Server
,但我们明白您的意思。 - carloswm85