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

本文详细介绍了自定义ORM框架中的Database组件设计与实现。该组件负责大部分数据访问操作,包括连接数据库、执行SQL语句等。文章还探讨了如何通过配置文件设置数据库连接字符串及操作类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上接:[原创] 我的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
ContractedBlock.gif ExpandedBlockStart.gif
<!--<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
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Databasedefinesaseriesofdatabase-basedoperations.
///</summary>

publicabstractpartialclassDatabase:IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
privatebool_isDisposed;

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

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Databaseconnectionstringwhichisspecifiedbythedatabasefactory.
///</summary>

publicstringConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis._connectionString;
}


set
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
this._connectionString=value;
}

}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Theconcretedatabasespecificproviderfactory.
///</summary>

publicDbProviderFactoryDatabaseProviderFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis._dbProviderFactory;
}

set
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
this._dbProviderFactory=value;
}

}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Thedefaullcommandtypetoperformthedatabaseoperationswhichdonotspecifythecommanftype.
///</summary>

publicCommandTypeDefaultCommandType
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis._defaultCommandType;
}

set
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
this._defaultCommandType=value;
}

}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Determinewhethertousecommandbuilderormappedstoredprocedurestoexecutedatabaseoperations.
///</summary>

publicboolUseCommandBuilder
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis._useCommandBuilder;
}

set
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
this._useCommandBuilder=value;
}

}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Astringwhichindicatesthetypetoperformmappingbetweenstoredprocedureparameterandsourcecolumn.
///</summary>

publicIDbParameterNameMappingDbParameterNameMapping
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis._dbParameterNameMapping;
}

set
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
this._dbParameterNameMapping=value;
}

}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Astringwhichindicatesthetypetoperformmappingbetweentablenameandtherelatedstoredprocedurenames.
///</summary>

publicIStoredProcedureNameMappingStoredProcedureNameMapping
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis._storedProcedureNameMapping;
}

set
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
this._storedProcedureNameMapping=value;
}

}

#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif
Connection&DatabaseDataAdapter#regionConnection&DatabaseDataAdapter
privateDbDataAdapter_dbDataAdapter;
privateDbConnection_connection;

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Agenericdatabasedataadapterwhichisresponsibleforsavethechangeddataintodatabase.
///</summary>

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


returnthis._dbDataAdapter;
}

}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Thedatabaseconnection.
///</summary>

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


returnthis._connection;
}

}

#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif
Constructor#regionConstructor
publicDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{

}


#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif
IDisposableMembers#regionIDisposableMembers

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


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

}


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

}

}

this._isDisposed=true;
}

#endregion

}

}

Part II: Fill Dataset

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

ContractedBlock.gif ExpandedBlockStart.gif
<!--<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
ExpandedBlockStart.gifContractedBlock.gif
html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值