Unity Application Block, legacy injection

I got a problem using Unity Application Block, I created a base class called Composition.

Each composition contains an IUnityContainer object when I create a top-level UniversalComposition object that I want to initialize with the UnityContainer.

Any object that is created from UniversalComposition, I want to inject a child IUnityContainer into it using the IUnityContainer.CreateChildContainer method.

class Program
{
    static void Main(string[] args)
    {
        UniversalComposition universe = new UniversalComposition();


    }
}

public class UniversalComposition : Composition
{
    // Everything that gets resolved in this container should contain a child container created by the this container

    public UniversalComposition()
    {
        this.Container.RegisterType<IService, Service>();
    }

    protected override IUnityContainer CreateContainer()
    {
        return new UnityContainer();
    }

}

public abstract class Composition
{
    protected IUnityContainer Container {get; private set;}

    public Composition()
    {
        this.Container = this.CreateContainer();
    }

    public TInstance Resolve<TInstance>()
    {
        return this.Container.Resolve<TInstance>();
    }

    protected abstract IUnityContainer CreateContainer();
}

public class Service : Composition, IService
{
    public Service(/* I want to inject a child Unity Container in here container.CreateChildContainer() */)
    {
    }
}

public interface IService { }

      

0


source to share


1 answer


I don't think this will work via injection as you have implemented it in the parent, since the parent container does not exist until the child is instantiated. Thus, you have no way to create a child injection container. Your best bet would be to inject the parent container through the parameterized base class constructor and then inject both into the child class. The default constructor can call a parameterized nullable constructor, and the parameterized constructor can detect this and create a container if not specified. The example below is simplified for clarity.



public abstract class BaseClass
{
    public IUnityContainer Container { get; protected set; }

    public BaseClass() : BaseClass(null) {}

    public BaseClass( IUnityContainer container )
    {
        this.container = container ?? this.CreateContainer();
    }

    public abstract IUnityContainer CreateContainer();
}

public class DerivedClass : BaseClass
{
    public IUnityContainer ChildContainer { get; private set; }

    public DerivedClass() : DerivedClass(null,null) {}

    public DerivedClass( IUnityContainer parent, IUnityContainer child )
        : BaseClass( parent )
    {
        this.ChildContainer = child ?? this.CreateChildContainer();
    }

    public IUnityContainer CreateContainer()
    {
         return new UnityContainer();
    }

    public IUnityContainer CreateChildContainer()
    {
         return this.Container.CreateChildContainer();
    }

}

      

+1


source







All Articles