Attribute在.NET程式設計中的應用(三) (轉)

amyz發表於2007-11-16
Attribute在.NET程式設計中的應用(三) (轉)[@more@]

Attribute在中的應用(三)

用於引數的Attribute

在編寫多層應用的時候,你是否為每次要寫大量類似的資料訪問程式碼而感到枯燥無味?比如我們需要編寫過程的程式碼,或者編寫T_程式碼,這些程式碼往往需要傳遞各種引數,有的引數個數比較多,一不小心還容易寫錯。有沒有一種一勞永逸的方法?當然,你可以使用MS的Data Access Application Block,也可以使用自己編寫的Block。這裡向你提供一種另類方法,那就是使用Attribute。

下面的程式碼是一個呼叫AddCustomer儲存過程的常規方法:

public int AddCustomer(SqlConnection connection, string customerName, string country, string province, string city, string address, string telephone) { SqlCommand command=new SqlCommand("AddCustomer", connection); command.CommandType=CommandType.StoredProcedure; command.Parameters.Add("@CustomerName",SqlType.NVarChar,50).Value=customerName; command.Parameters.Add("@country",SqlDbType.NVarChar,20).Value=country; command.Parameters.Add("@Province",SqlDbType.NVarChar,20).Value=province; command.Parameters.Add("@City",SqlDbType.NVarChar,20).Value=city; command.Parameters.Add("@Address",SqlDbType.NVarChar,60).Value=address; command.Parameters.Add("@Telephone",SqlDbType.NvarChar,16).Value=telephone; command.Parameters.Add("@CustomerId",SqlDbType.Int,4).Direction=ParameterDirection.Output; connection.Open(); command.ExecuteNonQuery(); connection.Close(); int custId=(int)command.Parameters["@CustomerId"].Value; return custId; }


上面的程式碼,建立一個Command例項,然後新增儲存過程的引數,然後呼叫ExecuteMonQuery方法資料的插入操作,最後返回CustomerId。從程式碼可以看到引數的新增是一種重複單調的工作。如果一個專案有100多個甚至幾百個儲存過程,作為開發人員的你會不會要想辦法偷懶?(反正我會的:-))。

下面開始我們的程式碼自動生成工程:

我們的目的是根據方法的引數以及方法的名稱,自動生成一個Command例項,第一步我們要做的就是建立一個SqlParameterAttribute, 程式碼如下:

SqlCommandParameterAttribute.cs using System; using System.Data; using De=System.Diagnostics.Debug; namespace DataAccess { // SqlParemeterAttribute 施加到儲存過程引數 [ AttributeUsage(AttributeTargets.Parameter) ] public class SqlParameterAttribute : Attribute { private string name; //引數名稱 private bool paramTypeDefined; //是否引數的型別已經定義 private SqlDbType paramType; //引數型別 private int size; //引數尺寸大小 private byte precision; //引數精度 private byte scale; //引數範圍 private bool directionDefined; //是否定義了引數方向 private ParameterDirection direction; //引數方向 public SqlParameterAttribute() { } public string Name { get { return name == null ? string.Empty : name; } set { _name = value; } } public int Size { get { return size; } set { size = value; } } public byte Precision { get { return precision; } set { precision = value; } } public byte Scale { get { return scale; } set { scale = value; } } public ParameterDirection Direction { get { Debug.Assert(directionDefined); return direction; } set { direction = value; directionDefined = true; } } public SqlDbType SqlDbType { get { Debug.Assert(paramTypeDefined); return paramType; } set { paramType = value; paramTypeDefined = true; } } public bool IsNameDefined { get { return name != null && name.Length != 0; } } public bool IsSizeDefined { get { return size != 0; } } public bool IsTypeDefined { get { return paramTypeDefined; } } public bool IsDirectionDefined { get { return directionDefined; } } public bool IsScaleDefined { get { return _scale != 0; } } public bool IsPrecisionDefined { get { return _precision != 0; } } ...

以上定義了SqlParameterAttribute的欄位和相應的屬性,為了方便Attribute的使用,我們過載幾個構造器,不同的過載構造器用於不用的引數:

... // 過載構造器,如果方法中對應於儲存過程引數名稱不同的話,我們用它來設定儲存過程的名稱 // 其他構造器的目的類似 public SqlParameterAttribute(string name) { Name=name; } public SqlParameterAttribute(int size) { Size=size; } public SqlParameterAttribute(SqlDbType paramType) { SqlDbType=paramType; } public SqlParameterAttribute(string name, SqlDbType paramType) { Name = name; SqlDbType = paramType; } public SqlParameterAttribute(SqlDbType paramType, int size) { SqlDbType = paramType; Size = size; } public SqlParameterAttribute(string name, int size) { Name = name; Size = size; } public SqlParameterAttribute(string name, SqlDbType paramType, int size) { Name = name; SqlDbType = paramType; Size = size; } } }


為了區分方法中不是儲存過程引數的那些引數,比如SqlConnection,我們也需要定義一個非儲存過程引數的Attribute:

//NonCommandParameterAttribute.cs using System; namespace DataAccess { [ AttributeUsage(AttributeTargets.Parameter) ] public sealed class NonCommandParameterAttribute : Attribute { } }


我們已經完成了SQL的引數Attribute的定義,在建立Command物件生成器之前,讓我們考慮這樣的一個事實,那就是如果我們資料訪問層呼叫的不是儲存過程,也就是說Command的CommandType不是儲存過程,而是帶有引數的SQL語句,我們想讓我們的方法一樣可以適合這種情況,同樣我們仍然可以使用Attribute,定義一個用於方法的Attribute來表明該方法中的生成的Command的CommandType是儲存過程還是SQL文字,下面是新定義的Attribute的程式碼:

//SqlCommandMethodAttribute.cs using System; using System.Data; namespace Emisonline.DataAccess { [AttributeUsage(AttributeTargets.Method)] public sealed class SqlCommandMethodAttribute : Attribute { private string commandText; private CommandType commandType; public SqlCommandMethodAttribute( CommandType commandType, string commandText) { commandType=commandType; commandText=commandText; } public SqlCommandMethodAttribute(CommandType commandType) : this(commandType, null){} public string CommandText { get { return commandText==null ? string.Empty : commandText; } set { commandText=value; } } public CommandType CommandType { get { return commandType; } set { commandType=value; } } } }


我們的Attribute的定義工作已經全部完成,下一步就是要建立一個用來生成Command物件的類。(待續)

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-982775/,如需轉載,請註明出處,否則將追究法律責任。

相關文章