Implement unity container

0

I am building the architecture and I need to define the injection of dependencies. The idea is to set unity in a class library project to be able to reuse it (is this possible?)

I have the following structure:

  

Solution.Core (I define interfaces and models)

     

Solution.Infrastructure (EF and repository implementation)

     

Solution.Infrastructure.Service (Implementation of services)

     

- UI

     

Solution.Mvc1

     

Solution.Mvc2

     

- WEB API

     

Solution.Mvc1.WebApi

     

Solution.Mvc2.WebApi

The idea is to create a new project for the injection:

  

Solution.Infrastructure.DependencyInyection

and inject the dependencies in the projects WebApi that I already have defined.

Is the architecture I'm following correct?

What if I need to use the dependency injection project in one or more class library in addition to the projects WebApi ? How can I do it generic?

Greetings

    
asked by Mauro Petrini 30.01.2016 в 22:28
source

1 answer

1

Here is an excellent and complete guide

ASP.NET MVC 4 Dependency Injection

Actually in the project DependencyInyection you will not perform the injection, what you would do there is to define the mapping between the interfaces and the classes that should be injected. When the mvc pipeline executes, it will use the data of this project to inject the instance into the constructor.

For what you comment, I understand that in that project DependencyInyection you will define the class that will load the UnityContainer that you will use in the Application_Start so that mvc can resolve the dependencies.

What if there is a topic, I do not know which libraries you will work with but if you use mvc 5 for example with Unity .Mvc5

Using Unity.Mvc5 and Unity. WebApi together in a project

You will notice that the information to register the UnityDependencyResolver changes if it is an mvc project or if it is webapi, that's why the common project that defines the libraries returned by an instance of UnityContainer with all defined mapping and nothing else , then depending on where you use it, you will assign it to DependencyResolver in this way that project that defines the mapping is generic for use in both types of mvc projects.

    
answered by 30.01.2016 в 22:57