I am new to castle windsor and dependency injection. I have a question about registration of generic types.
This is a similar example to my issue. (Just created in a small project to able to solve, but couldnt solve it) I have a web api project which has default ValuesController
public class ValuesController : ApiController
{
IVehicleManager _service;
public ValuesController(IVehicleManager service)
{
_service = service;
}
.
.
// GET api/values/5
public string Get(int id)
{
return $"value {_service.GetVehicleInfo(id)};";
}
}
This as a dependency to IVehicleManager. I injected this dependency to my controller.
VehicleManager.cs class implements IVehicleManager interface. Also it has a dependency to a generic Vehicle type.
public class VehicleManager : IVehicleManager
{
IVehicle<IInfo> _info;
public VehicleManager(IVehicle<IInfo> info)
{
_info = info;
}
public string GetVehicleInfo(int id)
{
return _info.GetNameOfBrand(id);
}
}
public interface IVehicleManager
{
string GetVehicleInfo(int id);
}
Vehicle is a generic class which implements IInfo interface, also has dependencies to ICalculator and "brandName" field.
public class Vehicle<T>:IVehicle<T> where T:IInfo
{
ICalculator _calculator;
string _brandName;
public Vehicle(string brandName, ICalculator calculator)
{
_brandName = brandName;
_calculator = calculator;
}
public string GetNameOfBrand(int id)
{
return $"{_brandName} and {_calculator.GetAge(id)}" ;
}
}
public interface IVehicle<T>
where T:IInfo
{
string GetNameOfBrand(int id);
}
ICalculator has different implementations and one of them is (code doesnt have any meaning)
public class AgeCalculator : ICalculator
{
public long GetAge(int id)
{
return id;
}
}
public interface ICalculator
{
long GetAge(int id);
}
I have IInfo interface which is implemented by BaseInfo and ChildInfo classes as below:
public class BaseInfo : IInfo
{
public string BrandName { get; set; }
}
public class ChildInfo2 : BaseInfo
{
public string TypeOfVehicle { get; set; }
}
public interface IInfo
{
string BrandName { get; set; }
}
I register these components by Castle Windsor as below to my ServiceInstaller.cs
public class ServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ICalculator>().ImplementedBy<AgeCalculator>().LifestylePerWebRequest());
container.Register(Component.For<IInfo>().ImplementedBy<BaseInfo>().LifestylePerWebRequest(),
Component.For<IInfo>().ImplementedBy<ChildInfo>().LifestylePerWebRequest(),
Component.For(typeof(IVehicle<IInfo>)).ImplementedBy(typeof(Vehicle<IInfo>)).DependsOn(Dependency.OnValue("brandName", "bmw")).DependsOn(Dependency.OnComponent<IInfo,BaseInfo>())
.LifestylePerWebRequest(),
Component.For<IVehicleManager>().ImplementedBy<VehicleManager>().DependsOn(Dependency.OnComponent<IInfo, ChildInfo>()).DependsOn(Dependency.OnComponent<IVehicle<IInfo>, Vehicle<IInfo>>()).LifestylePerWebRequest());
}
}
When I try to run api/values/5 (HTTPGet) I got the error below.
Castle.MicroKernel.Resolvers.DependencyResolverException occurred
HResult=0x80131500
Message=Missing dependency.
Component WebApplication1.Controllers.ValuesController has a dependency on
BusinessLogic.IVehicleManager, which could not be resolved.
Make sure the dependency is correctly registered in the container as a
service, or provided as inline argument
Inner Exception 1:
HandlerException: Handler for BusinessLogic.IVehicleManager was not found.
If anyone can help me how to register these generic types? Thank you
User contributions licensed under CC BY-SA 3.0