WPF之Unity与ServiceLocator运用

Posted qcst123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF之Unity与ServiceLocator运用相关的知识,希望对你有一定的参考价值。

 以下记录unity和serviceLocator在WPF中的运用。unity主要通过配置文件的方式,分别展示了无参和传参两种形式,并对不同的生命周期对象进行了对比。下图是程序的结构及正文部分:

 技术图片

 

Bll.cs

  public class Bll : IBll
    {
        public void GetStr()
        {
            Console.WriteLine("this is 111...");
        }
    }

    public class Bll2 : IBll2
    {
        public void GetStr(string a)
        {
            Console.WriteLine(string.Format("this is {0}...", a));
        }
    }

    public class Bll3 : IBll3
    {
        private string b;

        public Bll3(string aa)
        {
            b = aa;
        }
        public void GetStr()
        {
            Console.WriteLine(string.Format("this is {0}...", b));
        }
    }

    public class DAL : IDAL
    {
        public void GetStr()
        {
            Console.WriteLine("this is DAL111...");
        }
    }

    public class Bll4 : IBll4
    {
        private IDAL _DAL;
        public Bll4(IDAL dal)
        {
            _DAL = dal;
        }

        public void GetStrBll4()
        {
            _DAL.GetStr();
        }
    }

 

IBll.cs

  public interface IBll
    {
        void GetStr();
    }

    public interface IBll2
    {
        void GetStr(string a);
    }

    public interface IBll3
    {
        void GetStr();
    }

    public interface IBll4
    {
        void GetStrBll4();
       
    }

    public interface IDAL
    {
        void GetStr();
    }

 

aa.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container name="MyContainer">
      <!--多实例-->
      <register type="UnityTest.Interface.IBll,UnityTest"
                mapTo="UnityTest.BllClass.Bll,UnityTest">
      </register>
      
       <!--单例-->
      <register type="UnityTest.Interface.IBll2,UnityTest"
            mapTo="UnityTest.BllClass.Bll2,UnityTest">
        <lifetime type="singleton" />
      </register>

        <!--带参数-->
      <register type="UnityTest.Interface.IBll3,UnityTest"
          mapTo="UnityTest.BllClass.Bll3,UnityTest">
        <lifetime type="singleton" />
        <constructor>
          <param name="aa" type="System.String" value="333">
          </param>
        </constructor>
      </register>
      
      <register type="UnityTest.Interface.IBll4,UnityTest"
                mapTo="UnityTest.BllClass.Bll4,UnityTest">
      </register>

  <register type="UnityTest.Interface.IDAL,UnityTest"
                mapTo="UnityTest.BllClass.DAL,UnityTest">
      </register>

    </container>
  </unity>
</configuration>

 

Ioc.cs

public static class Ioc
    {
        private static IUnityContainer container;
        static Ioc()
        {
            container = new UnityContainer();
            //采用独立配置,指定映射的配置文件
            var map = new ExeConfigurationFileMap { ExeConfigFilename = "aa.config" };
            //读取配置信息
            var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            //获取指定名称的配置节点
            var section = (UnityConfigurationSection)config.GetSection("unity");
            section.Configure(container, "MyContainer");

            //为使用ServiceLocator模式提供provider
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocatorAdapter(container));
        }

        public static T R<T>()
        {
            return R<T>(null);
        }

        public static T R<T>(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return container.Resolve<T>();
            }
            return container.Resolve<T>(name);
        }
    }

 

Program.cs

  class Program
    {
        static void Main(string[] args)
        {
            //默认为多实例
            IBll x = Ioc.R<IBll>();
            x.GetStr();
            IBll x2 = Ioc.R<IBll>();
            x2.GetStr();
            var str = x == x2 ? "same" : "different";
            Console.WriteLine(string.Format("x and x2 is {0} object!", str));

            //单例
            IBll2 y = Ioc.R<IBll2>();
            y.GetStr("222");
            IBll2 y2 = Ioc.R<IBll2>();
            y2.GetStr("222");
            var str2 = y == y2 ? "same" : "different";
            Console.WriteLine(string.Format("y and y2 is {0} object!", str2));

            //结构方法带参数
            IBll3 z = Ioc.R<IBll3>();
            z.GetStr();

            //引用ServiceLocator模式
            var bll = ServiceLocator.Current.GetInstance<IBll3>();
            bll.GetStr();

            //依赖倒置
            IBll4 a = ServiceLocator.Current.GetInstance<IBll4>();
            a.GetStrBll4();
           
            Console.ReadKey();
        }
    }

 

以上是关于WPF之Unity与ServiceLocator运用的主要内容,如果未能解决你的问题,请参考以下文章

将 Unity 中的 XAML (WPF) 应用与 MixedRealityToolkit 相结合

ServiceLocator 是反模式吗?

如何使用ZF3设置延迟加载(从任何地方都没有ServiceLocator模式)

心跳包 WPF Unity 数据库搭建

使用 Prism Unity 引导程序创建 WPF 应用程序

yii2源码分析之组件实例化流程