I'm using the Simple Injector as my IoC container. I have developed a number of (excuse me probably using the wrong term) partially closed implementations, for a single generic interface.
I would like to be in a position to request the generic interface, and based on the types supplied, have Simple Injector return the correct class implementation. (I can understand this may be a no-no as implementations could overlap if done wrong etc. but I'd still like to know if it can be done.)
Based on the code snippets below, how can I configure Simple Injector to return me an instance of ITrim<Ford, Green>?
Common base class layer:
public interface IColour { }
public interface IVehicle { }
public interface ITrim<TVehicle, TColour>
where TVehicle : IVehicle
where TColour : IColour
{
void Trim(TVehicle vehicle, TColour colour);
}
public abstract class TrimVehicle<TVehicle, TColour> : ITrim<TVehicle, TColour>
where TVehicle : IVehicle
where TColour : IColour
{
public virtual void Trim(TVehicle vehicle, TColour colour) { }
}
Middle layer, providing common code for a type of vehicle:
public abstract class Green : IColour { }
public abstract class Blue : IColour { }
public abstract class Car : IVehicle { }
public abstract class Bike : IVehicle { }
public abstract class TrimCar<TCar, TColour> : TrimVehicle<TCar, TColour>
where TCar : Car
where TColour : IColour
{
public override void Trim(TVehicle vehicle, TColour colour)
{
base.Trim(vehicle, colour);
}
}
public abstract class TrimBike<TBike, TColour> : TrimVehicle<TBike, TColour>
where TBike : Bike
where TColour : IColour
{
public override void Trim(TVehicle vehicle, TColour colour)
{
base.Trim(vehicle, colour);
}
}
Final layer, providing more specific implementations:
public class Ford : Car { }
public class TrimFord<TFord, TColour> : TrimCar<TFord, TColour>
where TFord : Ford
where TColour : IColour
{
public override void Trim(TVehicle vehicle, TColour colour)
{
base.Trim(vehicle, colour);
}
}
public class Yamaha : Bike { }
public class TrimYamaha<TYamaha, TColour> : TrimBike<TYamaha, TColour>
where TYamaha : Yamaha
where TColour : IColour
{
public override void Trim(TVehicle vehicle, TColour colour)
{
base.Trim(vehicle, colour);
}
}