码迷,mamicode.com
首页 > 其他好文 > 详细

动态创建DAL层类的实例

时间:2016-09-13 11:31:31      阅读:141      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!