문제

I'm wondering what to do with dependency injection considering the following architecture.

I have 3 projects

MyProject.UI
MyProject.Business
MyProject.SomeOtherThing

StructureMap is being used for dependency injection in MyProject.UI.

public static class Bootstrapper
{
    public static void Run()
    {
        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());

        ObjectFactory.Initialize(x =>
        {
                x.For<ISomeClass>().Use<SomeClass>();
        }
    }
}

My question is, the MyProject.SomeOtherThing has some classes in it that are being consumed by MyProject.Business. These classes are set up to use DI.

namespace MyProject.SomeProject
{
    public SomeClass
    {
        public ISomeDependency SomeDependency { get; set; }

        public SomeClass (ISomeDependency someDependency)
        {
            SomeDependency = someDependency;
        }
    }
}

The business layer consumes the class and exposes a service that use SomeClass called SomeService.

In order for the DI registration to work, MyProject.UI has to have a reference to MyProject.SomeOtherThing.

I'd like to avoid that. Ideally the UI project would only have a reference to MyProject.Business.

What's the best way to handle this situation? Is it to move DI configurations into MyProject.Business? Or is there something else I'm missing?

Thanks!

도움이 되었습니까?

해결책 2

I had the same issue in my current project, as I have a core assembly for UI projects (asp.net MVC, asp.net web api and portable ares of asp.net mvc in separate assemblies), so I decided to put all of the configuration of DI in my Web.Core assembly. there is no other way to handle it. now your case you don't need to reference MyProject.SomeOtherThing in MyProject.UI if you don't need it, the best place to the DI configuration for your application is MyProject.Business. you just need to register the configuration in MyProject.UI, that's all

다른 팁

We created a new project "Binding" that references everything and has the Ninject bindings. This way, your UI project only needs to know about the Business layer and the Binding layer, not the SomeOtherThing layer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top