Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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>.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!