zoukankan      html  css  js  c++  java
  • 分享一套Code Smith 搭建N层架构模板

    工欲善其事,必先利其器:分享一套Code Smith 搭建N层架构模板

     

     开篇

    平常开发时,由于冗余代码过多,程序员做重复的工作过多势必会影响开发效率。倘若对重复性代码简单的复制、粘贴,虽然也能节省时间,但也需仔细一步步替换,这无疑也是一件费力的事。这时我们急需代码生成工具,根据一套Template快速生成我们需要的代码。代码生成器原理简单,完全可以开发一套适合自己的代码生成器,一个最简单的代码生成器,有几点你需要关注下:

    1. 查询系统视图:INFORMATION_SCHEMA.TABLES、 INFORMATION_SCHEMA.COLUMNS  可以获得数据库中表、列的相关信息。
    2. 字符串的拼接:StringBuilder的使用,其AppendLine()自动换行。
    3. 将字符串写入文本文件:File.WriteAllText()
    4. 使用了部分类(partial)
    5. 使用可空类型:由于数据库中表中数据很有可能是NULL,可空类型使得数据从表中读取出来赋值给值类型更加兼容。

    当然自己开发的代码生成器局限性很大,但对于小项目也是很好的选择。我也写过两篇代码生成器的拙文,仅供参考。

    说起代码生成器,不得不说Code Smith,基于Template的编程,下面举例的NTier架构是很基础的,除了熟悉的三层架构,还生成了抽象工厂、缓存、单例、反射、存储过程等,当然这个Demo只是学习用,大家可以继续扩展,打造自己的铜墙铁壁。

    Code Smith

    CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率。Code Smith提供自定义Template,语法也不复杂,类似于asp.net的标识符号,<%%>、<%=%>、<script runat="template">...</script>

    Code Smith API

    N层架构-实体类模板-Entity Template

    • 首先创建一个C# template,创建指令集,导入程序集和名称空间:
    复制代码
    <%@ CodeTemplate  Inherits="CodeTemplate" Language="C#" TargetLanguage="Text"
     Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Assembly Name="System.Design" %>
    <%@ Assembly Name="System.DirectoryServices" %>
    <%@ Assembly Name="System.Web" %>
    <%@ Assembly Name="System.Xml" %>
    
    <%@ Import Namespace="SchemaExplorer" %>
    <%@ Import NameSpace="System.IO" %>
    <%@ Import NameSpace="System.Text" %>
    <%@ Import NameSpace="System.Text.RegularExpressions" %>
    <%@ Import NameSpace="System.Diagnostics" %>
    <%@ Import NameSpace="System.Xml" %>
    <%@ Import NameSpace="System.Xml.Xsl" %>
    <%@ Import NameSpace="System.Xml.XPath" %> 
    复制代码
    • 添加属性Property:
    复制代码
    <%--DataSourse--%>
    <%@ Property Name="CurrentTable" Type="SchemaExplorer.TableSchema" Default="" Optional="False" DeepLoad="True" Category="" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
    
    <%@ Property Name="RootNamespace" Default="MyOffice.Models" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
    复制代码
    • 一个简单的实体类模板内容,可以这样写:
    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace <%=this.RootNamespace%>.Entity
    {
        public class <%= CurrentTable.Name%>
        {
            <% foreach(ColumnSchema col in CurrentTable.Columns)
            {%>
                public <%=col.DataType%> <%=col.Name%> {get;set;}
            <%}%>
                
        }
    }
    复制代码

    当然生成实体类时,需要考虑可空类型,写好一个Template,以后爽歪歪:)。

    N层架构-数据访问层接口模板,IDao Template

    • Script标签里可以自定义调用的方法,属性等。数据访问层接口大家肯定烂熟于心。
    • 常用的CRUD方法以及主表找子表,子表找主表。
    • Script里面的方法,你需要熟悉一下Code Smith的API,我在上面已经贴出了常用的API,供大家参考。

      

    N层架构-抽象工厂-AbstactFactory Template

      

    N层架构-DaoSqlFactory Template

      

    N层架构-数据访问层-SqlProviderDao Template

    • 数据访问层(DAAB)可以使用微软企业级框架 Microsoft Enterprise Library

      

    N层架构-SP存储过程-ScriptSp Template

    • 创建存储过程之前首先需要判读存储过程是否存在

     

    N层架构-业务逻辑层-BLLTemplate

    • 业务逻辑层上一对多,多对一需要考虑清楚,还要考虑复合主键这类情况

      

    N层架构-单例模式-DefaultProvieder Template

    • 单例+反射

     

    N层架构-主模板-Main Template

    • 添加指令集
    复制代码
    <%@ Import NameSpace="System.Text" %>
    <%@ Import NameSpace="System.Text.RegularExpressions" %>
    <%@ Import NameSpace="System.Diagnostics" %>
    <%@ Import NameSpace="System.Xml" %>
    <%@ Import NameSpace="System.Xml.Xsl" %>
    <%@ Import NameSpace="System.Xml.XPath" %> 
    复制代码
    • 注册子模板
    复制代码
    <%@ Register Name="EntityClassTemplate" Template="Eyes.Entity.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="BizClassTemplate" Template="Eyes.Biz.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="IDALInterfaceTemplate" Template="Eyes.IDAL.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="DALFactoryTemplate" Template="Eyes.DALFactory.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="DALSqlFactoryTemplate" Template="Eyes.DALSqlFactory.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="SqlSPTemplate" Template="Eyes.SqlSp.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="DataProvider" Template="Eyes.Provider.cst" MergeProperties="False" ExcludeProperties="" %>
    <%@ Register Name="DALSqlTemplate" Template="Eyes.DALSql.cst" MergeProperties="False" ExcludeProperties="" %>
    <%--DataSourse--%>
    <%@ Property Name="ChooseSourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>
    复制代码
    • 添加属性
    复制代码
    <%--DataSourse--%>
    <%@ Property Name="ChooseSourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>
    
    <%@ Property Name="RootNamespace" Default="Net.Itcast.CN" Type="System.String" Optional="False"%>
    复制代码
    • 调用Script里面的创建模板方法

      

    小结

    上面的Ntier Tempalte不是最优化的,还有很多细节需要你考虑,比如数据类型的转换:

     

    就像我开篇所说那样,工欲善其事,必先利其器,一套好的模板可以事半功倍,我在这儿抛砖引玉,期望于君共勉,打造属于自己的铜墙铁壁。

    点击下载

    本博客为木宛城主原创,基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名木宛城主(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
    分类: 架构思想
  • 相关阅读:
    面试题-Java多线程基础、实现工具和可见性保证(新更新版)
    Linux脚本-使用jar自动替换配置文件
    让ie兼容opacity属性的方法
    rgba兼容ie
    ie6的又一个变态问题
    长为112px的td里面有一个块span,居中时引起的兼容问题
    ie8中position不显示的问题
    haslayout
    CSS中zoom作用
    Vue学习笔记一
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2616341.html
Copyright © 2011-2022 走看看