This project is read-only.

Windsor Bootstrapper and Log4Net

Introduction

CompositeWPF comes uses Unity as default dependency injection container, but you can replace the default container and use other containers too. Here we see how to use Castle Windsor container and use Log4Net library as our logging mechanism.

Using Boostrapper

There is an abstract WindsorBootstrapper which you need to inherit from :

public class Bootstrapper : WindsorBootstrapper
{
    private IWindsorContainer container;

    public Bootstrapper() : this(new WindsorContainer())
    {
    }

    public Bootstrapper(IWindsorContainer container)
    {
        this.container = container;
    }

    protected override IWindsorContainer CreateContainer()
    {
        return container;
    }
}


After when interfacing with IWindsorContainer, you can use generic extension methods provided to you in WindsorContainerExtensions class :

using CompositeWPFContrib.Composite.WindsorExtensions;

[Singleton]
public class OrderService : IOrderService
{
    public void AcceptOrder(string orderName)
    {
        var serviceInstance = Container.TryResolve<IOrderProcessService>();
        serviceInstance.Process(orderName);
    }
}

Using Log4Net Logger

When using WindsorBootstrapper, an instance of ILoggerFacade implemented using Log4Net is already registered for you. To start logging, all you need is to configure Log4Net, either with configuration files or via code when starting the application, and later get an instance of ILoggerFacade, either via Constructor injection or Service Lookup, and perform the actual logging. Please note that, since Log4Net does not have a notion of Priorities and only supports Categories when logging, all the specified priorities will be ignored.

[Singleton]
public class OrderService : IOrderService
{
    private ILoggerFacade logger;
    private IWindsorContainer container;

    public OrderService(IWindsorContainer container, ILoggerFacade logger)
    {
        this.container = container;
        this.logger = logger;
    }

    public void AcceptOrder(string orderName)
    {
        logger.Log("Getting Order Process service", Category.Debug, Priority.High);
        var serviceInstance = container.TryResolve<IOrderProcessService>();

        logger.Log("Processing the order", Category.Info, Priority.Low);
        serviceInstance.Process(orderName);
    }
}

Using Windsor Adapter

When using CompositeWPF, there are a lot of times when you need to interact with the IoC container. To avoid coupling to specific container, you'd better stick to IContainerFacade implementation provided to you. This will help a lot in case you need to switch to another IoC container. When using WindsorBootstrapper an instance of IContainerFacade implemented using Windsor is registered for you. So, instead of using IWindsorContainer interface or implementation directly, it is advised to use IContainerFacade :

[Singleton]
public class OrderService : IOrderService
{
    private IContainerFacade container;

    public OrderService(IContainerFacade container)
    {
        this.container = container;
    }

    public void AcceptOrder(string orderName)
    {
        var serviceInstance = container.TryResolve(typeof(IOrderProcessService));
        serviceInstance.Process(orderName);
    }
}

Last edited Oct 4, 2008 at 10:16 AM by HEskandari, version 2

Comments

No comments yet.