zoukankan      html  css  js  c++  java
  • 假丶依赖注入

    先在DAL添加多个共同的使用的接口:

    namespace SQLserver_MySQL
    {
        public interface ISqlHelper
        {
            string add();
            //  省略具体实现,如修改 删除 查询
        }
    }

    然后写DAL真正的方法继承上接口:

    namespace DAL
    {
        public class DALOracleSqlHelper : ISqlHelper
        {
            public int addOracle(string str)
            {
                //  省略具体实现
                return 1;
            }
            //  省略具体实现,如修改 删除 查询 
            public string add()
            {
                //  省略具体实现
                return "Oracle";
            }
        }
    }
    namespace DAL
    {
        public class DALMsSqlHelper : ISqlHelper
        {
            public int addMsSql(string str)
            {
                //  省略具体实现
                return 1;
            }
            public string add()
            {
                //  省略具体实现
                return "MsSql";
            }
        }
    }

    开始写BLL,利用构造函数来注入?不知道是不是这样说:

    namespace BLL
    {
        public class BLLAddStudent
        {
            ISqlHelper mssql = null;
            public BLLAddStudent(ISqlHelper sqlhelper)
            {
                mssql = sqlhelper;
            }
            
            public string addStudent()
            {
                return mssql.add();
            }
        }
    }

    到了我们的控制器,我这用到反射,可以随时替换:

    namespace SQLserver_MySQL
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Assembly asse = Assembly.LoadFrom("DAL.dll");
                ISqlHelper sql = (ISqlHelper)asse.CreateInstance("DAL.DALMsSqlHelper", true);
                sql = Assembly.Load("DAL").CreateInstance("DAL.DALMsSqlHelper") as ISqlHelper;
    
    
                ISqlHelper sqlhelper = new DALMsSqlHelper();         
                BLLAddStudent s = new BLLAddStudent(sqlhelper); 
                Console.WriteLine(s.addStudent());
    
    
                //ISqlHelper sqlhelper = new DALMsSqlHelper();
                //BLLAddStudent bll = new BLLAddStudent();
                //bll.sethelper(sqlhelper);
                ////BLLAddStudent s = new BLLAddStudent(sqlhelper); 
                //Console.WriteLine(s.addStudent());
    
                //Type dbHlperType = asse.GetType("DAL.DALOracleSqlHelper");
                //object obj = asse.CreateInstance("DAL.DALOracleSqlHelper"); //执行带参数的公共方法
                //object ds = dbHlperType.GetMembers();
                //object asd = dbHlperType.Name;
                //MethodInfo mt = dbHlperType.GetMethod("add");//加载方法
                //Console.WriteLine(mt.Invoke(obj, null));
    
    
    
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    ARP攻击及原理
    用C#语言构造蜘蛛程序
    在C#中调用VC编写的dll库
    关于ToolBar和MDI背景的问题
    修改IP和Mac地址的另类方法
    C#调用C++编写的COM DLL
    ARP欺骗技术实现原理分析
    获取本机的MAC的方法
    常用的攻击软件源代码(c)
    c#产生验证图片
  • 原文地址:https://www.cnblogs.com/ya-jun/p/11661342.html
Copyright © 2011-2022 走看看