Dependency Nesting for Models

I'm sure someone asked about this before, but I'm struggling to find where.

I use Ninject to remove dependencies from my controllers along with the repository design pattern.

As I understand it, one of the advantages of this approach is that I can easily split my repositories and domain entities and use a different assembly if I wish. Therefore, I have saved my domain entities and repositories in external assemblies and can mock all interface dependencies.

It seems that although I can use interfaces to refer to my domain objects in most places, I should use references to my specific classes when it comes to model binding. I read that this is due to serialization, which I understand, but is this the only way to avoid accessing domain objects to create separate models?

What can I do with custom model binding?

A bit of background: I'm an experienced ASP.net developer, but new to MVC.

+4
source share
3 answers

The main advantage of using the dependency injection framework is IoC (control inversion):

  • weak connection
  • great flexibility
  • easier testing

So, what is usually done is to enter repositories through their interfaces, for example

public class MyController : Controller { private IPersonRepository personRepo; public MyController(IPersonRepository personRepo) { this.personRepo = personRepo; } ... } 

During testing, this makes it easy to enter my false repository, which returns exactly the values ​​that I want to check.

Injection of domain objects does not make such a sense, since they are more closely related to the functionality in a particular class / controller, and thus, their abstraction will be just an overhead rather than a benefit. Instead, if you want to separate your actual entity model from the controller , you can take a look at the MVVM template by creating specialized "ViewModels".

Just think about controlling your controller: "What would I like to mock unit test it?"

  • Access to the database β†’ repository
  • External dependencies -> other BL classes, WS calls, etc.

I would not include domain objects here, since they usually represent a container .

+4
source

Models for viewing should be ordinary data containers without logic and, therefore, should not have any dependencies at all. Instead, add storage to the controller and assign the necessary data from the repository to the appropriate properties of your view model.

+8
source

Some details will help. Maybe a little code?

For starters, you should avoid injecting dependencies into the domain entities, but rather use domain services.

More info here .

Change 001:

I think we should clarify our terminology. There is a domain level with all of your domain entities, for example. product, category, etc. Then there is a data layer with your repositories that moisturize your domain objects, and then you have a service level with your applications that talk to the data level.

Finally, you have a presentation layer with your views and controllers. The controllers tell you about the Aplication service level. Therefore, the product controller is negotiating with a directory service (for example, GetProductBySku). The CatalogService will have one or more repositories entered into its constructor (IProductRepository, ICategoryRepository, etc.). Asp.net mvc quite often also has ViewModels. Put ViewModels at the service level of applications.

So I'm not sure what you mean when you say β€œmodels” and β€œdomain names,” but I hope this clears up the terminology.

+1
source

Source: https://habr.com/ru/post/1380152/


All Articles