已在温莎城堡进行批量注册

时间:2019-03-07 14:57:30

标签: dependency-injection castle-windsor castle-windsor-3

在我的项目中,我具有接口 IProcess 和许多实现此接口的类。我需要注册所有这些实现。以下代码对我来说很好用:

Container.Register(Component.For<IProcess>().Named("SampleProcess").ImplementedBy<SampleProcess>());
Container.Register(Component.For<IProcess>().Named("SampleProcess2").ImplementedBy<SampleProcess2>());

但是,如果我有很多实现,那么使用这种方法进行注册就很麻烦。因此,我正在寻找一种注册方法,以名称在给定的程序集中注册IProcess的所有实现。注册密钥应使用的名称仅为类名称。

有人可以提示我在哪里找吗?

1 个答案:

答案 0 :(得分:1)

听起来像convention registration API的经典场景。

container.Register(
    Classes.FromThisAssembly()
        .BasedOn<IProcess>()
        .WithServiceBase()
        // and then if you *really* need to explicitly control naming
        .Configure(c => c.Named(c.Implementation.Name)
        )

通常不应该显式地命名组件,除非您有多个具有相同实现类的组件,所以只需确保您确实需要它们。

相关问题