开发者

Dependency resolution as a separate project ..How to?

开发者 https://www.devze.com 2023-01-21 11:23 出处:网络
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

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 re开发者_C百科solve 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


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


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 wouldn't register MVC controllers in the same project as your repositories. The MVC app should "own" its composition, including registering its internal dependencies.

Registrations for your infrastructure project should ideally be in that project, especially if it's not something you're going to make available to other projects that use different DI containers. If that project needs to remain DI-agnostic then maybe it might make sense to separate DI registration into a separate project.

But I definitely wouldn't make a service (like your repository) responsible for registering the internal dependencies of its consumers (like your MVC app.)

0

精彩评论

暂无评论...
验证码 换一张
取 消