2

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
  • 1
    StructureMap has a nice feature to bind by convention. You should look into that. If you don't like it, then you could move the dependency resolution into an external class library (in the infrastructure of your layered application) and then call some "Initialize" method from the UI layer. Commented Feb 10, 2014 at 18:49
  • Related: stackoverflow.com/questions/9501604/… Commented Feb 10, 2014 at 21:11

2 Answers 2

2

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.

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

Comments

1

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

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.