niallNoigeallach
niallNoigeallach

Reputation: 130

Abstract Factory and Unity Dependency Injection

Im very unsure how i should approach this as im new to the Unity Container.

Heres a factory i have:

 class DataCopierFactory : IDataCopierFactory
{
    private readonly IUnityContainer _container;
    //not mad on binding the factory to the container, but feck it
    public DataCopierFactory(IUnityContainer container)
    {
    _container = container;

    }

    //return a data copier that will transfer data from any DB to any other DB
    public IDataCopier createDataCopier(int i)
    {
        switch(i)
        {
            case 1:
                return new ScheduleDataCopier(new LTEEFContext());  //somehow resolve the LTEEFContext..
            //case 2:
            //    
            default:
                throw new InvalidOperationException("Parameter -t=" + i + " does not exist");

        }
    }
}

When i call the createDataCopier method and pass in some enum, I want to use the Unity container to resolve my DB Object Context of the various types of IDataCopiers.

How do i do this?

Should i do it in the class that calls the factory? or inject the unitycontainer and resolve the different DB Object Contexts at that point.

Each Implementation of the IDatacopier has a IDBContext taken as a constructor, I want to basically inject this, but allow me to change the Context for different implementations of the Datacopier.

Hopefully this makes sense... :) Thanks in advance Neil

Upvotes: 0

Views: 1661

Answers (3)

Danny van der Kraan
Danny van der Kraan

Reputation: 5366

Good to see that the OP solved this himself. If people are still interested we have an extensive example of how to solve problems like these: https://dannyvanderkraan.wordpress.com/2015/06/29/real-world-example-of-dependency-injection-based-on-run-time-values/

Upvotes: 0

Sean B
Sean B

Reputation: 11607

I would suggest having multiple Unity registrations for your IDataCopier (for each implementation). You can inject them all to your factory by taking a constructor param of IDataCopier[]. Note: with Unity you must name each registration when multiply registering.

You then need a way to select the one you want. I'd suggest adding a get property to your IDataCopier interface that is either your int or a string tag. Your createDataCopier(int) could then query the IDataCopier[] that you got from Unity for an element with the get property == the int i. This is the "strategy pattern", and allows for more flexibility than the static switch statement. Storing IDataCopier[] into a dictionary field would be a handy way to facilitate the selection, but a simple LINQ statement would work too.

Upvotes: 0

niallNoigeallach
niallNoigeallach

Reputation: 130

Found the answer.. well it was my fault :)

I was trying to inject the objectContext as a dependency when i shouldnt have. I dont think the factory should have an instance of your IOC container.

Im also using the Repository pattern, each DataCopier impl shouldnt need an instance of the ObjContext as they all have instances of their respective repositories they need.

This mightnt be the best way of doing things, as im coupling the repo to the datacopier classes, but i think its ok since the abstraction is already there between the repo's and the object context (i inject the objectcontext as a dependency using Unity to all the repo's )

Any suggestions welcomed..

Upvotes: 1

Related Questions