ASP.NET三层架构源码(CodeSmith版)之六:Table-生成DALFactory
动软代码生成器生成的ASP.NET三层架构代码比较规范,是学习ASP.NET的好例子
此三层架构改造自动软的工厂模式模板,使用CodeSmith进行重写,以方便大家修改模板文件
以下是针对表格的DAL生成工厂的源码:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="OutputFileCodeTemplate" Debug="False" CompilerVersion="v3.5" Description="Template description here." %> <%-- SchemaExplorer --%> <%@ Assembly Name="SchemaExplorer" %> <%@ Assembly Name="CodeSmith.BaseTemplates" %> <%@ Assembly Name="CodeSmith.CustomProperties" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Import Namespace="System.Text" %> <%@ Import Namespace="CodeSmith.BaseTemplates" %> <%@ Import Namespace="CodeSmith.CustomProperties" %> <%-- 添加源数据库属性 --%> <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. GettingStarted - Required" Description="Database" %> <%@ Property Name="Namespace" Type="String" Category="Context" Description="NameSpace" Default="CrsNameSpace"%> <%@ Property Name="Author" Type="String" Category="Context" Description="Author" Default="chenr"%> <%@ Property Name="TablePrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %> /*------------------------------------------------ // File Name:DALFactory.cs // File Description:DAL Factory // Author:<%=Author%> // Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%> //------------------------------------------------*/ using System; using System.Reflection; using System.Configuration; using <%=Namespace%>.IDAL; namespace <%=Namespace%>.DALFactory { ////// 抽象工厂模式创建DAL。 /// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口) /// DataCache类在导出代码的文件夹里 /// public sealed class <%=Namespace%>DALFactory { private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"]; ////// ///(这里的命名空间根据实际情况更改为自己项目的命名空间) /// /// 创建对象或从缓存获取 /// 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; } ////// 创建数据层接口 /// //public static t Create(string ClassName) //{ //string ClassNamespace = AssemblyPath +"."+ ClassName; //object objType = CreateObject(AssemblyPath, ClassNamespace); //return (t)objType; //} <% foreach(TableSchema TableName in this.SourceDatabase.Tables) %> <% { %> ////// 创建<%=ClearPrefix(TableName.Name) %>数据层接口。 /// public static I<%=ClearPrefix(TableName.Name) %> Create<%=ClearPrefix(TableName.Name) %>() { string ClassNamespace = AssemblyPath +".<%=ClearPrefix(TableName.Name) %>"; object objType=CreateObject(AssemblyPath,ClassNamespace); return (I<%=ClearPrefix(TableName.Name) %>)objType; } <% } %> /////////////////////////////////////////////////////////////////////////////// <% foreach(ViewSchema ViewName in this.SourceDatabase.Views) %> <% { %> ////// 创建<%=ClearPrefix(ViewName.Name) %>数据层接口。 /// public static I<%=ClearPrefix(ViewName.Name) %> Create<%=ClearPrefix(ViewName.Name) %>() { string ClassNamespace = AssemblyPath +".<%=ClearPrefix(ViewName.Name) %>"; object objType=CreateObject(AssemblyPath,ClassNamespace); return (I<%=ClearPrefix(ViewName.Name) %>)objType; } <% } %> } }