0

In my app I am using Unity container(through ServiceLocator) with constructor injection.

I have following class structure:

class Main
{

    void Main()
    {

        var c = ServiceLocator.Container.Resolve<C>(new ParameterOverrides { { "somedate", somedate } }, new ParameterOverrides { { "someRates", someRates } });
        var b = ServiceLocator.Container.Resolve<B>(new ParameterOverrides { { "c", c } }, new ParameterOverrides { { "somePara", somePara } });

    }
}

class A
{
    private B _b;

    public A(B b)
    {
        _b = b;
    }
}

class B
{
    private C _c;

    public B(C c, string somePara)
    {
        _c = c;
    }

}

class C
{

    public C(DateTime somedate, IDictionary<string, double> someRates)
    {

    }
}

Now you can see I have to call Resolve method and pass ParameterOverrides. When the parameters and Resolve calls increases the main method becomes messy, like if C also expects some other class instances or parameters and so on. Please suggest best practices to refactor this.

Thanks in advance!!!

Deepak
  • 303
  • 3
  • 14
  • This is not constructor injection, it's actually a common [anti-pattern](http://www.devtrends.co.uk/blog/how-not-to-do-dependency-injection-the-static-or-singleton-container). For starters, see http://stackoverflow.com/questions/9308424/unitycontainer-resolve-or-servicelocator-getinstance – Trevor Ash Jul 02 '16 at 04:13
  • It's constructor injection for class B, C. I am not using ServiceLocator inside class B, C I am injecting the dependency through constructor. – Deepak Jul 02 '16 at 04:18
  • Constructor injection means, in your example, a constructor that looks like this "void Main(C c, B c)". The "clue" that it's the anti-pattern is that there is a dependency on the service locator inside of the class. You're using a ServiceLocator for sure, but it's not via constructor injection. Phrased differently, you're creating values "inside" the constructor instead of passing them "to" the constructor. Incidentally, I'd offer an answer but I am not familiar enough with the features of Unity's ServiceLocator for constructor injection. – Trevor Ash Jul 02 '16 at 04:22
  • Main class is the entry point of the app, I can't pass instances of object to Main class if it's a console app, Please look the code sample from perspective of class B & C and not from Main class perspective. What I am looking for is avoid the messiness of too many resolves in Main class. – Deepak Jul 02 '16 at 04:27
  • Understood. Cannot contribute a direct answer due to ignorance of Unity. What I'd like to express however is that other dependency injection systems allow this type of setup external to the class consuming it. E.g. AutoFac, Ninject, etc. – Trevor Ash Jul 02 '16 at 04:29
  • Thanks! if you are so interested in making that code sample using constructor injection please suggest how would you pass instances in void Main(C c, B c) in a console app which is executed as an exe. Please answer from unity container's perspective, since that what I can use in my proj. – Deepak Jul 02 '16 at 04:32

0 Answers0