先看看 IoC百度百科

优化过程

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //场景 某公司客服要回访一些客户
            //刚开始 回访的方式只有 打电话
            //简单的实现
            CustomerService1 cs1 = new CustomerService1();
            cs1.Call();
        }
    }
    public class CustomerService1
    {
        public string Call()
        {
            return "打电话";
        }
    }
}
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //后来又添加了发短信的回访方式
            //考虑到以后还可能增加回访方式
            //而且为了不改动Main函数代码 我们将封装回访方法
            //但是这样只是将回访方式将造成的改动下移到了 Customer2里面了
            //然后将回访方式的选择放到配置文件appconfig里面 用户可以设置默认的回访方式
            CustomerService2 cs2 = new CustomerService2();
            string Method = System.Configuration.ConfigurationManager.AppSettings["Method"];
            cs2.Visit(Method);
        }
    }
    public class CustomerService2
    {
        public string Visit(string Method)
        {
            string result = "Not Found";
            switch (Method)
            {
                case "Call":
                    result = Call();
                    break;
                case "SMS":
                    result = SMS();
                    break;
            }
            return result;
        }
        private string Call()
        {
            return "打电话";
        }
        private string SMS()
        {
            return "发短信";
        }
    }
}
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string Method = System.Configuration.ConfigurationManager.AppSettings["Method"];

            //下来我们继续减少功能变化造成的代码修改
            //如果增加功能虽然Main不用更改但是CustomerService2还是得改动
            //我们接下来继续优化CustomerService
            //见CustomerService3 内部注释 Main已经不需改动
            CustomerService3 cs3 = new CustomerService3();
            cs3.Visit(Method);
        }
    }
    public class CustomerService3
    {
        public string Visit(string Method)
        {
            //参照CustomerService2可以发现如果 添加回访方式 就必须在Visit的Switch里面加一个case 再加一功能方法
            //我们先分离这些方法
            //将他们接口化
            //可以发现方法的实现被抽象出来了
            //进一步用户swith请看 CustomerService4
            IServiceMethod servicemethod = null;
            switch (Method)
            {
                case "Call":
                    servicemethod = new CallMethod();
                    break;
                case "SMS":
                    servicemethod = new SMSMethod();
                    break;
            }
            return servicemethod.Vist();
        }
    }
    public class CallMethod
    {
        public string Vist()
        {
                 return "打电话";
        }
    }
    public class SMSMethod
    {
            public string Vist()
           {
                  return "发短信";
            }
     }
}    
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string Method = System.Configuration.ConfigurationManager.AppSettings["Method"];

            CustomerSerive4 cs4 = new CustomerSerive4();
            cs4.Visit(Method);
        }
    }

    public class CustomerSerive4
    {
        public string Visit(string Method)
        {
            //为了根据Method来实例化方法 并进行调用 我们需要引入 反射机制
            //根据我们原来的命令规范 Method参数+"Method"就是我们需要找的类 由于在我们在同一个程序集里面 所以可以直接用Test 后面考虑建一个映射配置来查找
            //这样我们就把回访方式的增加带来的代码的更改 从Main下放到了CustomerService 又从CustomerService下方到了IServiceMetod的实现类中
            //最后一步就是将IServiceMethod实现类的增加带来项目的重新生成dll的重新编译 重新发布问题了
            //请看 CustomerService5
            IServiceMethod servicemethod = Activator.CreateInstance("Test", Method + "Method") as IServiceMethod;
            return servicemethod.Vist();
        }
    }
       public interface IServiceMethod
       {
             string Vist();
       }
       public class CallMethod : IServiceMethod
       {
             public string Vist()
             {
                   return "打电话";
              }
       }
       public class SMSMethod : IServiceMethod
       {
            public string Vist()
            {
                return "发短信";
            }
        }
}
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string Method = System.Configuration.ConfigurationManager.AppSettings["Method"];

            CustomerService5 cs5 = new CustomerService5();
            cs5.Visit(Method);

        }
    }
    public class CustomerService5
    {
        public string Visit(string Method)
        {
            //解决重新编译的方法就是
            //1.将IServiceMetod接口独立成一个项目 另一个名称空间 使之独立存在 注意此处 IServiceMetod是TestInterface名称空间(TestInterface.dll里面)
            //2.将CallMethod、SMSMethod等IServiceMetod接口的实现类也独立成一个项目TestMethodImp (TestMethodImp.dll)里面
            TestMethodInterface.IServiceMethod servicemethod = Activator.CreateInstance("TestMethodImp", Method+"Method") as TestMethodInterface.IServiceMethod;
            return servicemethod.Vist();
        }
    }

    public interface IServiceMethod
    {
        string Vist();
    }
    public class CallMethod : IServiceMethod
    {
        public string Vist()
        {
            return "打电话";
        }
    }
    public class SMSMethod : IServiceMethod
    {
        public string Vist()
        {
            return "发短信";
        }
    }

}

namespace TestMethodInterface
{
    public interface IServiceMethod
    {
        string Vist();
    }
}
namespace TestMethodImp
{
    public class CallMethod : TestMethodInterface.IServiceMethod
    {
        public string Vist()
        {
            return "打电话";
        }
    }
    public class SMSMethod : TestMethodInterface.IServiceMethod
    {
        public string Vist()
        {
            return "发短信";
        }
    }
}

框架后

主要方法

        /// <summary>
        /// 创建对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="Key">对象关键字</param>
        public static T CreateStance<T>(string Key)

        /// <summary>
        /// 创建一个对象 单例模式
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="Key">对象关键字</param>
        public static T CreateSingle<T>(string Key)  

