DI Castle WCF构造函数我如何

时间:2012-10-04 18:27:17

标签: c# oop dependency-injection castle-windsor

我使用Castle DI继承了一个项目。我在解决方案中添加了一个WCF,需要使用解决方案中的一些功能。该课程有以下内容,我假设是注入。

private readonly IOrderRepository _orderRepository;
    private readonly IEshopOrderRepository _eShopOrderRepository;
    private readonly IUserRepository _userRepository;
    private readonly IListRepository _listRepository;
    private readonly INHibernateRepositoryWithTypedId<ProductVariant, string> _productVariantRepository;

    private readonly IMapper<GiftCardPayment, string, GiftCardPaymentDto> _giftCardDtoMapper;
    private readonly IMapper<AbstractOrder, OrderDto> _orderDtoMapper;
    private readonly IMapper<AbstractOrder, RecurringOrder> _recurringOrder;
    private readonly IMapper<Address, CreditCardPaymentDto> _creditCardDtoMapper;

    public delegate OrderDto ShipmentProcessing(OrderDto order, bool isRecap);

    public OrderManagementService(IOrderRepository orderRepository, IUserRepository userRepository, IListRepository listRepository,
        IEshopOrderRepository eShopOrderRepository,
        INHibernateRepositoryWithTypedId<ProductVariant, string> productVariantRepository,
        IMapper<GiftCardPayment, string, GiftCardPaymentDto> giftCardDtoMapper,
                                  IMapper<AbstractOrder, OrderDto> orderDtoMapper,
        IMapper<AbstractOrder, RecurringOrder> recurringOrder,
        IMapper<Address, CreditCardPaymentDto> creditCardDtoMapper)
    {
        _orderRepository = orderRepository;
        _eShopOrderRepository = eShopOrderRepository;
        _userRepository = userRepository;
        _listRepository = listRepository;
        _productVariantRepository = productVariantRepository;
        _giftCardDtoMapper = giftCardDtoMapper;
        _orderDtoMapper = orderDtoMapper;
        _recurringOrder = recurringOrder;
        _creditCardDtoMapper = creditCardDtoMapper;
    }

所有这些都有效。我的问题是如何在WCF服务类中实现此模式。我无法将参数添加到构造函数中,因为客户端不会提供它们。

我可以在WCF中使用DI,因此该部分正在运行。

由于

1 个答案:

答案 0 :(得分:1)

Castle有一个WCF facility。本质上,它是一个自定义服务工厂,它创建您的服务,而不是WCF提供的默认工厂。如果要使用流畅的注册,则必须将服务提供给服务主机工厂中使用的容器。为此,您可以创建自己的服务主机工厂,继承自castle项目提供的工厂(Castle.Facilities.WcfIntegration.DefaultServiceHostFactory),并在工厂的构造函数中创建windsor容器。

您自己的服务主机工厂的示例实现,派生自castle提供的DefaultServiceHostFactory:

public class MyOwnServiceHostFactory: Castle.Facilities.WcfIntegration.DefaultServiceHostFactory
{

    public MyOwnServiceHostFactory() : base(CreateKernel())
    { }

    private static Castle.MicroKernel.IKernel CreateKernel()
    {
        var container = new Castle.Windsor.WindsorContainer();
        container.Install(new WindsorInstaller());
        return container.Kernel;
    }

}


public class WindsorInstaller : IWindsorInstaller
{

    #region IWindsorInstaller Members

    public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {
        container.AddFacility<Castle.Facilities.WcfIntegration.WcfFacility>();
        container.AddFacility<Castle.Facilities.TypedFactory.TypedFactoryFacility>();

        container.Kernel.Resolver.AddSubResolver(new Castle.MicroKernel.Resolvers.SpecializedResolvers.ListResolver(container.Kernel));
        // add your services here...
    }

}