使用Castle Windsor IoC容器时添加服务初始化方法

时间:2013-04-23 10:03:58

标签: c# .net castle-windsor ioc-container

如果我的服务有一些初始化代码,我知道两个选项:

  1. 实施ISupportInitialize
    Pro:仅依赖于.NET本机接口 Con:我通常只使用BeginInit(),所以总是没有必要的代码为空EndInit()

  2. 实施Castle Core的IInitializable 亲:只需要实施一种方法 Con:我必须让我的课程依赖Castle Core。

  3. 有更好的选择吗?

2 个答案:

答案 0 :(得分:6)

是的,有:

Component.For<Foo>().OnCreate(foo => foo.WhateverMethodYouWant());

详情为in the documentation

答案 1 :(得分:0)

另一种选择可能是将相关组件转换为Castle设施,在那里您可以更明确地控制,但这可能是过度的。

我会选择选项1.冗余的EndInit是无关紧要的。