AddAllTypesOf 与 ConnectImplementationsToTypesClosing

AddAllTypesOf vs ConnectImplementationsToTypesClosing

我很好奇这两种方法之间的区别。我正在实现一个带有开放泛型的装饰器模式,无论我使用 AddAllTypesOf 还是 ConnectImplementationsToTypesClosing 都没有关系,我得到了相同的功能。

public class CommandRegistry : Registry 
    {
        public CommandRegistry()
        {

            For<CommandProcessor>().Use<DefaultCommandProcessor>().Transient();

            Scan(scanner =>
            {
                scanner.AssemblyContainingType<SaveCoolCommandHandler>();                    

                //scanner.AddAllTypesOf(typeof(CommandHandler<>));
                //scanner.AddAllTypesOf(typeof(IValidator<>));
                //scanner.AddAllTypesOf(typeof(LogMehh<>));

                scanner.ConnectImplementationsToTypesClosing(typeof(CommandHandler<>));
                scanner.ConnectImplementationsToTypesClosing(typeof(IValidator<>));
                scanner.ConnectImplementationsToTypesClosing(typeof(LogMehh<>));
            });

            var handlerType = For(typeof(CommandHandler<>));

            handlerType.DecorateAllWith(typeof(CommandValidator<>)); //Second
            handlerType.DecorateAllWith(typeof(CommandLogger<>)); //First

          //  ObjectFactory.WhatDoIHave();
        }
    } 

无论我选择哪种方法,对 ObjectFactory.WhatDoIHave() 的调用也会给我相同的结果。

我查看了源代码,这些方法肯定在做不同的事情,我只是无法确定到底有什么区别。当一个人比另一个人更受欢迎时,是否有任何指导方针或场景?

警告:我已经好几年没有在商业项目中使用 StructureMap 了。从那以后事情可能发生了变化,但您的示例代码看起来非常熟悉,所以我猜它没有太大变化。

我知道您希望在哪些方面偏爱另一个的唯一原因是当您想要明确定义将用于将具体实现映射到 T 的约定时.两者都可以做到,但实现的稳健性不同。

如果您使用 ConnectImplementationsToTypesClosing<T>,在 Scan() 设置过程中,您将传入继承自 IRegistrationConvention 的约定 class。至少对我来说,它没有任何麻烦。

AddAllTypesOf<T> 应该通过 ITypeScanner 具有类似的功能,但实际上我们遇到了各种奇怪的问题,例如重复类型注册,如果在与 [=10 不同的命名空间中,则类型不会被注册=],并且经常找不到他们应该找到的具体实现。使用 ConnectImplementationsToTypesClosing<T>.

时这些问题都消失了

如果您不想做任何太聪明的事情并且默认约定适合您,您应该注意到两者之间没有区别。如果您出于任何原因需要覆盖默认约定,我会强烈支持 ConnectImplementationsToTypesClosing<T>.