• 假丶依赖注入


    先在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();
            }
        }
    }
  • 相关阅读:
    我理解的朴素贝叶斯模型
    P2P贷款全攻略,贷前、贷中、贷后工作事项解析
    Jupyter Notebook 快速入门
    R语言|数据特征分析
    R语言︱处理缺失数据&&异常值检验、离群点分析、异常值处理
    mysql explain执行计划详解
    R语言中的回归诊断-- car包
    一行代码搞定 R 语言模型输出!(使用 stargazer 包)
    基于R语言的时间序列指数模型
    基于R语言的ARIMA模型
  • 原文地址:https://www.cnblogs.com/ya-jun/p/11661342.html
Copyright © 2020-2023  润新知