Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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 -----]


Posted 10-13-2008 12:47 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Jeremy D. Miller wrote re: Setup up StructureMap 2.5 to Auto Configure your dependencies
on 10-13-2008 2:17 PM

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

Frank Mao wrote re: Setup up StructureMap 2.5 to Auto Configure your dependencies
on 11-19-2008 11:22 AM

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.

Auto-wire in StructureMap 2.5 « maonet technotes wrote Auto-wire in StructureMap 2.5 &laquo; maonet technotes
on 11-19-2008 11:37 AM

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)