3

有人可以提供我应该与 Enterprise Library 4.1 中的 Microsoft Unity 应用程序块一起使用的 XML 配置来实现与以下相同的结果吗?

    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);
        }

    }

}

所以代替这个:

    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.Default.Configure(container);

使用 XML 配置文件。

4

3 回答 3

7

我自己找到了答案:

<?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>

再一次,C# 代码:

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);
        }
    }
}
于 2009-03-18T16:31:07.967 回答
0

这可以在没有 Test 属性的情况下完成吗?纯粹来自 XML。只需指定要拦截的接口,它将拦截所有或匹配的方法?

于 2010-04-07T15:57:58.573 回答
0

我使用 Unity 2 编写了一个示例,它与之前的答案非常相似。可以在以下位置进行检查:

http://hmadrigal.wordpress.com/2010/12/25/aspect-orienting-programming-and-interceptor-design-pattern-with-unity-2/

最好的问候,赫伯

于 2010-12-25T13:19:29.697 回答