xml配置

<?xml version="1.0" encoding="utf-8" ?>
<IoCMap>
  <Map>
    <Key>Data</Key>
    <Value>Test_Business.Data</Value>
  </Map>
  <Map>
    <Key>Main</Key>
    <Value>Test_Business.Main</Value>
  </Map>
  <Map>
    <Key>Data2</Key>
    <Value>Test_Business2.Data</Value>
  </Map>
  <Map>
    <Key>Main2</Key>
    <Value>Test_Business2.Main</Value>
  </Map>
</IoCMap>

 实现主要代码

Assembly.CreateInstance(ObejctName);
Assembly.LoadFrom(AssemblePath);

实战使用

第一步:创建接口

/// <summary>
    /// 数据接口
    /// </summary>
    public interface IData
    {
        /// <summary>
        /// 数据
        /// </summary>
        string DataString { get; set; }
    }

    /// <summary>
    /// 程序运行接口
    /// </summary>
    public interface IMain
    {
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="data">数据类型</param>
        string ProcData(IData data);
    }

第二部:创建两个不同程序集的实现类

 /// <summary>
    /// 程序运行接口实现类一号
    /// </summary>
    public class Main : Test_InterFace.IMain
    {
        /// <summary>
        /// 处理方法实现
        /// </summary>
        public string ProcData(Test_InterFace.IData data)
        {
            return "我是一号处理方法" + data.DataString;
        }
    }
        /// <summary>
    /// 数据实现类一号
    /// </summary>
    public class Data : Test_InterFace.IData
    {
        private string _datastring;
        /// <summary>
        /// 实现数据属性
        /// </summary>
        public string DataString
        {
            get
            {
                return _datastring;
            }
            set
            {
                _datastring = "我是数据一号" + value;
            }
        }
    }

2.

/// <summary>
    /// 程序运行接口实现类二号
    /// </summary>
    public class Main2:IMain
    {
        /// <summary>
        /// 处理方法实现
        /// </summary>
        public string ProcData(IData data)
        {
            return "我是二号处理方法" + data.DataString;
        }
    }
     /// <summary>
    /// 数据实现类二号
    /// </summary>
    public class Data2:IData
    {
        private string _dataString;
        /// <summary>
        /// 实现数据属性
        /// </summary>
        public string DataString
        {
            get
            {
                return _dataString;
            }
            set
            {
                _dataString = "我是数据二号" + value;
            }
        }
    }

第三部:写配置

<?xml version="1.0" encoding="utf-8" ?>
<IoCMap>
  <Map>
    <Key>Data</Key>
    <Value>Test_Business.Data</Value>
  </Map>
  <Map>
    <Key>Main</Key>
    <Value>Test_Business.Main</Value>
  </Map>
  <Map>
    <Key>Data2</Key>
    <Value>Test_Business2.Data2</Value>
  </Map>
  <Map>
    <Key>Main2</Key>
    <Value>Test_Business2.Main2</Value>
  </Map>
</IoCMap>

第四部:开始调用

    class Program
    {
        static void Main(string[] args)
        {
            IMain main = IoCFactory.CreateStance<IMain>("Main");
            IData data = IoCFactory.CreateSingle<IData>("Data");
            Console.WriteLine(main.ProcData(data));
            Console.ReadKey();
        }
    }

运行结果:

更改配置:

<?xml version="1.0" encoding="utf-8" ?>
<IoCMap>
  <Map>
    <Key>Data</Key>
    <Value>Test_Business2.Data2</Value>
  </Map>
  <Map>
    <Key>Main</Key>
    <Value>Test_Business2.Main2</Value>
  </Map>
  <Map>
    <Key>Data2</Key>
    <Value>Test_Business.Data</Value>
  </Map>
  <Map>
    <Key>Main2</Key>
    <Value>Test_Business.Main</Value>
  </Map>
</IoCMap>

结果:

以后的扩展只需引用接口 实现接口 放到程序的bin目录 就可以了

欢迎大牛指点、吐槽。

需要完整代码的留言 O(∩_∩)O谢谢 

最新文章

  1. 11.ok6410之led驱动程序编写
  2. 笔记 线程(threads)
  3. Android 开发工具之Codota
  4. hiho1096_divided_product
  5. lucene合并测试的总结
  6. Android中的距离单位
  7. DB天气app冲刺第二天
  8. 初学swift笔记 枚举(七)
  9. 在开发 ExtJS 应用程序常犯的 10 个错误
  10. UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解
  11. 【VBA研究】查找目录以下全部文件的名称
  12. SQLite学习网址
  13. MySQL -- 关闭 binlog
  14. 3proxy使用方法
  15. javascript的数组之find()
  16. java用字符io流复制文件
  17. java字节码的工具(含IDEA插件)
  18. spring过滤器
  19. python+stomp+activemq
  20. CentOS工作内容(三)配置网络IP地址

热门文章

  1. 【Git】Git远程操作详解
  2. DSP开发资源总结,经典书籍,论坛
  3. BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
  4. 调用test case集,并生成测试报告
  5. C#获取字符串生成图片后的长度
  6. MVC中各类文件
  7. java笔记12之面向对象初始
  8. Java做acm所需要的基础知识之排序问题
  9. Unity3D基础学习 NGUI Example 7-Scroll View(Panel)制作固定包裹栏,点击传递参数显示物体
  10. redis常见错误