views:

18

answers:

1

hi, I am creating a new application using asp.net mvc, I m using munq IOC container as my dependency injection..The issue is i want to create a new project for dependency resolution where i can register all the controllers of mvc project and the repositories of infrastructure project..I have to add Dependency Resolution project as a reference in my mvc app as thats the starting point... but the prob is in order to register the controllers in this separate app i need to have the reference of the mvc in the dependency Resolution project itself...but such a thing is not possible because that would cause a circular reference..

so how to resolve this issue? or what is the best way of managing the dependency resolution? I don't want to end up registering everything in the Global.asax

A: 

Get the latest version from the source tab at Munq.Codeplex.com. This version has a view improvements and it is the version I am most familiar with, and I wrote it.

To prevent circular references for registration, create a class project that includes reverences to Munq.Interfaces and the interfaces and implementations you wish to register.

Create a class that implements IMunqConfig. It has one method void RegisterIn(IIocContainer container). Implement this method.

public class MyRegistration : IMuncConfig

{

public void RegisterIn(IIocContainer container)
{

    container.Register<IMyInterface>(c => new MyImplementation());

    //       OR

    container.Register<IMyInterface, MyImplementation>();

   // Repeat as required for each thing to register

}

Then in global.asax

    protected void Application_Start()
    {
        IocContainer = new Container();
        **Munq.COnfigurationLoader.FindAndRegisterDependencies(container);

** AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

This will search the bin directory for any dlls that have classes implementing IMunqConfig and execute the RegisterIn method on each. So just drop the registration dlls into the bin directory and registration happens automagically :)

Matthew

Matthew