10 November 2008

Container Configuration

by mo

In my last post I briefly mentioned how we were wiring some components in to our container. The syntax looked like the following:

  new ReportPresenterTaskConfiguration(),
  new ReportPresenterTask(

We’re using Castle Windsor under the hood, but we have an abstraction over it that allows us to configure it as we like. Even switching the underlying implementation. Which we did, from our hand rolled container to Castle Windsor. The implementation of the above method looks as follows:

  public void AddProxyOf<Interface, Target>(IProxyConfiguration<Interface> configuration, Target instance) where Target : Interface
    var builder = new ProxyBuilder<Interface>();

Wikipedia defines the Proxy design pattern as

A proxy, in its most general form, is a class functioning as an interface to another thing.

The AddProxyOf method creates an instance of a proxybuilder. It then passes it to the configuration to allow it to configure the proxy builder before building the proxy. Then it registers the proxy as a singleton in to the container.

  public interface IConfiguration<T>
    void Configure(T item_to_configure);

  public interface IProxyConfiguration<T> : IConfiguration<IProxyBuilder<T>>

In this case the proxy configuration looks like:

  public class ReportPresenterTaskConfiguration : IProxyConfiguration<IReportPresenterTask>
    public void Configure(IProxyBuilder<IReportPresenterTask> builder)
      var constraint = builder.AddInterceptor<DisplayProgressBarInterceptor>();

This adds a progress bar interceptor, that displays a progress bar as the report is getting generated via the RetrieveAuditReport method on the IReportPresenterTask.

csharp designpatterns