Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

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 StructureMap 2.5 to scan all assemblies in a folder

In a magical confluence of events, I’m free from all parental and spousal responsibilities today and I’m trying to use the day to write StructureMap docs.  Hopefully, this is the first post of several today.

This is the very last (or next to last) feature to make it into the StructureMap 2.5 release that Josh added last week to support an extensibility need that we have at Dovetail.  From the documentation:

StructureMap 2.5 has a brand new capability to auto register types in all assemblies in a given folder path.  My current project is using this feature for our extensibility mechanism.  For customer-specific deployments, we need to add business rules and even all new screens and features to our application that can be discovered at runtime -- without changing our core code.  Our design calls for all extensions to be created in separate assemblies.  On application startup, our system will look for all assemblies in a well known folder and use StructureMap to scan these assemblies for the extensions that will be specified in Registry classes.  Our bootstrapping looks like:

            Scan(x =>

            {

                // I'm telling StructureMap to sweep a folder called "Extensions" directly

                // underneath the application root folder for any assemblies found in that folder

                x.AssembliesFromPath("Extensions");

 

                // I also direct StructureMap to add any Registries that it finds in these

                // assemblies.  I'm assuming that all the StructureMap directives are

                // contained in Registry classes -- and this is the recommended approach

                x.LookForRegistries();

            });

Note in the code above that I made an explicit call to "LookForRegistries."  Scanning for Registry's is no longer active by default.  This is a breaking change from the 2.4.9 preview release. 

You can also filter the assemblies based on a Predicate like this:

            Scan(x =>

            {

                // This time I'm going to specify a filter on the assembly such that

                // only assemblies that have "Extension" in their name will be scanned

                x.AssembliesFromPath("Extensions", assembly => assembly.GetName().Name.Contains("Extension"));

 

                x.LookForRegistries();

            });


Posted Sat, Oct 18 2008 11:44 AM by Jeremy D. Miller

[Advertisement]

Comments

Steven Harman wrote re: Using StructureMap 2.5 to scan all assemblies in a folder
on Sat, Oct 18 2008 10:30 PM

Curious why scanning an assembly for Registries is no longer the default behavior? Especially since registries are the preferred (both IMO and according to you) way to wire things up... seems like looking for Registries should be the default and looking for attributes should be opt-in.

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

Khalid Abuhakmeh wrote re: Using StructureMap 2.5 to scan all assemblies in a folder
on Thu, Jan 22 2009 9:05 AM

that's wicked! Now I'm having a harder time picking between MEF and StructureMap. I guess I need more time evaluating both frameworks.

monstersgotmy.net/.../Another-Crazy-Idea-Persistence-as-an-Aspect.aspx

Jeremy D. Miller wrote re: Using StructureMap 2.5 to scan all assemblies in a folder
on Thu, Jan 22 2009 9:33 AM

@Khalid,

You do remember the part about MEF not being complete right?  

Khalid Abuhakmeh wrote re: Using StructureMap 2.5 to scan all assemblies in a folder
on Thu, Jan 22 2009 9:39 AM

Yeah, it is still in Preview 3. I see promise in it, but I am worried that there is a boogie man or two in there. I can hear the managers now "What do you mean it's not in release?"

I know you have a slight bias towards StructureMap; but which one would you recommend and why?

I am attempting to build a plugin architecture that can change at runtime. Very flexible and fluid.

Eric Colman wrote re: Using StructureMap 2.5 to scan all assemblies in a folder
on Mon, Feb 2 2009 7:57 PM

Hey Jeremy,

I wanted to be able to drop an assembly in a folder and have StructureMap pick it up and allow me to use the added implementations I've put in the DLL.  In my extension implementation DLL, I have the implementation and a registry class.  For some reason, I can't get StructureMap to read in my registry class from the external DLL.  It's finding the DLL just fine;  It's a pretty straightforward registry as well:

   public class ProductMockRegistry : Registry

   {

       public ProductMockRegistry()

       {

           ForRequestedType<IProductRepository>().AddInstances(instances =>

               instances.OfConcreteType<ProductMockRepository>().WithName("ProductMock")

           );            

       }

   }

I'm using:

assembly.AssembliesFromPath("extensions", addedAssembly => addedAssembly.GetName().Name.Contains("Extension"));

assembly.LookForRegistries();

to reference the DLL and use it's registries...but it doesn't find it.  Any idea?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?