学习三层结构心得(一)

    以前分析过Microsoft中另一个轻巧的三层结构的PetShop例子,发现PetShop还是比较好懂。这几天有空一直在看Duwamish的代码,参照了几篇分析Duwamish的文章。所以,这些天都会围绕这两个例子来学习,并写一些心得。

    首先,Duwamish分层为五层结构。Web层,业务外观层,业务规则层,业务实体层,数据访问层。

    业务实体层,里面实现的是各个实体的对象,用DataSet来表示,比如:Book对象,Custermer对象,Order对象等。建立该层以方便在其它各个层来传递数据(也就是说,各层传递数据是以DataSet来传递的)。另个,这个对象实体也与数据库中的库结构是对应的。

    抽取其中的一段代码来分析。以BookData.cs为例,在其中,就建立了Book这个类:

    
学习三层结构心得(一) public   class  BookData : DataSet
学习三层结构心得(一)
{
学习三层结构心得(一)   
public const String BOOKS_TABLE            = "Books";
学习三层结构心得(一)        
/// <value>The constant used for PKId field in the Books table. </value>
学习三层结构心得(一)        public const String PKID_FIELD             = "PKId";
学习三层结构心得(一)        
/// <value>The constant used for TypeId field in the Books table. </value>
学习三层结构心得(一)        public const String TYPE_ID_FIELD          = "TypeId";
学习三层结构心得(一)        
/// <value>The constant used for PublisherId field in the Books table. </value>
学习三层结构心得(一)        public const String PUBLISHER_ID_FIELD     = "PublisherId";
学习三层结构心得(一)        
/// <value>The constant used for PublicationYear field in the Books table. </value>
学习三层结构心得(一)        public const String PUBLICATION_YEAR_FIELD = "PublicationYear";
学习三层结构心得(一)        
/// <value>The constant used for ISBN field in the Books table. </value>
学习三层结构心得(一)        public const String ISBN_FIELD             = "ISBN";
学习三层结构心得(一)        
/// <value>The constant used for ImageFileSpec field in the Books table. </value>
学习三层结构心得(一)        public const String IMAGE_FILE_SPEC_FIELD  = "ImageFileSpec";
学习三层结构心得(一)        
/// <value>The constant used for Title field in the Books table. </value>
学习三层结构心得(一)        public const String TITLE_FIELD            = "Title";
学习三层结构心得(一)        
/// <value>The constant used for Title field in the Books table. </value>
学习三层结构心得(一)        public const String DESCRIPTION_FIELD      = "Description";
学习三层结构心得(一)        
/// <value>The constant used for UnitPrice field in the Books table. </value>
学习三层结构心得(一)        public const String UNIT_PRICE_FIELD       = "UnitPrice";
学习三层结构心得(一)        
/// <value>The constant used for UnitCost field in the Books table. </value>
学习三层结构心得(一)        public const String UNIT_COST_FIELD        = "UnitCost";
学习三层结构心得(一)        
/// <value>The constant used for ItemType field in the Books table. </value>
学习三层结构心得(一)        public const String ITEM_TYPE_FIELD        = "ItemType";
学习三层结构心得(一)        
/// <value>The constant used for PublisherName field in the Books table. </value>
学习三层结构心得(一)        public const String PUBLISHER_NAME_FIELD   = "PublisherName";
学习三层结构心得(一)        
/// <value>The constant used for Authors field in the Books table. </value>
学习三层结构心得(一)        public const String AUTHORS_FIELD          = "Authors";
学习三层结构心得(一)
学习三层结构心得(一)
学习三层结构心得(一)
///表示按什么来进行查找。有按title,isbn,author学习三层结构心得(一)等查找方法
学习三层结构心得(一)[SerializableAttribute]
学习三层结构心得(一)        
public enum SearchTypeEnum
学习三层结构心得(一)        
{
学习三层结构心得(一)            
/// <summary>
学习三层结构心得(一)            
///     Title search.
学习三层结构心得(一)            
/// </summary>

学习三层结构心得(一)            Title = 0,
学习三层结构心得(一)            
/// <summary>
学习三层结构心得(一)            
///     ISBN search.
学习三层结构心得(一)            
/// </summary>

学习三层结构心得(一)            ISBN = 1,
学习三层结构心得(一)            
/// <summary>
学习三层结构心得(一)            
///     Author search.
学习三层结构心得(一)            
/// </summary>

学习三层结构心得(一)            Author = 2,
学习三层结构心得(一)            
/// <summary>
学习三层结构心得(一)            
///     Subject search.
学习三层结构心得(一)            
/// </summary>

学习三层结构心得(一)            Subject = 3,
学习三层结构心得(一)            
/// <summary>
学习三层结构心得(一)            
///     Id search.
学习三层结构心得(一)            
/// </summary>

学习三层结构心得(一)            ID = 4,
学习三层结构心得(一)            
/// <summary>
学习三层结构心得(一)            
///     Id list search.
学习三层结构心得(一)            
/// </summary>

学习三层结构心得(一)            IdList = 5
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
///这个函数没分析出它什么意思
学习三层结构心得(一)private BookData(SerializationInfo info, StreamingContext context) : base(info, context) 
学习三层结构心得(一)        
{        
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
///构造函数,在类对象中加入一张表
学习三层结构心得(一)public BookData()
学习三层结构心得(一)        
{
学习三层结构心得(一)            
//
学习三层结构心得(一)            
// Create the tables in the dataset
学习三层结构心得(一)            
//
学习三层结构心得(一)
            BuildDataTables();
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
///建立表结构,并加入DataSet
学习三层结构心得(一)private void BuildDataTables()
学习三层结构心得(一)        
{
学习三层结构心得(一)            
//
学习三层结构心得(一)            
// Create the Books table
学习三层结构心得(一)            
//
学习三层结构心得(一)
            DataTable table   = new DataTable(BOOKS_TABLE);
学习三层结构心得(一)            DataColumnCollection columns 
= table.Columns;
学习三层结构心得(一)            
学习三层结构心得(一)            columns.Add(PKID_FIELD, 
typeof(System.Int32));
学习三层结构心得(一)            columns.Add(TYPE_ID_FIELD, 
typeof(System.Int32));
学习三层结构心得(一)            columns.Add(PUBLISHER_ID_FIELD, 
typeof(System.Int32));
学习三层结构心得(一)            columns.Add(PUBLICATION_YEAR_FIELD, 
typeof(System.Int16));
学习三层结构心得(一)            columns.Add(ISBN_FIELD, 
typeof(System.String));
学习三层结构心得(一)            columns.Add(IMAGE_FILE_SPEC_FIELD, 
typeof(System.String));
学习三层结构心得(一)            columns.Add(TITLE_FIELD, 
typeof(System.String));
学习三层结构心得(一)            columns.Add(DESCRIPTION_FIELD, 
typeof(System.String));
学习三层结构心得(一)            columns.Add(UNIT_PRICE_FIELD, 
typeof(System.Decimal));
学习三层结构心得(一)            columns.Add(UNIT_COST_FIELD, 
typeof(System.Decimal));
学习三层结构心得(一)            columns.Add(ITEM_TYPE_FIELD, 
typeof(System.String));
学习三层结构心得(一)            columns.Add(PUBLISHER_NAME_FIELD, 
typeof(System.String));
学习三层结构心得(一)            
//
学习三层结构心得(一)            
// [Authors] is an optional column that will get added dynamically to the table schema
学习三层结构心得(一)            
// when the stored proc. GetBookById is used to fill the 'Books' table
学习三层结构心得(一)            
//
学习三层结构心得(一)
            this.Tables.Add(table);
学习三层结构心得(一)        }

学习三层结构心得(一)}
    

    一旦对象实体建立好,其余层皆可创建该类的一个对象用它来作数据传递。



    数据访问层:功能很简单,与数据库进行交互。无非就是几种操作:Query,Update,Insert,Delete,当然,要根据数据库的设计,由程序应用提升出来的数据操作抽象来写出怎么与数据库进行哪些操作。还是看代码,看看DataAccess中的Book类怎么进行与数据库操作。

public   class  Books : IDisposable
    {

         private  SqlDataAdapter dsCommand;

        
public  Books()
        {

            dsCommand  =   new  SqlDataAdapter();

            dsCommand.SelectCommand  =   new  SqlCommand();
            dsCommand.SelectCommand.Connection  
=   new  SqlConnection(DuwamishConfiguration.ConnectionString);

            dsCommand.TableMappings.Add( " Table " , BookData.BOOKS_TABLE);
        }
        
        
///   <summary>
        
///      Dispose of this object's resources.
        
///   </summary>
         public   void  Dispose()
        {
            Dispose(
true );
            GC.SuppressFinalize(
true );  //  as a service to those who might inherit from us
        }

        
///   <summary>
        
///         Free the instance variables of this object.
        
///   </summary>
         protected   virtual   void  Dispose( bool  disposing)
        {
            
if  ( !  disposing)
                
return //  we're being collected, so let the GC take care of this object

            
if  (dsCommand  !=   null  )
            {
                
if  (dsCommand.SelectCommand  !=   null )
                {
                    
if ( dsCommand.SelectCommand.Connection  !=   null )
                        dsCommand.SelectCommand.Connection.Dispose();
                    dsCommand.SelectCommand.Dispose();
                }
                dsCommand.Dispose();
                dsCommand 
=   null ;
            }
        }
}


首先,book类继承自IDisposable,它可以派生Dispose函数来自主释放资源(在这里是指释放数据库的链接)。另外,它有一个SqlDataAdapter的成员,就是用来让SqlCommand与DataSet进行互通的,DataSet就是我们上面所提到的业务实体的对象。

不难看出,Book数据层应该对数据库的操作就是按照种类来查找书,按bookid来查找某本书,按作者来查找某些书,按书号来查找书,等等,于是我们在数据库中写入相应的存储过程(GetBooksByCategoryId,GetBookById,GetBooksByAuthor,GetBooksByISBN),然后,在源代码中调用如下:

学习三层结构心得(一) public  BookData GetBooksByCategoryId( int  categoryId)
学习三层结构心得(一)        
{
学习三层结构心得(一)            
return FillBookData("GetBooksByCategoryId""@CategoryId", categoryId.ToString());
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
public  BookData GetBookById( int  bookId)
学习三层结构心得(一)        
{
学习三层结构心得(一)            
return FillBookData("GetBookById""@BookId", bookId.ToString());
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
学习三层结构心得(一)
public  BookData GetBooksByAuthor(String searchText)
学习三层结构心得(一)        
{
学习三层结构心得(一)            
return FillBookData("GetBooksByAuthor""@Author", searchText );
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
public  BookData GetBooksByISBN(String searchText)
学习三层结构心得(一)        
{
学习三层结构心得(一)            
return FillBookData("GetBooksByISBN""@ISBN", searchText);
学习三层结构心得(一)        }

学习三层结构心得(一)
学习三层结构心得(一)
学习三层结构心得(一)
private  BookData FillBookData(String commandText, String paramName, String paramValue)
学习三层结构心得(一)        
{
学习三层结构心得(一)            
if (dsCommand == null )
学习三层结构心得(一)            
{
学习三层结构心得(一)                
throw new System.ObjectDisposedException( GetType().FullName );
学习三层结构心得(一)            }
            
学习三层结构心得(一)            BookData   data    
= new BookData();
学习三层结构心得(一)            SqlCommand command 
= dsCommand.SelectCommand;
学习三层结构心得(一)
学习三层结构心得(一)            command.CommandText 
= commandText;
学习三层结构心得(一)            command.CommandType 
= CommandType.StoredProcedure; // use stored proc for perf
学习三层结构心得(一)
            SqlParameter param = new SqlParameter(paramName, SqlDbType.NVarChar, 255);
学习三层结构心得(一)            param.Value 
= paramValue;
学习三层结构心得(一)            command.Parameters.Add(param);            
学习三层结构心得(一)
学习三层结构心得(一)            dsCommand.Fill(data);
学习三层结构心得(一)            
return data;
学习三层结构心得(一)        }


以上就是数据层做的事。

至于业务规则层与业务外观层,暂时没看到,续...

你可能感兴趣的:(学习)