[原创]我的ORM: 开发自己的Data Access Application Block - Part II

上接:[原创] 我的ORM: 开发自己的Data Access Application Block - Part I
4. Database

下面来介绍重中之重:Database,绝大部分的DataAccess 操作都集中在这个Abstract Database中。这是一个相对庞大的Class,所以不得不采用Partial Class的方式来编写。

Part I:Field 和Property

这些Field 和Property基本上对应我们前面的Configuraiton。此为我们定义了三个Field 和Property:DbDataAdapter,Connection,_transaction。考虑到垃圾回收,使Database实现IDisposable接口。值得说明一点的是,我们通过Database的DatabaseProviderFactory创建了泛型的DbDataAdapter,DbConnection和Transaction。

  • ConnectionString:string
  • DatabaseProviderFactory:DbProviderFactory
  • DefaultCommandType:CommandType
  • UseCommandBuilder:bool
  • DbParameterNameMapping:IDbParameterNameMapping
  • StoredProcedureNameMapping:IStoredProcedureNameMapping
  • DbDataAdapter:DbDataAdapter
  • Connection: DbConnection
  • Transaction: DbTransaction

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.Common;

usingArtech.ApplicationBlock.DataMapping;

namespaceArtech.ApplicationBlock.DataAccess
{
/**////<summary>
///Databasedefinesaseriesofdatabase-basedoperations.
///</summary>

publicabstractpartialclassDatabase:IDisposable
{
privatebool_isDisposed;

Thefiveprivatefieldspossessthecorresspondingpubicproperties,andtheyareonlyallowedtobeevaluatedbyDatabaseFactory.#regionThefiveprivatefieldspossessthecorresspondingpubicproperties,andtheyareonlyallowedtobeevaluatedbyDatabaseFactory.
privateDbProviderFactory_dbProviderFactory;
privatestring_connectionString;
privateCommandType_defaultCommandType;
privatebool_useCommandBuilder;
privateIDbParameterNameMapping_dbParameterNameMapping;
privateIStoredProcedureNameMapping_storedProcedureNameMapping;

/**////<summary>
///Databaseconnectionstringwhichisspecifiedbythedatabasefactory.
///</summary>

publicstringConnectionString
{
get
{
returnthis._connectionString;
}


set
{
this._connectionString=value;
}

}


/**////<summary>
///Theconcretedatabasespecificproviderfactory.
///</summary>

publicDbProviderFactoryDatabaseProviderFactory
{
get
{
returnthis._dbProviderFactory;
}

set
{
this._dbProviderFactory=value;
}

}


/**////<summary>
///Thedefaullcommandtypetoperformthedatabaseoperationswhichdonotspecifythecommanftype.
///</summary>

publicCommandTypeDefaultCommandType
{
get
{
returnthis._defaultCommandType;
}

set
{
this._defaultCommandType=value;
}

}


/**////<summary>
///Determinewhethertousecommandbuilderormappedstoredprocedurestoexecutedatabaseoperations.
///</summary>

publicboolUseCommandBuilder
{
get
{
returnthis._useCommandBuilder;
}

set
{
this._useCommandBuilder=value;
}

}


/**////<summary>
///Astringwhichindicatesthetypetoperformmappingbetweenstoredprocedureparameterandsourcecolumn.
///</summary>

publicIDbParameterNameMappingDbParameterNameMapping
{
get
{
returnthis._dbParameterNameMapping;
}

set
{
this._dbParameterNameMapping=value;
}

}


/**////<summary>
///Astringwhichindicatesthetypetoperformmappingbetweentablenameandtherelatedstoredprocedurenames.
///</summary>

publicIStoredProcedureNameMappingStoredProcedureNameMapping
{
get
{
returnthis._storedProcedureNameMapping;
}

set
{
this._storedProcedureNameMapping=value;
}

}

#endregion


Connection&DatabaseDataAdapter#regionConnection&DatabaseDataAdapter
privateDbDataAdapter_dbDataAdapter;
privateDbConnection_connection;

/**////<summary>
///Agenericdatabasedataadapterwhichisresponsibleforsavethechangeddataintodatabase.
///</summary>

privateDbDataAdapterDatabaseAdapter
{
get
{
if(this._dbDataAdapter==null)
{
this._dbDataAdapter=this._dbProviderFactory.CreateDataAdapter();
this._dbDataAdapter.AcceptChangesDuringUpdate=false;
this._dbDataAdapter.MissingSchemaAction=MissingSchemaAction.Add;
}


returnthis._dbDataAdapter;
}

}


/**////<summary>
///Thedatabaseconnection.
///</summary>

privateDbConnectionConnection
{
get
{
if(this._connection==null)
{
this._connection=this._dbProviderFactory.CreateConnection();
this._connection.ConnectionString=this._connectionString;
}


returnthis._connection;
}

}

#endregion


Constructor#regionConstructor
publicDatabase()
{

}


#endregion


IDisposableMembers#regionIDisposableMembers

publicvoidDispose()
{
Dispose(
true);
GC.SuppressFinalize(
this);
}


privatevoidDispose(booldisposing)
{
if(!this._isDisposed)
{
if(disposing)
{
if(this._connection!=null)
{
if(this._connection.State==ConnectionState.Open)
{
this._connection.Close();
}

}


if(this._transaction!=null)
{
this._transaction.Dispose();
}

}

}

this._isDisposed=true;
}

#endregion

}

}

Part II: Fill Dataset

很简单,基本上ADO.NET 的基本操作,没什么可值得说的。


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.Common;

usingArtech.ApplicationBlock.DataMapping;

namespaceArtech.ApplicationBlock.DataAccess
http://www.cnblogs.com/Ima
分享到:
评论

你可能感兴趣的:(.net,orm,Access)