码迷,mamicode.com
首页 > 编程语言 > 详细

WPF之Unity与ServiceLocator运用

时间:2019-12-23 20:54:15      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:Locator   return   and   独立   tor   gis   mamicode   指定   schema   

 以下记录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运用

标签:Locator   return   and   独立   tor   gis   mamicode   指定   schema   

原文地址:https://www.cnblogs.com/qcst123/p/12084786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!