views:

169

answers:

2

Background: I wanted to distribute a library to some other developers (something along the lines of an SDK or toolkit abstracting some internal web services). I'm using Autofac in order to inject different versions of the underlying services, mostly for stubbing. I want to distribute this assembly to a wide range of developers: users that understand IoC, and users that don't.

For any savvy developers, they can create their own implementations to be injected. Otherwise, a dev should be able to use the assembly w/o config and never know or care IoC is happening under the hood.

Fun stuff: I can provide Xml configuration to Autofac, or I can programatically configure components (the latter being the preferred method). In this case, I won't know what's been configured until run-time, hence the possible need for Xml config. Is there a way to make Autofac use the Xml configuration if it exists, else ignore it if none has been provided?

Note: I can implement this by hand by inspecting configuration for a specific section ("autofac"); if it's not there, then don't call

builder.RegisterModule(new ConfigurationSettingsReader());

Is there a better way to do this than inspecting the configuration manually (preferably something magical hidden within Autofac, as it seems to be able to do anything you ask of it!)

+1  A: 

The only thing I can see on autofac is this page on the wiki: http://code.google.com/p/autofac/wiki/StructuringWithModules And you're basically doing that already. Just run XML registration after all the other registrations.

Your programmatic check should be fine, but I wonder if the configuration settings reader can automatically pick up anything with that uses the specific type of "Autofac.Configuration.SectionHandler, Autofac". If it can pick that up automatically I would just check to see if trying ti read from configuration settings when nothing is defined will crash your application.

Min
It breaks w/o config. You get the argument exception "configuration section 'XXX' could not be read' error, hence the question.
joshua.ewer
I actually wonder if you would rather use MEF or some other kind of registration mechanism in order to allow users to extend your library.
Min
Just a note in hindsight: we ended up scrapping this portion. IoC ended up being a bit too far of a reach for our audience. If we end up doing something similar though, you're right. MEF would definitely be a solid approach here.
joshua.ewer
+1  A: 

To check whether your config have a specific section you should use the ConfigurationManager.OpenExeConfiguration. It will give you a Configuration object and from there inspect the Sections property to figure out if there is a section of the Autofac section handler type.

Peter Lillevold