我使有"动软.Net代码生成器生成的DALFactory层的代码, 哪位大虾能不能帮详细解译一下这段代码的意思.
using System;
using System.Reflection;
using System.Configuration;
using Test.IDAL;
namespace Test.DALFactory
{
/// <summary>
/// 抽象工厂模式创建DAL。
/// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)
/// DataCache类在导出代码的文件夹里
/// <appSettings>
/// <add key="DAL" value="Test.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
/// <summary>
/// 创建对象或从缓存获取
/// </summary>
public static object CreateObject(string AssemblyPath,string ClassNamespace)
{
object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
if (objType == null)
{
try
{
objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
DataCache.SetCache(ClassNamespace, objType);// 写入缓存
}
catch
{}
}
return objType;
}
/// <summary>
/// 创建TABLE1数据层接口
/// </summary>
public static Test.IDAL.ITABLE1 CreateTABLE1()
{ string ClassNamespace = AssemblyPath +".TABLE1";
object objType=CreateObject(AssemblyPath,ClassNamespace);
return (Test.IDAL.ITABLE1)objType;
}
}
}
using System;
using System.Reflection;
using System.Configuration;
using Test.IDAL;
namespace Test.DALFactory
{
/// <summary>
/// 抽象工厂模式创建DAL。
/// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)
/// DataCache类在导出代码的文件夹里
/// <appSettings>
/// <add key="DAL" value="Test.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
/// <summary>
/// 创建对象或从缓存获取
/// </summary>
public static object CreateObject(string AssemblyPath,string ClassNamespace)
{
object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
if (objType == null)
{
try
{
objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
DataCache.SetCache(ClassNamespace, objType);// 写入缓存
}
catch
{}
}
return objType;
}
/// <summary>
/// 创建TABLE1数据层接口
/// </summary>
public static Test.IDAL.ITABLE1 CreateTABLE1()
{ string ClassNamespace = AssemblyPath +".TABLE1";
object objType=CreateObject(AssemblyPath,ClassNamespace);
return (Test.IDAL.ITABLE1)objType;
}
}
}
那么具体哪些命名空间里的类需要实例化呢,我们最开始肯定是不知道的,因为后面可能要增加,
为了后面增加的时候不改程序,所以就把需要实例化的类的命名空间写到配置文件里面.
创建之后缓存到DataCache类里面,以后就可以直接到DataCache类去取了.
跟 SqlHelper.cs 差不多啊
那么具体哪些命名空间里的类需要实例化呢,我们最开始肯定是不知道的,因为后面可能要增加,
为了后面增加的时候不改程序,所以就把需要实例化的类的命名空间写到配置文件里面.
创建之后缓存到DataCache类里面,以后就可以直接到DataCache类去取了.