我有以下项目结构,这些都是单独的项目,我被告知要这样做,所以不是我的选择。
CORE
--Self Explanitory
DATA
--Contains EF 4.1 EDMX, POCO's Generic Repository Interface
DATAMapping
--Contains Generic Repository
Services
-- Contains nothing at the moment
MVC 3 Application
-- Self Explanitory
这是我的问题。我一直在读到,最好的做法是控制 Controller 的节食,并且模型/ View 模型应该是愚蠢的,因此引入了我的项目结构的服务层部分。现在的实际问题是;这是一个好方法还是我为自己创造了太多工作?
因此,如果我想说对产品或类别或任何其他实体有一些 CRUD 操作,存储库应该从服务层/业务逻辑层实例化?
请输入一些信息?
最佳答案
就我个人而言,我的服务层仅引用用于 CRUD 操作的通用和抽象存储库。例如,服务层构造函数可能如下所示:
public class MyService: IMyService
{
private readonly IFooRepository _fooRepo;
private readonly IBarRepository _barRepo;
public MyService(IFooRepository fooRepo, IBarRepository barRepo)
{
_fooRepo = fooRepo;
_barRepo = barRepo;
}
public OutputModel SomeBusinessMethod(InputModel input)
{
// ... use CRUD methods on _fooRepo and _barRepo to define a business operation
}
}
Controller 将简单地将IMyService
放入其构造函数中并使用业务操作。
然后一切都将通过您选择的依赖注入(inject)框架进行连接。
关于asp.net-mvc-3 - MVC-3 项目结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5523959/