views:

494

answers:

4

I have one interface with 2 classes implementing it, I need to load each class but unity has:

m_unityContainer.Resolve() // Where is the interface IGeneric

my config looks like:

      <type type="IGeneric" mapTo="ClassA">
      </type>
      <type type="IGeneric" mapTo="ClassB">
      </type>

any ideas?

thanks

A: 

This will give you all the registered classes that implement IGeneric.

IEnumerable<IGeneric> objects = container.ResolveAll<IGeneric>();
gcores
A: 

I found out the solution, a name property has to be used in each entry:




and the code will look like obj= container.ResolveAll("ClassA");

A: 

You could also use a generic interface as follow:

public interface IGeneric{}

public interface IGeneric<T> : IGeneric{}

Then have a type safe resolution of the the interface:

container.RegisterType<IGeneric<ClassA>, ClassA>();
container.RegisterType<IGeneric<ClassB>, ClassB>();

ClassA classA = container.Resolve<IGeneric<ClassA>>();
ClassB classB = container.Resolve<IGeneric<ClassB>>();

Some interesting things start happening when you go down this road...

Schalk
A: 

Schalk - looks good. What would be the notation for specifying that in the Unity.config?