views:

44

answers:

1

I am writing a desktop GIS application and it supports MapXtreme, MS Virtual Earth and our Custom Map Engine.Users of application can change the map engine at run-time by selecting from dropdownlist.I have a Factory class to change map engine like this.

public class MapFactory implements IMapFactory
{
    public IMapEngine createInstance(MapType type)
    {
       if(type==MapType.MapXtreme)
          return new MapXtremeEngine();
       else if(type==MapType.VirtualEarth)
          return new VirtualEarth();
       //....other code
    }
}

Can I use a Dependency Injection Framework to create suitable MapEngine implementation at run-time by type parameter?

+1  A: 

Your example is the exact right pattern for conditionally instantiating an object. Anywhere you need to create an instance, accept IMapFactory in the constructor.

The most a DI framework should do is hand out the IMapFactory instance.

Bryan Watts
Can DI Framework create instances of VirtualEarth, MapXtreme or.. by using type parameter, if so can you give me an example how to configure it to do this?
mcaaltuntas
As I said, what you have is the right way to set up conditional instantiation. You don't need the DI framework to create the VirtualEarth and MapXtreme instances, the factory does that. The DI framework creates instances of the factory.
Bryan Watts
Unless those classes have complex constructors, and you left them out for simplicity. Is that the case?
Bryan Watts