Hi I have been having trouble trying to tell Unity that for an Interface if it has multiple implementations , I want it to inject them in different classes.Here is what I mean:
Let's say I have an interface IProductCatalogService and two implementations
ProductCatalog : IProductCatalogService and ProductCatalogService : IProductCatalogService.
How would I go about telling Unity that for Class A I want in my constructor passed an instance of type ProductCatalog and for Class B I want an instance of ProductCatalogService.
I am using Unity in an ASP.NET Web API project and I have set the resolver in the GLobalConfiguration.
For simple 1 to 1 registrations everything works.
Here is what I have tried but it does not seem to work:
public class DependencyServiceModel
{
public Type From { get; set; }
public Type To { get; set; }
public IEnumerable<Type> ForClasses { get; set; }
}
public void RegisterTypeForSpecificClasses(DependencyServiceModel dependencyService)
{
foreach (var forClass in dependencyService.ForClasses)
{
string uniquename = Guid.NewGuid().ToString();
Container.RegisterType(dependencyService.From,
dependencyService.To, uniquename);
Container.RegisterType(forClass, uniquename,
new InjectionConstructor(
new ResolvedParameter(dependencyService.To)));
}
}
In the DependencyServiceModel, From is the interface, To is the object to witch I want to instantiate and ForClasses are the Type for which I want to use the To Object.