Dependency registration responsibility (Ioc, DI, n-layer architecture)

Asked

Viewed 580 times

7

I’m starting a WPF (.NET/C#) project using an n-layer (Domain Driven Design) architecture, and to implement Control Inversion and Dependency Injection (Ioc/DI), I’m trying to use Microsoft’s Unity Framework.

The question I have is about the responsibility of registering dependencies, it would be correct to let the presentation layer create the container and record the required dependencies?

Or ALL of these dependencies must be registered in the Infrastructure layer?

Or is it both? For example: the presentation layer records things like Views and the Infrastructure layer records dependencies that are common to all layers?

  • Has your doubt been resolved? I ask because your question is still open.

1 answer

3

...it would be correct to let the presentation layer create the container and record the required dependencies?

Not.

The Presentation Layer is responsible for displaying system information to the user.

The Infrastructure Layer is responsible for providing technical resources that will support the upper layers. I mean, that last yes should have that kind of concern.

Some problems you may have when registering the container and its dependencies in the presentation layer:

How you are using an architecture following Domain Driven Design (DDD), you probably have a Business Layer, where "services" will depend on queries to repositories.

In this scenario, depending on your implementation, your business project (called DDD Domain) would depend directly on your Presentation Layer project, which is inconsistent with the DDD approach.

Another scenario you may have problem with: If your project’s Presentation Layer changes in the future to Asp.Net MVC, Web Form, etc.. you will have to keep changing/moving this code that solves/registers dependencies, IE, you will have more maintenance and moving this code again :-(.

Log dependencies in the Infrastructure Layer.

...Or ALL of these dependencies must be registered in the Infrastructure layer?

Yes, with respect to dependencies common to projects/layers, mainly Repositories and Services.

...Or is it both? For example: the presentation layer records things like Views and the Infrastructure layer records dependencies that are common to all layers?

How to register/resolve dependencies is not the responsibility of the Presentation Layer, I advise you to register the dependencies in the Infrastructure Layer, so you have a single point and do not need to maintain in two places.

  • Renan, I expressed myself badly when I said "things like Views", I meant register Viewmodels. I can for example have an interface called Iserverviewmodel that will be imploded later to control the behavior of the UI (XAML), in this case the record of the dependencies must be done in the presentation layer, correct?

  • I wouldn’t register it in the presentation layer. If you need to consume this "between the layers of your application" in order to maintain a low coupling between layers/modules, then I recommend that you register the dependency in the Infrastructure layer.

  • Even if the application has more than one presentation layer type? For example WPF and WEB (ASP.NET MVC)

  • 1

    Yes, imagine saving a Client on the system for example, when the user clicks Save, whether via WPF or Asp.net MVC, the business rule is the same, "the dependencies of repositories and services that the system will solve are also the same".

  • 1

    As for business rules: yes, they must be registered in the infra layer, but an interface that will only be used to control something related only to the screen, should it really be registered in the infrastructure? If I have 3 or more types of presentations (WEB, Mobile, Desktop, etc) record this all in one place, it seems a little strange.

  • Strange is that you register a dependency on the Presentation layer. The Presentation layer is responsible for displaying system information to the user. The Infrastructure layer provides technical resources that will support the upper layers. That is, that last yes should have that kind of concern.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.