1、现在很多项目都须要为将来的扩展考虑,当然数据库也是一个很重我要的方面,扩展自己的Provider,这就需要反射技术,虽然会对性能有所影响,但是性价比还是很高的哦,从 PetShop 和CommunityServer 都可以看到反射技术啦 ,也可以说反射是最基本的啦 ,呵呵! 他的老家是在System.Reflection,当我们要开工时首先就是要把他抓出来 .要实现这一功能我们当然要知道 Provider 的程序集,和工作的类名了,在多层架够中才能让逻辑和数据进行沟通,这样也方便团队开发的协条款发展,我们通过 PetShop 和CommunityServer 两个例子来说明一下 .我们先看看
2、PetShop 的反射技术 ,在配制文件中发现如下配制 :其实只从配制文件中得到程序集名称,一般程序集就是类所在命名空间,也就是编译后显示的DLL 名称 ,那 PetShop 是怎样工作的,下面我们来看一下 DataAccess 类,这也可说成一个工厂,呵呵,我们来看一下代码:1 using System.Reflection;2 using System.Configuration;34 namespace PetShop.DALFactory 56 / 7 / This class is implemented following the Abstract Factory pattern
3、to create the DAL implementation8 / specified from the configuration file9 / 10 public sealed class DataAccess 1112 / Look up the DAL implementation we should be using13 private static readonly string path = ConfigurationManager.AppSettings“WebDAL“;14 private static readonly string orderPath = Confi
4、gurationManager.AppSettings“OrdersDAL“;15 16 private DataAccess() 1718 public static PetShop.IDAL.ICategory CreateCategory() 19 string className = path + “.Category“;20 return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);21 2223 public static PetShop.IDAL.IInventory CreateIn
5、ventory() 24 string className = path + “.Inventory“;25 return (PetShop.IDAL.IInventory)Assembly.Load(path).CreateInstance(className);26 2728 public static PetShop.IDAL.IItem CreateItem() 29 string className = path + “.Item“;30 return (PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);
6、31 3233 public static PetShop.IDAL.IOrder CreateOrder() 34 string className = orderPath + “.Order“;35 return (PetShop.IDAL.IOrder)Assembly.Load(orderPath).CreateInstance(className);36 3738 public static PetShop.IDAL.IProduct CreateProduct() 39 string className = path + “.Product“;40 return (PetShop.
7、IDAL.IProduct)Assembly.Load(path).CreateInstance(className);41 4243 44 其中(PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);就是将类进行反射,首先要载入程序集,然后再创进类实例,通过静态方法就可以直接调用接口的方法等,从而实现了继承接口的类的反射,同时也方便表现层的数据传输.下面我们来看一下 CommunityServer 是怎么实现的,CS 是一个大象极别的项目,所以他有很自己扩展 Provider,那怎么样才能让他们工作呢 ?其实原
8、理和上述的反射方法差不多,只不过CS 用的是 Object.GetTyp()而达到这项功能.当我第一次看到时,还一直为怎样传输ConnectionString8 using System.Configuration;9 using System.IO;10 using System.Reflection;11 using System.Web;12 using CommunityServer.Configuration;1314 namespace CommunityServer.Components15 16 / 17 / DataProviders is responible for lo
9、ading and managing the various CS DataProviders18 / 19 public sealed class DataProviders20 21 / 22 / This class can not be instantiated23 / 24 private DataProviders()25 26 2728 private static void GetDataStoreParameters(Provider dataProvider, out string connectionString, out string databaseOwner)29
10、30 databaseOwner = dataProvider.Attributes“databaseOwner“;31 if(databaseOwner = null | databaseOwner.Trim().Length = 0)32 databaseOwner = ConfigurationSettings.AppSettingsdataProvider.Attributes“databaseOwnerStringName“;3334 connectionString = dataProvider.Attributes“connectionString“;35 if(connecti
11、onString = null | connectionString.Trim().Length = 0)36 connectionString = ConfigurationSettings.AppSettingsdataProvider.Attributes“connectionStringName“;37 3839 / 40 / Creates an instance of the provider using Activator. This instance should be41 / cached since it is an expesivie operation42 / 43 p
12、ublic static object CreateInstance(Provider dataProvider)44 45 /Find the current attributes46 string connectionString = null; /dataProvider.Attributes“connectionString“;47 string databaseOwner = null;/ dataProvider.Attributes“databaseOwner“;4849 GetDataStoreParameters(dataProvider, out connectionStr
13、ing, out databaseOwner);5051 /Get the type52 Type type = Type.GetType(dataProvider.Type);5354 object newObject = null;55 if(type != null)56 57 newObject = Activator.CreateInstance(type,new objectdatabaseOwner,connectionString); 58 59 60 if(newObject = null) /If we can not create an instance, throw a
14、n exception61 ProviderException(dataProvider.Name);6263 return newObject;64 6566 / 67 / Creates and Caches the ConstructorInfo for the specified provider. 68 / 69 public static ConstructorInfo CreateConstructorInfo (Provider dataProvider) 70 7172 / The assembly should be in bin or GAC, so we simply
15、need73 / to get an instance of the type74 /75 CSConfiguration config = CSConfiguration.GetConfig();76 ConstructorInfo providerCnstr = null;77 try 78 79 /string providerTypeName = (Provider) config.ProvidersproviderName).Type;80 Type type = Type.GetType( dataProvider.Type );8182 / Insert the type int
16、o the cache83 /84 Type paramTypes = new Type2;85 paramTypes0 = typeof(string);86 paramTypes1 = typeof(string);8788 providerCnstr = type.GetConstructor(paramTypes);8990 91 catch 92 93 ProviderException(dataProvider.Name);94 9596 if(providerCnstr = null)97 ProviderException(dataProvider.Name);9899 ret
17、urn providerCnstr;100 101102 / 103 / Creates an instance of the specified provider using the Cached104 / ConstructorInfo from CreateConstructorInfo105 / 106 public static object Invoke(Provider dataProvider)107 108 object paramArray = new object2;109110 111 string dbOwner = null; 112 string connstri
18、ng = null;113114 GetDataStoreParameters(dataProvider, out connstring, out dbOwner);115116 paramArray0 = dbOwner;117 paramArray1 = connstring;118119 return CreateConstructorInfo(dataProvider).Invoke(paramArray);120 121122 #region Exception123 private static void ProviderException(string providerName)
19、124 125 CSConfiguration config = CSConfiguration.GetConfig();126 HttpContext context = HttpContext.Current;127 if (context != null) 128 129 130 / We cant load the dataprovider131 /132 StreamReader reader = new StreamReader( context.Server.MapPath(“/Languages/“ + config.DefaultLanguage + “/errors/Dat
20、aProvider.htm“) );133 string html = reader.ReadToEnd();134 reader.Close();135136 html = html.Replace(“DATAPROVIDERCLASS“, providerName);137 html = html.Replace(“DATAPROVIDERASSEMBLY“, providerName);138 context.Response.Write(html);139 context.Response.End();140 141 else 142 143 throw new CSException(CSExceptionType.DataProvider, “Unable to load “ + providerName);144 145 146 #endregion147 148 文章是越写越不知道写些什么?CS 有很多人研究他,应该会有关于这样的文章,不多说了,呵呵.