2

I would like to use Ninject in a console app.

I have several assemblies and want to be able to use ninject in all of them.

I have created resolver project which contains all of my ninject modules I also have a class in here that loads all of my modules into a kernel.

How do i access my kernel in all of my modules?

I read that i could use the service locator pattern but apparently it is not a good pattern.

TJF
  • 1,081
  • 1
  • 12
  • 26
  • For what do you need access to the kernel in the modules? I'm quite certain that you can solve it without using the kernel but rather by injection. If not directly, then by injecting a factory. Please go into more detail so we can help you better. – BatteryBackupUnit May 09 '14 at 06:52

1 Answers1

0

There are probably many opinions on the service locator pattern. Here is at least a post on why the (anti) pattern is troublesome:

Service locator is an anti-pattern

We have however been using it quite successfully. One way to avoid obscuring the class dependencies too much, couble be to have a factory load the dependencies and create the class:

public class FooFactory
{
    public void CreateFoo()
    {
        var d = ServiceLocator.Current.GetInstance<IDependency>();
        return new Foo(d);
    }
}

public class Foo
{
    public Foo(IDependency dep)
    {
    }
}
sondergard
  • 3,184
  • 1
  • 16
  • 25