Dependency injection Castle Windsor使用工厂xml替代方案
我有一个普通的Castle Windsor组件注册命令:Dependency injection Castle Windsor使用工厂xml替代方案,dependency-injection,configuration,castle-windsor,umbraco-ucommerce,Dependency Injection,Configuration,Castle Windsor,Umbraco Ucommerce,我有一个普通的Castle Windsor组件注册命令: container.Register(Component.For<IEntity>() .UsingFactoryMethod(() => new EntityFactory().CreateEntity())); container.Register(Component.For()) .UsingFactoryMethod(()=>new EntityFacto
container.Register(Component.For<IEntity>()
.UsingFactoryMethod(() => new EntityFactory().CreateEntity()));
container.Register(Component.For())
.UsingFactoryMethod(()=>new EntityFactory().CreateEntity());
为了扩展我的UCommerce,我需要XML配置选项。
我已尝试注册它,但由于异常,它无法工作:
无法将字符串“Castle.Facilities.FactorySupport.FactorySupportFacility,Castle.Facilities.FactorySupport”转换为类型。找不到程序集。确保它已部署,并且名称没有输入错误`
是否有人面临同样的挑战?请注意链接文章顶部的免责声明: 首选
使用FactoryMethod
而不是此工具:虽然该工具提供编程API,但它已被弃用,不鼓励使用,此处不再讨论。推荐的方法是使用fluent注册API的使用FactoryMethod
方法来创建组件。这限制了该工具在XML驱动和遗留场景中的实用性
如果您仍然想使用XML(请确保您有一个很好的理由),那么您需要采取两个步骤:
公共类实体工厂
{
公共资源创建()
{
返回新的EntityFactory().CreateEntity();
}
}
然后,您的XML配置将如下所示:
另外,若我并没有在xml配置中包含facility元素,那个么它就不会识别组件节点中的工厂属性,而只是在试图通过构造函数解析实体时忽略它们。