Constructor injection using Unity 3.5 in a console application - C #

I have a console application that has links to many DLLs. The console application must create objects in these DLLs. The dll has dependencies that I would like to introduce into their constructors. My requirement is to do all registrations in the console application - this is the root of the composition. I can go this far, but how can I save these registrations in libraries without passing / reference to UnityContainer?

I want to avoid the service locator pattern and just use the constructor / injection method. None of this, if possible:

_container.Resolve<IData>()  // not what I want

Here is what I got in the console application:

            static IUnityContainer _container;

            static void Main(string[] args)
                    {
                        LoadContainer();
                        var worker = new Worker(_container.Resolve<IData>()); // I don't like this
                        worker.Start();
                    }

            private static void LoadContainer()
                    {
                        _container = new UnityContainer();

                        // This will register all types with a ISample/Sample naming convention 
                        _container.RegisterTypes(
                            AllClasses.FromLoadedAssemblies(),
                            WithMappings.FromMatchingInterface,
                            WithName.Default);
                    }

Worker, ? MVC UnityConfig, . , . Unity, ?

+4
2

, Worker , IWorker:

var worker = _container.Resolve<IWorker>();

var worker = _container.Resolve<Worker>();

:

worker.Start();

, , Worker , . , Resolve() . .

Edit

- , :

public class Root
{
    private readonly A _a;

    public Root(A a)
    {
        if (a == null) throw new ArgumentNullException("a");
        _a = a;
    }
}

public class A
{
    private readonly B _b;

    public A(B b)
    {
        if (b == null) throw new ArgumentNullException("b");
        _b = b;
    }
}

public class B
{
    private readonly C _c;

    public B(C c)
    {
        if (c == null) throw new ArgumentNullException("c");
        _c = c;
    }
}

public class C
{

}

, Unity, :

var root = _container.Resolve<Root>();

. "root" , , - .

, . , - , - , Resolve(), Main().

, - , , , DI + .

+3

( ) . , Foo, Bar, BarFactory BarResolver, Unity, - ( - CR).

http://www.wiktorzychla.com/2012/12/di-factories-and-composition-root.html

+1

Source: https://habr.com/ru/post/1539326/


All Articles