刀刀的命名空间

欢迎大家一起讨论.NET问题

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
         大家都知道CodeSmith是一个非常强大的代码生成工具,如果仅仅因为自动根据模板自动写代码,并不令人敬佩,单关键的是CodeSmith可以结合你自定义开发的Net程序集一起工作。这就他强大的地方,几乎只要达到“只有你想不到,没有做不到”的地步。
        在这个范类中,我开发了一个自定义的NET类daooNet.CodeSmith.TdaCodeSmithIntf,
 用来协助CodeSmith一起工作。
 daooNet.CodeSmith.TdaCodeSmithIntf代码如下:
using System;
using System.Data;

namespace daooNet.CodeSmith
{
    
/// <summary>
    
/// CodeSmithIntf 的摘要说明。
    
/// </summary>

    public class TdaCodeSmithIntf
    
{
        
private TdaDBbase m_db=null;
        
public TdaCodeSmithIntf(string strConn)
        
{
            m_db
=new TdaDBbase(strConn);
        }

        
/// <summary>
        
/// 取得表中所有字段集合
        
/// </summary>
        
/// <param name="tableName"></param>
        
/// <returns></returns>

        public DataColumnCollection GetColumnCollection(string tableName)
        
{
            
string strsql="select * from "+tableName;
            DataTable dt
=m_db.OpenSql_Table(true,strsql);
            
return dt.Columns;
        }

        
public string GetDataFieldType(DataColumn dc)
        
{            
            
return dc.DataType.Name;
        }

    }

}

  其中用到我的框架程序集daooNet.dll:/Files/daoodaoo/daooNet.rar 
需要把daooNet.dll放到CodeSmith的安装目录的Addins目录下,如“C:\Program Files\CodeSmith\v3.0\AddIns÷”

daooNet.DB.Entity.cst 模板文件如下:
<%--?? --%>
<%@ CodeTemplate Language="C#"  TargetLanguage="Text" Description="Template description here." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="daooNet" %>
<%@ Import Namespace="daooNet" %>
<%@ Import Namespace="daooNet.CodeSmith" %>
<%--数据库配置--%>
<%@ Property Name="DbConn" Type="System.String" Default=""
  Category
="Context" Description="数据库连接字符串,暂时只支持oracle/sqlserver" %>
<%@ Property Name="TableName" Type="System.String" Default="" 
Optional
="False" Category="" Description="表名" %>
<%--\????? --%>

<%@ Property Name="NameSpace" Type="System.String" Default="daooNet.Entity" Optional="False" Category="" Description="????" %>
<%@ Property Name="Author" Type="System.String" Default="daoodaoo" Optional="False" Category="" Description="??" %>
<%@ Property Name="Description" Type="System.String" Default="daoodaoo@163.com" Optional="False" Category="" Description="?????" %> 


<% TdaCodeSmithIntf csIntf=new TdaCodeSmithIntf(DbConn);%>
 

using System;
using System.Data;

namespace <%= NameSpace %>
{
    
/// <summary>
    
/// 描述:<%= Description %>
    
/// 作者:<%= Author %>
    
/// 日期:<%= DateTime.Now.ToString() %>
    
///</summary>

    [TdaAttrDataTable("<%=TableName%>")]
    
public class <%= TableName %>Entity:TdaEntity
    
{
        
/// <summary>
        
/// <%= TableName %>的实体类
        
///</summary>

        public <%= TableName %>Entity()
        
{
        }
  
  
  
<%
       
//????
        DataColumnCollection columns=csIntf.GetColumnCollection(TableName);
        
for(int i=0;i<columns.Count;i++)
           
{
                Response.Write(
"        //"+columns[i].Caption+"\r\n");
                Response.Write(
"        private "+csIntf.GetDataFieldType(columns[i])+" m_"+columns[i].Caption+";\r\n");
           }

  
%>  
  
  
<%
   
//??
   for(int i=0;i<columns.Count;i++)
   
{
  
%> 
          
///<summary>
        
///<%= columns[i].Caption %>
        
///</summary>

        [TdaAttrDataField(eDataFieldType.Normal)]
        
public <%= csIntf.GetDataFieldType(columns[i]) %> <%= columns[i].Caption %>
        
{
            
get   {  return m_<%= columns[i].Caption %>;   }
            
set   {  m_<%= columns[i].Caption %>=value;    }
        }

  
<%   
   }

  
%>  
    }

 }




CodeSmith自动生成的模板效果如下:

using System;
using System.Data;
using daooNet;
using daooNet.ORM;

namespace daooNet.Entity
{
    
/// <summary>
    
/// 描述:daoodaoo的实体类模板
    
/// 作者:daoodaoo@163.com
    
/// 日期:2006-4-27 17:54:36
    
///</summary>

    [TdaAttrDataTable("TUser")]
    
public class TUserEntity:TdaEntity
    
{
        
/// <summary>
        
/// TUser的实体类
        
///</summary>

        public TUserEntity()
        
{
        }
  
  
        
//USERID
        private Decimal m_USERID;
        
//LOGINNAME
        private String m_LOGINNAME;
        
//LOGINPASS
        private String m_LOGINPASS;
        
//USERNAME
        private String m_USERNAME;
        
//USERRIGHT
        private String m_USERRIGHT;
  
          
///<summary>
        
///USERID
        
///</summary>

        [TdaAttrDataField(eDataFieldType.Normal)]
        
public Decimal USERID
        
{
            
get   {  return m_USERID;   }
            
set   {  m_USERID=value;    }
        }

          
///<summary>
        
///LOGINNAME
        
///</summary>

        [TdaAttrDataField(eDataFieldType.Normal)]
        
public String LOGINNAME
        
{
            
get   {  return m_LOGINNAME;   }
            
set   {  m_LOGINNAME=value;    }
        }

          
///<summary>
        
///LOGINPASS
        
///</summary>

        [TdaAttrDataField(eDataFieldType.Normal)]
        
public String LOGINPASS
        
{
            
get   {  return m_LOGINPASS;   }
            
set   {  m_LOGINPASS=value;    }
        }

          
///<summary>
        
///USERNAME
        
///</summary>

        [TdaAttrDataField(eDataFieldType.Normal)]
        
public String USERNAME
        
{
            
get   {  return m_USERNAME;   }
            
set   {  m_USERNAME=value;    }
        }

          
///<summary>
        
///USERRIGHT
        
///</summary>

        [TdaAttrDataField(eDataFieldType.Normal)]
        
public String USERRIGHT
        
{
            
get   {  return m_USERRIGHT;   }
            
set   {  m_USERRIGHT=value;    }
        }

    }

 }
当然则个模板还有待改进的地方,比如System.Data.DataColumn不能表示是否是主键等信息。这将在以后的讨论中发布。
posted on 2006-04-27 18:09  刀刀=|~~~>  阅读(2232)  评论(0)    收藏  举报