模型的依赖注入
我确信以前有人问过这个问题,但我很难找到在哪里。
我正在使用 Ninject 以及存储库设计模式来删除控制器的依赖项。
据我了解,这种方法的好处之一是,如果我愿意,我可以轻松地分解我的存储库和域实体并使用另一个程序集。因此,我将域实体和存储库保留在外部程序集中,并且可以从接口模拟所有依赖项。
看来,虽然我可以在大多数地方使用接口来引用我的域实体,但在模型绑定方面我必须使用对我的具体类的引用。我读过这与我理解的序列化有关,但这是避免引用域实体来创建单独模型的唯一方法吗?
我可以使用自定义模型绑定做什么?
一些背景知识:我是一位经验丰富的 ASP.net 开发人员,但对 MVC 还很陌生。
I'm sure someone has asked this before, but I'm struggling to find where.
I'm using Ninject to remove dependencies from my controllers, along with a repository design pattern.
As I understand it, one of the benefits of this approach is that I can easily whip apart my repositories and domain entities and use another assembly should I so wish. Consequently I've kept my domain entities and repositories in external assemblies and can mock all my dependencies from interfaces.
It seems that while I can use interfaces to refer to my domain entities in most places I must use references to my concrete classes when it comes to model binding. I've read that this is to do with serialization which I understand, but is the only way to avoid referring to domain entities to create separate models?
Anything I can do with Custom Model Binding?
A bit of background: I'm an experienced ASP.net developer, but new to MVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
视图模型应该是没有逻辑的普通数据容器,因此根本不应该有任何依赖关系。相反,将存储库注入到控制器中,并让它将存储库中所需的数据分配给视图模型的适当属性。
View Models should be plain data containers with no logic and therefore shouldn't have any dependencies at all. Instead inject the repositories to your controller and have it assign the required data from the repository to the appropriate property of your view model.
使用依赖注入框架的主要优点是IoC(控制反转):
所以人们通常做的是通过他们的接口注入存储库,例如
在测试过程中,这允许轻松注入我的模拟存储库,该存储库准确地返回我想要测试的值。
注入域实体没有多大意义,因为它们与特定类/控制器中的功能联系更紧密,因此进一步抽象它们只是一种开销,而不是一种好处。相反,如果您想将实际实体模型与控制器解耦,您可以查看 MVVM 模式,创建专门的“ViewModels”。
只需考虑控制器的可测试性:“我想要模拟什么来单元测试它?”
我不会在这里包含域实体,因为它们通常只是一个数据容器。
The major advantage of using a dependency injection framework is IoC (Inversion of Control):
So what one usually does is to inject repositories through their interfaces like
During testing this allows to easily inject my mock repository which returns exactly those values I want to test.
Injecting domain entities doesn't make that much sense as they are more tightly linked with the functionality in the specific class/controller and thus abstracting them further would just be an overhead rather than being a benefit. Instead, if you want to decouple your actual entity model from the controller you might take a look at the MVVM pattern, creating specialized "ViewModels".
Just think in terms of testability of your controller: "What would I want to mock out to unit test it?"
I wouldn't include domain entities here as they're normally just a data container.
更多细节会有所帮助。也许有点代码?
首先,您应该避免将依赖项注入域实体,而是使用域服务。
更多信息请参见此处。
编辑001:
我认为我们应该澄清我们的术语。
领域层包含所有领域实体,例如产品、类别等。
然后是包含存储库的数据层,用于补充域实体,然后是包含与数据层对话的应用程序服务的服务层。
最后,您将拥有一个包含视图和控制器的表示层。控制器与应用程序服务层对话。因此,产品控制器与目录服务(例如 GetProductBySku)对话。 CatalogueService 将在其构造函数中注入一个或多个存储库(IProductRepository、ICategoryRepository 等)。
在 asp.net mvc 中也很常见有 ViewModel。将 ViewModel 放入您的应用程序服务层。
所以我不确定当你说“模型”和“领域实体”时你的意思是什么,但我希望这能澄清术语。
Some more details would help. A bit of code perhaps?
To start with, you should avoid injecting dependencies into domain entities, but rather use domain services.
Some more info here.
Edit 001:
I think we should clarify our terminology.
There is the domain layer with all you domain entities, e.g. product, category etc.
Then there's the Data Layer with your repositories that hydrate your domain entities and then you have a Service Layer with you application services that talks to the data layer.
Finally you have a presentation layer with your views and controllers. The Controllers talk to you Aplication Service Layer. So a Product Controller talks to a Catalogue Service (e.g. GetProductBySku). The CatalogueService will have one or more repositories injected into its constructor (IProductRepository, ICategoryRepository etc.).
It's quite common in asp.net mvc to have ViewModels too. Put the ViewModels in your Application Service Layer.
So I'm not sure what you mean when you say "models" and "domain enntities" but I hope that clears up the terminology.