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 independent of ObjectFactory

More dribs and drabs of StructureMap 2.5 documentation.  At the pace I'm on, I do believe that Duke Nukem Forever will be released tomorrow night. Knock on wood.

New to 2.5 is the ability to create an instance of the underlying Container class independent of the static ObjectFactory class (actually, you always could do this, but it's much easier in 2.5).  The IContainer interface has roughly the same methods as ObjectFactory (the only major difference is that ObjectFactory.GetNamedInstance<T>(name) is IContainer.GetInstance<T>(name) )  You can build a Container with a single Registry object:

 

            var container = new Container(new BasicActionRegistry());

 

Or more commonly, you can build the Container in a manner very similar to the ObjectFactory.Initialize() method.  One of the constructors for Container takes in an Action<ConfigurationExpression> expression that can be used to configure the Container in one atomic action like this code below:

 

            var container = new Container(x =>

            {

                x.Scan(scanner =>

                {

                    scanner.Assembly("StructureMap.Testing.Widget")

                });

 

                x.ForRequestedType<Rule>().TheDefault.Is.OfConcreteType<ColorRule>()

                    .WithCtorArg("Color").EqualTo("Blue");

 

 

                x.IncludeConfigurationFromConfigFile = true;

 

                x.AddConfigurationFromXmlFile("ExternalFile.xml");

            });

 

The ConfigurationExpression class is a Registry, so you can use all of the Registry DSL in the Lambda expression passed into the Container constructor.  In addition to the Registry DSL, you also have these options:

 

 

        bool IncludeConfigurationFromConfigFile { set; }

        void AddRegistry<T>() where T : Registry, new();

        void AddRegistry(Registry registry);

        void AddConfigurationFromXmlFile(string fileName);

        void AddConfigurationFromNode(XmlNode node);

 

 

These other options will allow you to add configuration from additional Registry classes, the StructureMap section in the App.config file, or other Xml files.

 

Lastly, you can create a Container directly with a no argument constructor, then use the Inject() methods or later use the Configure() method.

 

        [Test]

        public void Add_an_assembly_on_the_fly_and_pick_up_plugins2()

        {

            var container = new Container();

            container.Configure(

                registry =>

                {

                    registry.Scan(x =>

                    {

                        x.AssemblyContainingType(typeof (IService<>));

                        x.AddAllTypesOf(typeof (IService<>));

                    });

                }

                );

 

            IList<IService<string>> instances = container.GetAllInstances<IService<string>>();

            instances.Count.ShouldBeGreaterThan(0);

        }


Posted Sat, Oct 18 2008 9:05 PM by Jeremy D. Miller
Filed under:

[Advertisement]

Comments

Ben Hart wrote Dependency Inversion for Dummies: Unity and StructureMap
on Thu, Nov 20 2008 3:56 PM

Dependency Inversion for Dummies: Unity and StructureMap

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

Add a Comment

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