使用LINQ TO SQL 的CodeSmith模板(1)

     这几天抽空了解了一下微软的新宠物LINQ,才知道之前模仿Pedshop4.0和Nettier2.0弄出来的数据层代码模板可以删除掉了!微软总是让人跟不上脚步,还没有把C#2.0搞明白,C#3.0已经出来了,唉!真不知道是应该高兴,还是哭泣......
    既来之,则安之,跟着改吧!!!
     仔细看看dbml文件,觉得自动生成的代码组织得很好,留下了很多可以扩展的地方,预感到扩展起来应该还是很方便的。Google了一下,这方面的资料好像不多,对于我这样的一些“还没有长齐毛”的鸟,最怕的就是摸错了方向,希望可以通过这里和大家一起探讨。

    默认值的问题——我希望映射出来的类,每个属性都有默认值
    1、首先,设计数据表时,需要让表的每个字段都有默认值,通过CodeSmith获得一个表字段的默认值不难,但是需要处理一下才能合乎LINQ的要求,把Nettiers2.2的脚本代码处理以下:
public string GetMemberVariableDefaultValue(ColumnSchema column)
        
{
            
string result = column.ExtendedProperties["CS_Default"].Value.ToString();
            
string n = string.Empty;
            
foreach (char c in result)
            
{
                
switch (c)
                
{
                    
case '(':
                    
case ')':
                    
case '\'':
                        break;
                    
default:
                        n 
= n + c;
                        
break;
                }


            }

            result 
= "\"" + n + "\"";
            
switch (column.DataType)
            
{
                
case DbType.Guid:
                    
{
                        
return "Guid.Empty";
                    }

                
case DbType.AnsiString:
                
case DbType.AnsiStringFixedLength:
                
case DbType.String:
                
case DbType.StringFixedLength:
                    
{
                        
if (column.Size == 1)
                        
{
                            
return result = "'" + n + "'";
                        }

                        
return result;                        
                    }
                
                
case DbType.Int16:
                
case DbType.Int32:
                
case DbType.Int64:
                
case DbType.Single:
                    
{
                        
return "0";
                    }

                
case DbType.DateTime:
                    
{
                        
return "DateTime.Parse(" +  result + ")";
                    }

                
case DbType.Date:
                    
{
                        
return "DateTime.Parse(" +  result  + ")";
                    }

                
case DbType.Boolean:
                    
{
                        
return "false";
                    }

                
default:
                    
{
                        
return "";
                    }

            }

        }
有了这个方法就可以方便地自动生成如下代码:
//个人信息表流水号
            this.PersonAutoId = 0;
            
            
//门诊号
            this.ClinicNo = "0";
            
            
//病案号
            this.PMN = "0";
            
            
//姓名
            this.Name = "<空>";
            
            
//曾用名
            this.Usedname = "<空>";
            
            
//身份证号码
            this.IdCardNo = "<未填写>";
            
            
//身份证件类别
            this.IdentificationType = '0';
            
            
//身份证件号码
            this.IdentificationNo = "<未填写>";
            
            
//性别代码
            this.SexCode = '9';
            
            
//国籍代码
            this.CountryCode = "CN";
            
            
//民族代码
            this.NationalityCode = "00";
            
            
//籍贯代码
            this.NativePlaceCode = "000000";
            
            
//出生地代码
            this.BirthPlaceCode = "000000";
            
            
//出生日期及时间
            this.DateTimeOfBirth = DateTime.Parse("1900-01-01 00:00:00");
            
            
//婚姻状况代码
            this.MaritalStatusCode = "9";
            
            
//邮政碥码
            this.HomeZipcode = "00000";
            
            
//户籍登记地址
            this.RegistedResidenceAddress = "<空>";
            
            
//家庭地址(常住地址)
            this.HomeAddress = "<空>";
            
            
//家庭电话号码
            this.HomePhone = "<未提供>";
            
            
//职业类别代码
            this.OccupationTypeCode = "999";
            
            
//工作单位表自动编码
            this.EmployerAutoId = 0;
    以上代码应该放在什么地方合适呢?自然是放在构造函数里比较合适。发现vs2008自动生成的dbml文件里,每个映射类的构造函数里都有个OnCreated()方法,这是一个部分方法,于是决定把以上代码放在扩展的OnCreated()里面。
    新建一个部分类,扩展OnCreated()部分方法,得到以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YcMRIS.DataAccess
{
    
public partial class Person
    
{
        
partial void OnCreated()
        
{
            
//个人信息表流水号
            this.PersonAutoId = 0;
            
            
//门诊号
            this.ClinicNo = "0";
            
            
//病案号
            this.PMN = "0";
            
            
//姓名
            this.Name = "<空>";
            
            
//曾用名
            this.Usedname = "<空>";
            
            
//身份证号码
            this.IdCardNo = "<未填写>";
            
            
//身份证件类别
            this.IdentificationType = '0';
            
            
//身份证件号码
            this.IdentificationNo = "<未填写>";
            
            
//性别代码
            this.SexCode = '9';
            
            
//国籍代码
            this.CountryCode = "CN";
            
            
//民族代码
            this.NationalityCode = "00";
            
            
//籍贯代码
            this.NativePlaceCode = "000000";
            
            
//出生地代码
            this.BirthPlaceCode = "000000";
            
            
//出生日期及时间
            this.DateTimeOfBirth = DateTime.Parse("1900-01-01 00:00:00");
            
            
//婚姻状况代码
            this.MaritalStatusCode = "9";
            
            
//邮政碥码
            this.HomeZipcode = "00000";
            
            
//户籍登记地址
            this.RegistedResidenceAddress = "<空>";
            
            
//家庭地址(常住地址)
            this.HomeAddress = "<空>";
            
            
//家庭电话号码
            this.HomePhone = "<未提供>";
            
            
//职业类别代码
            this.OccupationTypeCode = "999";
            
            
//工作单位表自动编码
            this.EmployerAutoId = 0;
            
            
//医疗付款方式
            this.PaymentWayCode = "5";
            
            
//医疗保险帐号
            this.InsuranceNo = "0";
            
            
//数据版本
            this.RowVersion = "0000000000000000";
            
            
//备注
            this.Remark = "<空>";
            
        }

    }

}

    如果需要映射的数据表很多,一个一个建立这样的逻辑代码部分类是一件很烦的事情,自然想到还是使用CodeSmith来帮忙,设置好需要映射的表后,我们需要做的就是看CodeSmith如何工作了,具体脚本和原来差不多,不再列出,大家可以下载参考,希望不吝批评,留个言!谢谢。
    示例模板下载
posted @ 2008-03-17 20:47  江郎  阅读(1491)  评论(0)    收藏  举报