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

            });

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.