Welcome to Manicprogrammer Sign in | Join | Help

Stormwind Reports - Part 2 and MicroKernel Container

Now this is a project I'm rapidly becoming proud of. We are making huge progresses along the way.

I gotta thank Castle for their amazing Micro Kernel dependency injection container. It's helping us immensely. Let me share how we are using it so maybe it's a pattern people might wanna use.

We needed the dependency injection container for the following classes:

  • Data Broker  - so you can roll your own data broker.
  • Report Renderer - so you can render your report to several sources.
  • Filter Form Renderer - so you can render your filters the way you want too.

So, instead of just using the Micro Kernel, we created a class called StormwindContainer that contains a DefaultKernel inside.

This way I declared typed properties that map to the DefaultKernel.Resolve with the correct type. Here's the code for my container so far:

/// <summary>
/// Stormwind default dependency container.
/// </summary>
public class StormwindContainer {
    private DefaultKernel kernel;
 
    /// <summary>
    /// Default constructor.
    /// </summary>
    public StormwindContainer()
        : base() {
        kernel = new DefaultKernel();
        IConfiguration config = StormwindConfiguration.Current;
        Type tp = Type.GetType(config.DataBroker);
        if (tp == null) {
            throw new Faults.DataBrokerTypeNotFoundException(string.Format(ErrorMessages.DataBrokerNotFoundFaultMessage, 
                     config.DataBroker));
        }
        kernel.AddComponent("DataBroker", tp);
    }
 
    /// <summary>
    /// Configured Data broker.
    /// </summary>
    public IDataBroker DataBroker {
        get {
            return kernel.Resolve("DataBroker", new Dictionary<string,object>()) as IDataBroker;
        }
    }
 
    /// <summary>
    /// Stormwind Container instance.
    /// </summary>
    public static StormwindContainer Instance {
        get {
            return new StormwindContainer();
        }
    }
}

 Let me explain it.

In the constructor I initialize a new DefaultKernel with the components that the report user specified in their config file, like this:

<configuration>
  <configSections>
    <section name="stormwind" 
        type="Stormwind.Core.Configuration.StormwindConfiguration, 
                 Stormwind.Core" />
  </configSections>
  <stormwind dataBroker="Stormwind.Core.Data.ARDataBroker, 
                             Stormwind.Core">
  </stormwind>
</configuration>

This way the report user gets to specify each component of the Report Engine (so far just the Data Broker).

Now how does the Report Engine access the Injected Data Broker? By using the DataBroker property which is of type IDataBroker, just like this:

DataTable dt = StormwindContainer.Instance.DataBroker.GetData(def);

This is possible because when the engine accesses the Instance property a new Container is created, and when the DataBroker property is called, a DataBroker is obtained from the Micro Kernel container, as we can see here:

/// <summary>
/// Configured Data broker.
/// </summary>
public IDataBroker DataBroker {
    get {
        return kernel.Resolve("DataBroker", new Dictionary<string,object>()) as IDataBroker;
    }
}

 By calling the resolve method in the configured DefaultKernel, the Container automatically resolves for me the correct instance of the DataBroker. This is pure magic!

Hope it helps someone!

Gotta go back to development!

Til' next time!

#106

Published Tuesday, June 12, 2007 1:23 AM by heynemann

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments


Enter the text you see in the image:

Leave a Comment

(required) 
required 
(required)