首页 > 其他 > 详细

动态创建DAL层类的实例

时间:2016-09-13 11:29:25      阅读:127      评论:0      收藏:0      [点我收藏+]

为了可扩展性,方便以后对于代码的修改维护,使用动态创建DAL层对象。

1、首先在webconfig中的configuration下添加配置项

 <appSettings>
    <add key="IStuDAL" value="StuDAL.StudentDAL"/>
  </appSettings>

2、在工厂中创建实例

技术分享
 1 namespace DALFactory
 2 {
 3     public  class DALHelper
 4     {
 5         public static IStuDAL AddStu()
 6         {
 7             //IStuDAL stu = new StudentDAL();
 8             //return stu;
 9 
10 
11             //这里应该动态创建类的实例
12             string path = ConfigurationManager.AppSettings["IStuDAL"];   //得到StuDAL.StudentDAL
13             string type = path.Split(.)[0];    //得到StuDAL
14             Assembly ab = Assembly.Load(type);
15             return  (IStuDAL)ab.CreateInstance(path);
16         }
17 
18         public static IStuDAL GetAllStudent()
19         {
20            string path=ConfigurationManager.AppSettings["IStuDAL"];
21 
22            string type = path.Split(.)[0];
23            Assembly ab = Assembly.Load(type);
24            return (IStuDAL)ab.CreateInstance(path);
25         }
26     }
27 }
View Code

注意一点
用此方法前需要在UI层中添加对DAL层引用

 

动态创建DAL层类的实例

原文:http://www.cnblogs.com/move-up/p/5867491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!