运用反射实现多层和多数据库开发
现在很多项目都须要为将来的扩展考虑,当然数据库也是一个很重我要的方面,扩展自己的Provider,这就需要反射技术,虽然会对性能有所影响,但是性价比还是很高的哦,从PetShop和CommunityServer都可以看到反射技术啦,也可以说反射是最基本的啦,呵呵!他的老家是在System.Reflection,当我们要开工时首先就是要把他抓出来.
要实现这一功能我们当然要知道Provider的程序集,和工作的类名了,在多层架够中才能让逻辑和数据进行沟通,这样也方便团队开发的协条款发展,我们通过PetShop和CommunityServer两个例子来说明一下.
我们先看看PetShop的反射技术,在配制文件中发现如下配制:
<!-- Pet Shop DAL configuration settings -->
<add key="WebDAL" value="PetShop.SQLServerDAL"/>
<add key="OrdersDAL" value="PetShop.SQLServerDAL"/>
<add key="ProfileDAL" value="PetShop.SQLProfileDAL"/> 1
using System.Reflection;
2
using System.Configuration;
3
4
namespace PetShop.DALFactory {
5
6
/// <summary>
7
/// This class is implemented following the Abstract Factory pattern to create the DAL implementation
8
/// specified from the configuration file
9
/// </summary>
10
public sealed class DataAccess {
11
12
// Look up the DAL implementation we should be using
13
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
14
private static readonly string orderPath = ConfigurationManager.AppSettings["OrdersDAL"];
15
16
private DataAccess() { }
17
18
public static PetShop.IDAL.ICategory CreateCategory() {
19
string className = path + ".Category";
20
return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
21
}
22
23
public static PetShop.IDAL.IInventory CreateInventory() {
24
string className = path + ".Inventory";
25
return (PetShop.IDAL.IInventory)Assembly.Load(path).CreateInstance(className);
26
}
27
28
public static PetShop.IDAL.IItem CreateItem() {
29
string className = path + ".Item";
30
return (PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);
31
}
32
33
public static PetShop.IDAL.IOrder CreateOrder() {
34
string className = orderPath + ".Order";
35
return (PetShop.IDAL.IOrder)Assembly.Load(orderPath).CreateInstance(className);
36
}
37
38
public static PetShop.IDAL.IProduct CreateProduct() {
39
string className = path + ".Product";
40
return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
41
}
42
43
}
44
}
其中(PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);就是将类进行反射,首先要载入程序集,然后再创进类实例,通过静态方法就可以直接调用接口的方法等,从而实现了继承接口的类的反射,同时也方便表现层的数据传输.
using System.Reflection;2
using System.Configuration;3

4
namespace PetShop.DALFactory {5

6
/// <summary>7
/// This class is implemented following the Abstract Factory pattern to create the DAL implementation8
/// specified from the configuration file9
/// </summary>10
public sealed class DataAccess {11

12
// Look up the DAL implementation we should be using13
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];14
private static readonly string orderPath = ConfigurationManager.AppSettings["OrdersDAL"];15
16
private DataAccess() { }17

18
public static PetShop.IDAL.ICategory CreateCategory() {19
string className = path + ".Category";20
return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);21
}22

23
public static PetShop.IDAL.IInventory CreateInventory() {24
string className = path + ".Inventory";25
return (PetShop.IDAL.IInventory)Assembly.Load(path).CreateInstance(className);26
}27

28
public static PetShop.IDAL.IItem CreateItem() {29
string className = path + ".Item";30
return (PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);31
}32

33
public static PetShop.IDAL.IOrder CreateOrder() {34
string className = orderPath + ".Order";35
return (PetShop.IDAL.IOrder)Assembly.Load(orderPath).CreateInstance(className);36
}37

38
public static PetShop.IDAL.IProduct CreateProduct() {39
string className = path + ".Product";40
return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);41
}42

43
}44
}下面我们来看一下CommunityServer是怎么实现的,CS是一个大象极别的项目,所以他有很自己扩展Provider,那怎么样才能让他们工作呢?其实原理和上述的反射方法差不多,只不过CS用的是Object.GetTyp()而达到这项功能.当我第一次看到时,还一直为怎样传输ConnectionString&DataOwner而不解,后来打开源数据看了一下各个方法的注解后才理解.
CS从自定的配制文件中读取Provider节点并缓存,这在CSConfiguration类中可以发现,在DataProviders类中就实现反射,通过能构造函数的反射查找最匹配的构造函数对类进行实例化,当然当他们遇到有ConnectionString&DataOwner两参数的构造函数时,就对其进行实例化,从而达到对他们值的传输.
文章是越写越不知道写些什么?CS有很多人研究他,应该会有关于这样的文章,不多说了,呵呵.




浙公网安备 33010602011771号