Inject mappings automatically

Jun 15, 2012 at 12:29 PM

Hi, we have been using Unity in the azure environment and have written our ObjectBuilder wrapper class to traverse the executing directory and find all dependencies in the assemblise it finds. It does this by inspecting the classes in each assembly for one that implements IObjectBuilderInitialiser and invokes a Register(IObjectBuilder objectBuilder) method which registers all the dependencies in that assembly.

It works really well, in the azure environment as there is no GAC (that we install in) and everything the application needs is in the same directory. However, now we come back to more conventional development we are again working with the GAC.

Our ObjectBuilder wrapper for Unity is now in the GAC, meaning that when it initialises - it will not find any of the applications assemblies and therefore not find any dependencies.

We've taken this approach as we were finding the .config unity section becoming unwieldy.

Anyway, is there an approach we can take with Unity in this respect that will allow the dependencies of an application to be discovered? Don't pay too much heed to our original approach at the top - we are open to all suggestions.


Jun 15, 2012 at 12:38 PM
Edited Jun 15, 2012 at 12:38 PM

Have a look at the TecX project here on CodePlex. It contains a port of the StructureMap config engine that also supports various conventions like "register all implementations of IFoo that you can find in the application base directory". And its easy to modularize your configuration by deriving classes from ConfigurationBuilder, put them inside your modules and load them all from a bootstrapper (see the code in TecX.Unity.Configuration.Plugins and the associated tests).

Jun 21, 2012 at 12:00 PM

Hi weberse, thanks for the response and apologies for the delay in picking this back up.

I've had a read through the introduction to the TecX framework, but need to pick your brains on an aspect that I alluded to in my original post. The summary has this line in it:

"It searches all assemblies in the current application base directory for classes that inherit from ConfigurationBuilder. "

This very similar to our approach that we took, however we were usurped by the fact that our unity implementation (ObjectBuilder) lives in the GAC in a Common library, not in the application directory. Does your framework assume the the process is being instantiated by an object in the executing directory?

The problem we have is that a lot of implementations we need to automatically discover are located in the GAC and not necessarily in this application directory.

Can TecX cater for this?


Jun 21, 2012 at 12:25 PM

Hi @ChubbyArse (nice nickname btw ;)),

you can always use an overload of the AssemblyScanner.AssembliesFromPath() method to specify where the ConfigurationBuilder should look for assemblies.

The code would look something like that:

var builder = new ConfigurationBuilder();
builder.Scan(s => 

Does that help?

Mar 12, 2013 at 11:48 PM

Out of curiosity, why would you want to scan the entire GAC?
Especially, if you have a situation with different version of the same assembly in the GAC?

If you know which assembly from the GAC you want to be scanned, then you can load them and provide a list of assemblies.

It would help to know a concerete example of how you see this working wth the GAC.

By the way, we are adding the registration by convention feature in Unity 3.0. See this.