VS Studio可以帮助我们通过简单的拖拽生成Typed DataSet。同时自动生成了DataAdapter,更加简化了数据的访问操作。
下面是我基于VS 2005编写的一个类,为基于Typed DataSet的数据访问提供了比较简单的方法。
基类使用泛型,反射等方法实现了数据访问的封装。在定义实体类时,只需要继承该基类就可以实现数据的读取、保存和删除操作。

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Reflection;
  5using System.Data;
  6
  7namespace SimpleCode.Core.Model
  8{
  9    public class ModelBase<TAdapter, TDataTable, TRow> : SimpleCode.Core.Interface.IModel
 10    {
 11
 12        Variable define
 36
 37        Property define
 80
 81        Constructor
115
116        Private Method
191
192        Public Method
267
268    }

269}

270
在数据层中创建Typed DataSet

在业务层中定义Customer类
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using SimpleCode.Demo.DataModel;
 5using SimpleCode.Demo.DataModel.DemoDataTableAdapters;
 6using SimpleCode.Core.Model;
 7
 8namespace SimpleCode.Demo.BL
 9{
10    public class Customer : ModelBase<CustomerTableAdapter,DemoData.CustomerDataTable,DemoData.CustomerRow>
11    {
12        public Customer(){ }
13
14        public Customer(string id):base(id){}        
15    }

16}

在应用层中可以通过以下方式访问
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using SimpleCode.Demo.BL;
 5
 6namespace SimpleCode.Demo.Test
 7{
 8    class Program
 9    {
10        static void Main(string[] args)
11        {
12            Customer myCustomer = new Customer();
13            myCustomer.CurrentItem.CustomerID = "00125";
14            myCustomer.CurrentItem.Name = "Jason Lee";
15            myCustomer.CurrentItem.Address = "Shanghai";
16            //Add Customer Informations
17            myCustomer.Save();
18
19            Customer myCustomer2 = new Customer("00125");
20
21            Console.WriteLine("Customer Name is " + myCustomer2.CurrentItem.Name);
22        }

23    }

24}

25
posted on 2007-05-21 11:40  Jason Li 2011  阅读(259)  评论(0编辑  收藏  举报