当前位置:  开发笔记 > 编程语言 > 正文

Microsoft Unity - xml代码

如何解决《MicrosoftUnity-xml代码》经验,为你挑选了1个好方法。

有人可以提供我应该与企业库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();
            container.RegisterType();
            container.Configure().SetInterceptorFor(new InterfaceInterceptor());

            var logger = container.Resolve();
            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();
    container.RegisterType();
    container.Configure().SetInterceptorFor(new InterfaceInterceptor());

我会这样的:

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

使用XML配置文件.



1> Matt Ruwe..:

我自己找到了答案:



  
    

再次,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(); 
            //container.RegisterType(); 
            //container.Configure().SetInterceptorFor(new InterfaceInterceptor()); 

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


            var logger = container.Resolve(); 
            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);
        }
    }
}

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有