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.
  • http://stevenharman.net Steven Harman

    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.

  • http://www.monstersgotmy.net Khalid Abuhakmeh

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

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

  • http://codebetter.com/blogs/jeremy.miller Jeremy D. Miller

    @Khalid,

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

  • http://www.monstersgotmy.net Khalid Abuhakmeh

    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

    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().AddInstances(instances =>
    instances.OfConcreteType ().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?

  • http://www.best-registrycleaner.net Best Registry Cleaner

    Our design calls for all extensions to be created in separate assemblies.