覆盖温莎城堡中的组件注册?哪个容器支持它? [英] Overriding a component registration in Castle Windsor? Which container supports it?

查看:62
本文介绍了覆盖温莎城堡中的组件注册?哪个容器支持它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分是对尚未回答的同一问题的重复。请参阅此处:如何覆盖在Castle中注册的组件温莎?

this is partially a duplication of the same question which has not been yet answered. See here: How can I override a component registered in Castle Windsor?

由于我无法评论或发布现有问题的任何答案,因此我再次创建了此问题,以希望有人知道看似基本的答案和简单的问题。

Since I cannot comment or post any answers to an existing questions I created this question again in the hope that somebody knows the answer to a seemingly basic and simple question.

请记住:


  1. 我不想要创建一个新容器。

  2. 我不在乎容器是否不应该用于单元测试。

  3. 我不想使用派生容器

如果Castle Windsor无法提供此简单功能,您会建议采用哪种其他容器实现?

If Castle Windsor is not able to provide this simple functionality, what other container implementation would you recommend?

推荐答案

Windsor遵循最后一次注册获胜的约定。但是,如果您没有明确告诉它该组件将被覆盖,它将抛出异常。因此,有两种方法可以覆盖现有组件:

Windsor works with convention that "last registration wins". But, if you have not SPECIFICALLY told it that this component will be overridden, it will throw an exception. So, there are 2 ways to allow existing component to be be overridden:


  1. 使用注册组件。IsDefault() 。这将覆盖现有注册。

  2. 使用 .IsFallback()注册组件。

  3. 使用组件的唯一名称- .Named( NewComponentName)
  4. >
  1. Register component with .IsDefault(). This will override existing registration.
  2. Register component with .IsFallback(). This will allow component to be overridden later.
  3. Using unique name for component - .Named("NewComponentName").

我个人更喜欢 .IsDefault()并在集成测试中使用以下简写扩展名:

I personally prefer .IsDefault() and use this shorthand extension in my integration tests:

    public static class WindsorContainerExtensions
    {            
        public static void Override<TService>(this IWindsorContainer container, TService instance) where TService : class
        {
            container.Register(Component.For<TService>().Instance(instance).IsDefault());
        }
    }

这篇关于覆盖温莎城堡中的组件注册?哪个容器支持它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