o/r mapping in ado.net using DataSet

$Id: dataset_mapper.html,v 1.4 2004/12/16 06:02:30 george Exp $

To Be Continued...

Abstract: Using dataset as data holder and implement data mapper pattern.

I have to appologize first whether this pattern will be implement or not is still unknown. I have no idea that How to implement UoW and session level cache using DataSet.

Data Mapper pattern is important in Martin Fowler's greatest book. In his book, he mentioned that implementing Data Mapper pattern using DataSet( ADO.NET ).

Key consideration

  • How to store data in the mapper?

    We need to cache the domain object which get from the database, so next time we want to get the same object, we dont need to get it from database and we can use the same object in the system which won't be .

    Where to put the cache?

    • a. in mapper's data member?
    will be shared in all threads in the same process.
    • b. in application session cache?
    never implement in this way.

  • Using DataSet in Mapper as the dataset holder

    • a. How to select data into the dataset holder?
    According to martin's book, should not insert the data in the same table. That would be some problems if we want to insert the data into the table already exist in the dataset holder. Cannot use the select clause to select data from two table in the same time or we don't know which table owns the data from database.

    • b. How to update, delete and insert data into database for persistence.
    Look at this code fragment from
     8  namespace com.ds.mapper
     9  {
    10     /// <summary>
    11     /// from martin's excellent work.
    12     /// </summary>
    13     public class DataSetHolder
    14     {
    15        public DataSet Data = new DataSet();
    16
    17        private Hashtable DataAdapters = new Hashtable();
    18
    19        public void FillData( string query, string tableName )
    20        {
    21           if ( DataAdapters.Contains( tableName ) )
    22           {
    23              throw new MutlipleLoadException();
    24           }
    25
    26           OleDbDataAdapter da = new OleDbDataAdapter( query, DB.connection );
    27           OleDbCommandBuilder builder = new OleDbCommandBuilder( da );
    28           da.Fill( Data, tableName );
    29           DataAdapters.Add( tableName, da );
    30        }
    31
    32        /// <summary>
    33        /// UNDO: dont need delete method in DataSet???
    34        /// </summary>
    35        public void Update()
    36        {
    37           foreach ( string table in DataAdapters.Keys )
    38           {
    39              ( ( OleDbDataAdapter )DataAdapters[ table ] ).Update( Data, table );
    40           }
    41        }
    42
    43        /// <summary>
    44        /// Index for tables.
    45        /// </summary>
    46        public DataTable this[ string tableName ]
    47        {
    48           get{return Data.Tables[ tableName ];}
    49        }
    50     }
    51  }
    
    using CommandBuilder to generate Insert, Update and Delete sql clause. but there are some inconviences.
    1. Table must has primary key and the prmiary key must be in the select clause.
    1. use Select clause to select each table at a time, should not select multiple table in one select clause, then we cann't join table in sql which would cause some performance issue.
  • Unit of Work
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值