我有多深入课堂,正确地将Castle Windsor连接到DI?

时间:2010-11-03 14:28:37

标签: c# castle-windsor ioc-container

是的,所以我是Castle的新手,我正在努力弄清楚我需要在多大程度上连接服务。下面是我正在使用的课程样本,以及他们在我创建的世界中的位置。

alt text

我正在努力完成它以正确连接Castle,以便我可以调用TemplateEmailViaSalesforce类来完成工作,并通过来自Castle的DI连接相关类。但是我不确定在注册组件时我还有多远。下面是我的第一次尝试(我通过代码注册作为试用版,这是一个创建容器的几种方法的混搭)

IWindsorContainer container = new WindsorContainer();
        container.AddFacility<FactorySupportFacility>();

        // add the DAL mapper factory
        container.AddComponent<ITemplateMapperFactory>();

        // individual mappers
        container.Register(Component.For<ICSGEmailTemplateMapper>().UsingFactory((ITemplateMapperFactory f) => f.CSGEMailTemplate));
        container.Register(Component.For<IUserMapper>().UsingFactory((ITemplateMapperFactory f) => f.User));
        container.Register(Component.For<IGoupMapper>().UsingFactory((ITemplateMapperFactory f) => f.Group));
        container.Register(Component.For<IAccountTeamMapper>().UsingFactory((ITemplateMapperFactory f) => f.AccountTeam));
        container.Register(Component.For<ISalesTeamMapper>().UsingFactory((ITemplateMapperFactory f) => f.SalesTeam));
        container.Register(Component.For<ICSGFormulaMapper>().UsingFactory((ITemplateMapperFactory f) => f.CSGFormula));
        container.Register(Component.For<ISFObjectDefinitionMapper>().UsingFactory((ITemplateMapperFactory f) => f.SFDCObjectDefinition));
        container.Register(Component.For<ISFObjectValueMapper>().UsingFactory((ITemplateMapperFactory f) => f.SFDCObjectValue));
        container.Register(Component.For<ISalesforceTemplateMapper>().UsingFactory((ITemplateMapperFactory f) => f.Template));
        container.Register(Component.For<IRecipientMapper>().UsingFactory((ITemplateMapperFactory f) => f.Recipient));

        // BLL stuff (domain components)...general
        container.AddComponent<CSGEmailTemplateRepository, CSGEmailTemplateRepository>();
        container.AddComponent<RecipientRepository, RecipientRepository>();
        container.AddComponent<SFObjectDefinitionRepository, SFObjectDefinitionRepository>();
        container.AddComponent<SFObjectValueRepository, SFObjectValueRepository>();
        container.AddComponent<TemplateRepository, TemplateRepository>();
        container.AddComponent<UserRepository, UserRepository>();
        container.AddComponent<ITemplateService, TemplateService>();

        // specific for this action
        container.AddComponent<TemplateEmailerViaSalesforce>();
        container.AddComponent<TemplateParse>();

        // Aspects
        container.AddComponent<TraceAspect>();

现在,我稍后收到错误:container.Resolve<TemplateEmailerViaSalesforce>();

AssemblerTests.ShouldCreateTemplateService : FailedTest method Tests.AssemblerTests.ShouldCreateTemplateService threw exception:  Castle.MicroKernel.Facilities.FacilityException: You have specified a factory ('Castle.MicroKernel.Registration.GenericFactory`1[[CSG.Salesforce.TemplateEmailer.DAL.Access.IUserMapper, CSG.Salesforce.TemplateEmailer.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' - method to be called: Create) for the component 'CSG.Salesforce.TemplateEmailer.DAL.Access.IUserMapper' CSG.Salesforce.TemplateEmailer.DAL.Access.IUserMapper that failed during invoke. --->  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->  Castle.MicroKernel.ComponentRegistrationException: Type CSG.Salesforce.TemplateEmailer.DAL.ITemplateMapperFactory is abstract.

IUserMapper失败似乎是class UserMapper:BaseSalesforceMapper<UserData>,IUserMapper

其他映射器工作,但我得到一个错误,并非所有依赖项都满足,我需要IUserMapper注册才能完全满足。

我必须走多远才能让兔子穿上它?在这一点上,我正在寻找BLL进入DAL并进入一个基类,应用程序映射器是由它构建的,它似乎并不正确。我正在努力弄清楚我要注册的内容以及城堡本身对DI的隐含做法。

任何帮助都将不胜感激。感谢。enter code here

2 个答案:

答案 0 :(得分:3)

这些是我在您的代码中看到的问题:

  • container.AddComponent<ITemplateMapperFactory>();

    你需要在这里提供实现类,这就是你在解析时获得异常的原因。

  • container.AddComponent<UserRepository, UserRepository>();

    如果您正在注册没有接口的组件,则无需将实现类型重复为服务类型。

  • UsingFactory()已过时。请改用UsingFactoryMethod()

  • AddComponent()已过时。请改用Register(Component.For...))
  • 在最新版本的Windsor中,无需添加FactorySupportFacility即可使用UsingFactoryMethod()
  

我必须注册的内容以及使用Castle本身隐含地完成的内容。

您需要注册希望Windsor管理的每个组件。如果你没有注册,温莎不知道。你可以set up conventions to avoid registering components individually。温莎为您做的是隐式自动连线组件。

答案 1 :(得分:0)

我并不完全理解你在这里所做的一切。最好只需几个班级就可以开始,并采取一些步骤,按照您想要的方式配置所有内容。您也可能希望看一下Windsor的自动布线 - 它应该至少使您的一些组件更容易配置。

所有这一切,在我看来,你正在指定一个用于工厂实现的接口(ITemplateMapperFactory)。正如错误消息所示:“ITemplateMapperFactory是抽象的”。 Windsor无法实例化无法实例化的内容。

免责声明:我不是工厂设施的专家,因为我不使用它。