Reputation: 1969
I am having a little problem figuring out how to configure Unity IoC container in my n-tier ASP .net MVC application.
I have 3 projects in my solution: 1. MVC (Used as View) : Has referene to Business Logic Layer only 2. Business Logic Layer: Has reference to Repository project only 3. Respository: Do not have reference to any of the above projects
Please notice the way my reference work, I have done it like this so I will be able to replace the communication between my tiers using WCF with less difficulties.
I have configured a Unity container in my MVC application doing something like this (Global.asax):
container.RegisterType(); //MyService is in my Business Logic Layer
The MyService class uses my repository in its constructor meaning that when one of those MyService classes is created a MyRepository is also required:
public MyService(IRepository repository) : base(repository) { }
As you can see I still have not configured IRepository in my container.
I don't want to do it in my Global.asax because i dont want to add any reference of Repository to MVC (View) tier.
Where is the best place to configure this ? I am kind of confused here....
Thanks in advance.
Upvotes: 2
Views: 1324
Reputation: 2106
If we are using Unity. we can implement an extension of Unity container in the middle layer and register in the Presentation layer like below.
//Data Layer dependency mapping as extension eg : IUnitOfWork
container.AddNewExtension<DependencyInjectionExtension>();
More details please follow article.
Thanks
Upvotes: 0
Reputation: 1969
Let me tell you what I did so far:
I created a static class in my Service and Repository Later, I create the Unity container in my View Layer (MVC) and pass it to those static classes, they populate the container with what they need plus the next layer, for example service layer also call the static class in my repository.
This way I configure the container with everything i need.
The question is : is this a good thing to do ?
Upvotes: 1