Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Setup up StructureMap 2.5 to Auto Configure your dependencies

One of the great new features with StructureMap 2.5 is the ability to have the container auto-wire you dependencies.  This means that if you want to you do not need to register all your types.

Prior to this release if you wanted to map IFoo to Foo you needed to do something like this:

StructureMapConfiguration.ForRequestedType<IFoo>().TheDefaultIsConcreteType<Foo>();

Although this was not a major pain or even a major issue to me, I would love the ability to not have to create TONS of lines of wasted code simply to wire my interfaces to my concretes.

With 2.5 you can do the following and have the container do all the grunt work for you.

ObjectFactory.Initialize(
    x =>
        {
            x.Scan( scanner =>
                        {
                            scanner.TheCallingAssembly();
                            scanner.WithDefaultConventions();
                        } );
        }
    );

 

You will notice that I am using the ObjectFactory.Initialize all my stuff.  This is because StructureMapConfiguration has officially been deprecated and SHOULD not be used.  Also pay close attention to the way I am using x.Scan().  In my initial testing I was having NO luck getting the auto wiring to work because I was doing this.

ObjectFactory.Initialize(
    x =>
        {
            x.Scan( scanner =>
                    {
                        scanner.TheCallingAssembly();
                    } );

            x.Scan( scanner =>
                    {
                        scanner.WithDefaultConventions();
                    } );
        }
    );

I am not 100% sure of the reason why, but I think it is because that each time Scan is called the list of internal 'scanners' gets reset (was walking through the code).

Also, keep in mind this will only auto wire your dependencies if the naming is as follows IFoo maps to Foo.

Hope this helps,

Till next time,

[----- Remember to check out DimeCasts.Net -----]



Comments

Jeremy D. Miller said:

Each call to Scan() is a completely atomic action now instead of being additive.  I had to make the change in the scanning model to support our project at work, and that's obviously priority number 1 ;)

# October 13, 2008 2:17 PM

Frank Mao said:

Have you tried use this auto-wire inside a registry? I found moving this code into the registry then auto-wiring doesn't work anymore.

# November 19, 2008 11:22 AM

Auto-wire in StructureMap 2.5 « maonet technotes said:

Pingback from  Auto-wire in StructureMap 2.5 &laquo; maonet technotes

# November 19, 2008 11:37 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News