Programmatic configuration to XML

Jun 11, 2009 at 8:17 AM


I just started using Unity in my layered project in order to achieve loose coupling; works like a charm, yet there's one problem I can't fix.

I understood Unity should be configured in UI, either via web.config/app.config or programmatically. I preferred programmatic configuration to XML because of type-safety, so I ended up adding references to lower layers in UI for the sole purpose of configuring Unity. Here'a a quick look at my configuration:

public interface IThing { }

public class Thing : IThing { }


public sealed class UnityDependencyResolver : IDependencyResolver


    private IUnityContainer container;


    private UnityDependencyResolver()


        container = new UnityContainer()

            .RegisterType<IThing, Thing>();





The thing is I'm not quite happy with coupling UI with lower layers, but I'm not happy either with writing and maintaing XML configs...So I want the best of both: generate XML based on the programmatic configuration; then I can safely remove any references to lower layers and achieve loose coupling. I think I can use reflection on container and generate at least a simple configuration.

Before I actually start this... is there any easier way? Or if you consider the whole idea is wrong, could you provide some advice to a beginner?

Thank you!

Jun 12, 2009 at 11:28 AM

You may want to take a quick look at the Prism/CAL project here on codeplex even if you are not using WPF for your UI.  It allows for the registration to be done in multiple steps/layers.

The first layer configures and loads what they call Modules.  Then, each Module registers the views it contains.  The module is usually coupled to the views anyway, so it is okay that it knows about its level.  The top layer just knows which modules to load/resolve.  Nothing about the views within it.

The Smart Client Guidance for Winforms has similar patterns