Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

News

Advertisement

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
Using the StructureMap Container independently of ObjectFactory

From the StructureMap list I saw a link to an .Net IoC tool roundup that included some mildly inaccurate comments about StructureMap (which I’ll blame on lack o’ documentation for SM 2.5, it’s on my plate, I swear!).  Specifically, the author states:

With StructureMap, it was extremely hard to test it correctly, because it uses a static class for container/configuration and I had to reset it between tests.

Ok, the ReInitialize() business is a mess and needs to be documented (but you shouldn’t really ever have to use it in normal usage).  However, you don’t *have* to use the static ObjectFactory and StructureMapConfiguration if you don’t want to.  They’re strictly a convenience method and wrappers about two classes called Container (the actual Container) and Registry (for configuration).  You can set up a Container independently of ObjectFactory by doing this:

 

            IContainer container = new Container(registry =>

            {

                registry.ScanAssemblies().IncludeAssemblyContainingType<ColorWidget>();

 

                // Add an instance with properties

                registry.AddInstanceOf<IWidget>()

                    .UsingConcreteType<ColorWidget>()

                    .WithName("DarkGreen")

                    .WithProperty("color").EqualTo("DarkGreen");

 

                // Add an instance by specifying the ConcreteKey

                registry.AddInstanceOf<IWidget>()

                    .UsingConcreteType<ColorWidget>()

                    .WithName("Purple")

                    .WithProperty("color").EqualTo("Purple");

 

                // Pull a property from the App config

                registry.AddInstanceOf<IWidget>()

                    .UsingConcreteType<ColorWidget>()

                    .WithName("AppSetting")

                    .WithProperty("color").EqualToAppSetting("Color");

 

 

                registry.AddInstanceOf<IWidget>().UsingConcreteType<AWidget>();

            });

In the constructor function of Container up above takes in a single argument of Action<Registry>.  In that action you simply make any or all the configuration against that Registry object being passed in.  The IContainer interface has all the normal “GetInstance<T>()” type methods. 

        [Test]

        public void AddInstanceAndOverrideTheConcreteTypeForADependency()

        {

            IContainer container = new Container(

                registry => registry.AddInstanceOf<Rule>().UsingConcreteType<WidgetRule>()

                    .WithName("AWidgetRule")

                    .Child<IWidget>().IsConcreteType<AWidget>());

 

            container.GetInstance<Rule>("AWidgetRule")

                .IsType<WidgetRule>()

                .Widget.IsType<AWidget>();

        }

The author also criticizes StructureMap for not supporting the concept of hierarchical containers.  StructureMap does NOT do hierarchical containers the same way as the containers that were influenced by PicoContainer & Avalon containers from Java, but there is an analogue with the “Profile” feature.  I’ll get to that in the next post…


Posted Wed, Sep 10 2008 10:30 AM by Jeremy D. Miller

[Advertisement]

Comments

Dew Drop - September 11, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - September 11, 2008 | Alvin Ashcraft's Morning Dew
on Thu, Sep 11 2008 9:07 AM

Pingback from  Dew Drop - September 11, 2008 | Alvin Ashcraft's Morning Dew

Comparing .NET DI (IoC) Frameworks, Part 2 » Blog Archive » I Think It’s Interesting wrote Comparing .NET DI (IoC) Frameworks, Part 2 &raquo; Blog Archive &raquo; I Think It&#8217;s Interesting
on Fri, Sep 12 2008 12:22 PM

Pingback from  Comparing .NET DI (IoC) Frameworks, Part 2 &raquo;   Blog Archive &raquo;  I Think It&#8217;s Interesting

Andrey Shchekin wrote re: Using the StructureMap Container independently of ObjectFactory
on Fri, Sep 12 2008 12:32 PM

Thanks, I have fixed the post.

I was not really criticizing absense of _hierarchical_ containers, just not being able to keep several independent instances, which is not relevant anymore.

Jeremy D. Miller -- The Shade Tree Developer wrote A Gentle Quickstart for StructureMap 2.5
on Sun, Nov 30 2008 10:56 PM

The most general question I get with StructureMap is “how do I get started?” Personally, I’d recommend

Community Blogs wrote A Gentle Quickstart for StructureMap 2.5
on Sun, Nov 30 2008 11:48 PM

The most general question I get with StructureMap is “how do I get started?” Personally, I’d recommend

A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas wrote A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas
on Tue, Dec 2 2008 10:16 AM

Pingback from  A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas

A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas wrote A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas
on Wed, Dec 3 2008 11:04 AM

Pingback from  A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas

A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas wrote A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas
on Thu, Dec 4 2008 11:09 AM

Pingback from  A Gentle Quickstart for StructureMap 2.5 - taccato! trend tracker, cool hunting, new business ideas

Janus007 wrote re: Using the StructureMap Container independently of ObjectFactory
on Thu, Mar 5 2009 7:20 AM

Ohh so you think the documentaion is lacking *LOL* surprise surprise...

Well... I really like SM, but I must agree it's a tough one to get in the first round.

Jeremy D. Miller wrote re: Using the StructureMap Container independently of ObjectFactory
on Thu, Mar 5 2009 9:57 AM

@Janus007 ,

Documentation is useless unless you the reader actually pay attention to what it's saying.

ASP.NET 3.5 Social Networking: wrote StructureMap users - great article (Using the StructureMap Container independently of ObjectFactory)
on Fri, Apr 3 2009 1:40 PM

StructureMap users - great article (Using the StructureMap Container independently of ObjectFactory)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us