A
A
Anton Starostin2014-07-13 11:24:32
C++ / C#
Anton Starostin, 2014-07-13 11:24:32

How to use Unity IoC in N-Layer architecture application?

For a long time now I have been trying to master Unity IoC to use Dependency Injection in my applications. Every time the whole striving is shattered by the same problems. But more about this later and in order.
I am writing an ASP.NET MVC application. The N-Layer architecture was chosen for the application. Based on the architecture, there are 4 layers in the application (DAL, BLL, Web, DTO). Each layer depends on its neighbors and therefore inversion of control is used.
Previously, I used the Ninject package for DI, which allowed me to make properties like

[Inject]
public IMyInterface injectetProp {get; set;}

in special file, I indicated for which interface which instance to use and that's it. Each time, for example, a controller was constructed, an object was immediately injected that implemented this interface.
Somewhat later, I was told about Unity and that its configuration can be set in the application's configuration files, thereby eliminating the recompilation of the project if you need to change dependencies. This could not but surprise me. And so I decided to switch to Unity.
I read a lot of articles about DI using Unity, but everywhere there was either configuration or use separately. I could not find an answer to my questions, probably just overlooked and I ask you to poke your nose, so to speak, where it is written =)
Actually, the questions themselves:
- Is it possible to put the DI configuration in web.config, and so that this configuration is the same for all layers of the application, or will it be necessary to duplicate the interface resolution settings in each layer?
- Is there any simple dependency resolution mechanism available, like the one I used in Ninject, or will it be necessary to create a UnityContainer and Resolve through it wherever a dependency needs to be resolved? Of course, this is not too difficult, it just looks clumsy after Ninject.
Thanks for the help, I just don't have anyone to ask other than the community.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vmp32, 2014-07-14
@Lixoradka

- Is it possible to put the DI configuration in web.config, and so that this configuration is the same for all layers of the application, or will it be necessary to duplicate the interface resolution settings in each layer?

Yes, in the unity section of the web.config file,
an example at the end of this article: smarly.net/forum/asp-net-article/thread2/ispolzova...
Video with an example of use: www.youtube.com/watch?v=QcN2lh3GIpE&index= 5&list=P...
Properties that require injection must be marked with the [Dependency] attribute , when the dependency is resolved, the property will be initialized.
Using a container in MVC
video with a step-by-step example: www.youtube.com/watch?v=rVLY2NnEXRo&index=3&list=P...
PS: A
quick start on unity (will remove most of the questions):
habrahabr.ru/post/62830
habrahabr.ru /post/63280
habrahabr.ru/post/63568

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question