温莎城堡通用类型注册

时间:2018-06-19 22:20:31

标签: c# generics dependency-injection castle-windsor registration

我是温莎城堡和依赖注入的新手。我对通用类型的注册有疑问。

这是与我的问题类似的示例。 (仅在一个小项目中创建即可解决,但无法解决)      我有一个具有默认ValuesController的Web API项目

      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)};";
        }


    }

这是对IVehicleManager的依赖。我将此依赖项注入了控制器。

VehicleManager.cs类实现IVehicleManager接口。此外,它还与通用的Vehicle类型有关。

    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);
    }

车辆是实现IInfo接口的通用类,也具有对ICalculator和“ brandName”字段的依赖关系。

     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具有不同的实现,其中之一是(代码没有任何意义)

    public class AgeCalculator : ICalculator
    {
        public long GetAge(int id)
        {
            return id;
        }
    }
      public interface ICalculator
    {
        long GetAge(int id);
    }

我有一个IInfo接口,该接口由BaseInfo和ChildInfo类实现,如下所示:

     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; }
    }

我通过城堡温莎将这些组件注册到我的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());

        }
    }

当我尝试运行api / values / 5(HTTPGet)时,出现以下错误。

 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.

如果有人可以帮助我如何注册这些通用类型? 谢谢

0 个答案:

没有答案