Container Configuration Posted on November 10, 2008 @ 03:19 csharp, designpatterns, tools
In my last post I briefly mentioned how we were wiring some components in to our container. The syntax looked like the following:
1 container.AddProxyOf(
2 new ReportPresenterTaskConfiguration(),
3 new ReportPresenterTask(
4 Lazy.Load<IReportDocumentBuilder>(),
5 Lazy.Load<IApplicationSettings>())
6 );
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:
1 public void AddProxyOf<Interface, Target>(IProxyConfiguration<Interface> configuration, Target instance) where Target : Interface
2 {
3 var builder = new ProxyBuilder<Interface>();
4 configuration.Configure(builder);
5 AddInstanceOf(builder.CreateProxyFor(instance));
6 }
Wikipedia defines the Proxy design pattern as:
A proxy, in its most general form, is a class functioning as an interface to another thing.
To understand the ProxyBuilder
1 public interface IConfiguration<T>
2 {
3 void Configure(T item_to_configure);
4 }
5
6 public interface IProxyConfiguration<T> : IConfiguration<IProxyBuilder<T>>
7 {
8 }
In this case the proxy configuration looks like:
1 public class ReportPresenterTaskConfiguration : IProxyConfiguration<IReportPresenterTask>
2 {
3 public void Configure(IProxyBuilder<IReportPresenterTask> builder)
4 {
5 var constraint = builder.AddInterceptor<DisplayProgressBarInterceptor>();
6 constraint.InterceptOn.RetrieveAuditReport();
7 }
8 }
This guy adds a progress bar interceptor, that displays a progress bar as the report is getting generated via the "RetrieveAuditReport" method on the IReportPresenterTask.