2

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.

3 Answers 3

2

Have you tried: http://mvcunity.codeplex.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Lee, MVCUnity looks good but I don't quite understand how this will solve my problem ? I don't have any assembly reference to Repository in my MVC application.
1

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 ?

Comments

0

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.

N-tier with DI (Unity)

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.