views:

1194

answers:

2

Can someone provide the XML configuration I should use with Microsoft Unity application block in the Enterprise Library 4.1 to achieve the same result as the following?

    using System;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;

    namespace ConsoleApplication1

    {

    class Program
    {

        static void Main(string[] args)
        {

            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();
            container.RegisterType<ILogger, Logger>();
            container.Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());

            var logger = container.Resolve<ILogger>();
            logger.Write("World.");
            Console.ReadKey();
        }

    }


    public interface ILogger
    {
        [Test]
        void Write(string message);
    }


    public class Logger : ILogger
    {
        public void Write(string message)
        {
            Console.Write(message);
        }

    }

    public class TestAttribute : HandlerAttribute
    {

        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new TestHandler();
        }

    }

    public class TestHandler : ICallHandler
    {

        public int Order { get; set; }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.Write("Hello, ");
            return getNext()(input, getNext);
        }

    }

}

So instead of this:

    IUnityContainer container = new UnityContainer();
    container.AddNewExtension<Interception>();
    container.RegisterType<ILogger, Logger>();
    container.Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());

I would have this:

        IUnityContainer container = new UnityContainer();
        UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        section.Containers.Default.Configure(container);

with an XML config file.

+2  A: 

Found the answer on my own:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>

  <unity>
    <typeAliases>
      <typeAlias alias="ILogger" type="ConsoleApplication1.ILogger, ConsoleApplication1" />
      <typeAlias alias="Logger" type="ConsoleApplication1.Logger, ConsoleApplication1" />
      <typeAlias alias="TestAttribute" type="ConsoleApplication1.TestAttribute, ConsoleApplication1" />
      <typeAlias alias="TestHandler" type="ConsoleApplication1.TestHandler, ConsoleApplication1" />
      <typeAlias alias="interface" type="Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor, Microsoft.Practices.Unity.Interception, Version=1.2.0.0" />
    </typeAliases>
    <containers>
      <container name="ConfigureInterceptorForType">
        <extensions>
          <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
        </extensions>
        <extensionConfig>
          <add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">
            <interceptors>
              <interceptor type="interface">
                <key type="ILogger"/>
              </interceptor>
            </interceptors>
          </add>
        </extensionConfig>
        <types>
          <type type="ILogger" mapTo="Logger" />
        </types>
      </container>
    </containers>
  </unity>
</configuration>

And again, the C# code:

using System;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //IUnityContainer container = new UnityContainer(); 
            //container.AddNewExtension<Interception>(); 
            //container.RegisterType<ILogger, Logger>(); 
            //container.Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor()); 

            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers["ConfigureInterceptorForType"].Configure(container);


            var logger = container.Resolve<ILogger>(); 
            logger.Write("World."); 
            Console.ReadKey();
        }
    } 

    public interface ILogger
    {
        [Test]        
        void Write(string message);
    } 

    public class Logger : ILogger
    {
        public void Write(string message)
        {
            Console.Write(message);
        }
    } 

    public class TestAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container) 
        { 
            return new TestHandler(); 
        }
    } 

    public class TestHandler : ICallHandler
    {
        public int Order { get; set; } 

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.Write("Hello, "); 
            return getNext()(input, getNext);
        }
    }
}
Matt Ruwe
A: 

Can this be done without the Test attribute? Purely from XML. Just specify the interface to intercept and it will intercept all or matching methods?

hpcd
You'll need to use a matching rule with a policy
Matt Ruwe