VS Studio可以帮助我们通过简单的拖拽生成Typed DataSet。同时自动生成了DataAdapter,更加简化了数据的访问操作。
下面是我基于VS 2005编写的一个类,为基于Typed DataSet的数据访问提供了比较简单的方法。
基类使用泛型,反射等方法实现了数据访问的封装。在定义实体类时,只需要继承该基类就可以实现数据的读取、保存和删除操作。
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Reflection;
5
using System.Data;
6
7
namespace 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
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Reflection;5
using System.Data;6

7
namespace SimpleCode.Core.Model8
{9
public class ModelBase<TAdapter, TDataTable, TRow> : SimpleCode.Core.Interface.IModel10
{11

12
Variable define36

37
Property define80

81
Constructor115

116
Private Method191

192
Public Method267

268
}269
}270


在业务层中定义Customer类
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using SimpleCode.Demo.DataModel;
5
using SimpleCode.Demo.DataModel.DemoDataTableAdapters;
6
using SimpleCode.Core.Model;
7
8
namespace 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
}
using System;2
using System.Collections.Generic;3
using System.Text;4
using SimpleCode.Demo.DataModel;5
using SimpleCode.Demo.DataModel.DemoDataTableAdapters;6
using SimpleCode.Core.Model;7

8
namespace SimpleCode.Demo.BL9
{10
public class Customer : ModelBase<CustomerTableAdapter,DemoData.CustomerDataTable,DemoData.CustomerRow>11
{12
public Customer(){ }13

14
public Customer(string id):base(id){} 15
}16
}在应用层中可以通过以下方式访问
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using SimpleCode.Demo.BL;
5
6
namespace 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
using System;2
using System.Collections.Generic;3
using System.Text;4
using SimpleCode.Demo.BL;5

6
namespace SimpleCode.Demo.Test7
{8
class Program9
{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 Informations17
myCustomer.Save();18

19
Customer myCustomer2 = new Customer("00125");20

21
Console.WriteLine("Customer Name is " + myCustomer2.CurrentItem.Name);22
}23
}24
}25



浙公网安备 33010602011771号