Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddAllTypesOf vs ConnectImplementationsToTypesClosing

I'm curious as to the difference between these two methods. I'm implementing a decorator pattern with open generics and whether I use AddAllTypesOf or ConnectImplementationsToTypesClosing it doesn't matter, I get the same functionality.

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();
        }
    } 

The call to ObjectFactory.WhatDoIHave() also gives me the same results no matter which method I choose.

I've looked at the source code and these methods are definately doing different things, I just haven't been able to determine exactly what the difference is. Are there any guidelines or scenarios when one is preferred over the other?

like image 333
CSharper Avatar asked Jun 30 '15 20:06

CSharper


1 Answers

Caveat: I haven't used StructureMap in a commercial project for several years now. Things may have changed since then, but your example code looks completely familiar so I am guessing it hasn't changed much.

The only reason I'm aware of where you'll want to favour one over the other is when you want to explicitly define the convention(s) which will be used for mapping concrete implementations to T. Both can do it but the robustness of the implementation differs.

If you use ConnectImplementationsToTypesClosing<T>, during the Scan() setup you pass in a convention class which inherits from IRegistrationConvention. For me at least it just worked without any hassles.

AddAllTypesOf<T> supposedly has similar functionality through ITypeScanner but in practice we had all sorts of weird issues with it like duplicate type registrations, types not getting registered if in a different namespace from T, and often not finding the specific implementations they were supposed to. These problems all went away when using ConnectImplementationsToTypesClosing<T>.

If you aren't trying to do anything too clever and the default conventions work for you, you should notice no difference between the two. If you need to override the default conventions for any reason I would strongly favour ConnectImplementationsToTypesClosing<T>.

like image 133
nathanchere Avatar answered Nov 05 '22 14:11

nathanchere