ORM新实现——Dali
今天在http://0daycheck.eastgame.net/上看到一个ORM工具——Dali,号称最大能省80%的代码。Down下来看看,原来它将传统ORM中的配置文件省了,而且在实体映射类中加入了与数据操作相关的事件,还有代码生成工具。做简单的应用确实方便了很多。
把它给的两个例了贴出来,大家一看便知道有多方便了。
不使用Dali的例子:
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12![]()
13
namespace DaliWebDemoCS
14
{
15
// This code uses SQL Server. To use a different database you will need to
16
// change all the SQL Server specific objects such as SqlDataAdapter,
17
// SqlConnection and SqlCommand as well as the using statement above.
18![]()
19
public partial class CustomerNoDali : System.Web.UI.Page
20
{
21
private string connectString;
22![]()
23
private void Page_Load(object sender, System.EventArgs e)
24
{
25
connectString = (string)Application["ConnectString"];
26![]()
27
if (!IsPostBack)
28
{
29
// Populate the customer dropdown list
30
FillCustomerIdDropdown();
31![]()
32
// Load all the form controls with data from the database.
33
LoadPage();
34
}
35
}
36![]()
37
protected void CustomerID_SelectedIndexChanged(object sender, System.EventArgs e)
38
{
39
// Load all the form controls with data from the database.
40
LoadPage();
41
}
42![]()
43
protected void SaveCustomerLinkButton_Click(object sender, System.EventArgs e)
44
{
45
// Save all the form control data to the database.
46
SavePage();
47![]()
48
// Fill the Customer dropdown again in case customer changed. Before
49
// filling, get the current customer. After filling, re-select customer.
50
string selectedCustomer = CustomerID.SelectedValue;
51
FillCustomerIdDropdown();
52
CustomerID.SelectedValue = selectedCustomer;
53
}
54![]()
55
private void FillCustomerIdDropdown()
56
{
57
// Get a collection of CustomerListItem objects to fill the dropdown.
58
string sql =
59
"SELECT CustomerID, CompanyName " +
60
" FROM Customers " +
61
" ORDER BY CompanyName";
62
DataTable table = new DataTable();
63
SqlDataAdapter adapter = new SqlDataAdapter(sql, connectString);
64
adapter.Fill(table);
65![]()
66
// Use the table as the data source for the dropdown
67
CustomerID.DataSource = table;
68
CustomerID.DataValueField = "CustomerID";
69
CustomerID.DataTextField = "CompanyName";
70![]()
71
CustomerID.DataBind();
72
}
73![]()
74
private void LoadPage()
75
{
76
string sql =
77
"SELECT * " +
78
" FROM Customers " +
79
" WHERE CustomerID = @CustomerID ";
80
DataTable table = new DataTable();
81
SqlDataAdapter adapter = new SqlDataAdapter(sql, connectString);
82
adapter.SelectCommand.Parameters.AddWithValue("@CustomerID", CustomerID.SelectedValue);
83
adapter.Fill(table);
84![]()
85
CompanyName.Text = table.Rows[0]["CompanyName"] as string;
86
ContactName.Text = table.Rows[0]["ContactName"] as string;
87
ContactTitle.Text = table.Rows[0]["ContactTitle"] as string;
88
Phone.Text = table.Rows[0]["Phone"] as string;
89
Fax.Text = table.Rows[0]["Fax"] as string;
90
Address.Text = table.Rows[0]["Address"] as string;
91
City.Text = table.Rows[0]["City"] as string;
92
Region.Text = table.Rows[0]["Region"] as string;
93
PostalCode.Text = table.Rows[0]["PostalCode"] as string;
94
Country.Text = table.Rows[0]["Country"] as string;
95
}
96![]()
97
private void SavePage()
98
{
99
string sql =
100
"UPDATE Customers " +
101
" SET CompanyName = @CompanyName, " +
102
" ContactName = @ContactName, " +
103
" ContactTitle = @ContactTitle, " +
104
" Phone = @Phone, " +
105
" Fax = @Fax, " +
106
" Address = @Address, " +
107
" City = @City, " +
108
" Region = @Region, " +
109
" PostalCode = @PostalCode, " +
110
" Country = @Country " +
111
" WHERE CustomerID = @CustomerID ";
112
DataTable table = new DataTable();
113
SqlConnection connection = new SqlConnection(connectString);
114
SqlCommand command = new SqlCommand(sql, connection);
115
command.Parameters.AddWithValue("@CustomerID", CustomerID.SelectedValue);
116
command.Parameters.AddWithValue("@CompanyName", CompanyName.Text);
117
command.Parameters.AddWithValue("@ContactName", ContactName.Text);
118
command.Parameters.AddWithValue("@ContactTitle", ContactTitle.Text);
119
command.Parameters.AddWithValue("@Phone", Phone.Text);
120
command.Parameters.AddWithValue("@Fax", Fax.Text);
121
command.Parameters.AddWithValue("@Address", Address.Text);
122
command.Parameters.AddWithValue("@City", City.Text);
123
command.Parameters.AddWithValue("@Region", Region.Text);
124
command.Parameters.AddWithValue("@PostalCode", PostalCode.Text);
125
command.Parameters.AddWithValue("@Country", Country.Text);
126
connection.Open();
127
command.ExecuteNonQuery();
128
connection.Close();
129
}
130
}
131
}
132![]()
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.Data.SqlClient;12

13
namespace DaliWebDemoCS14
{15
// This code uses SQL Server. To use a different database you will need to16
// change all the SQL Server specific objects such as SqlDataAdapter, 17
// SqlConnection and SqlCommand as well as the using statement above.18

19
public partial class CustomerNoDali : System.Web.UI.Page20
{21
private string connectString;22

23
private void Page_Load(object sender, System.EventArgs e)24
{25
connectString = (string)Application["ConnectString"];26

27
if (!IsPostBack)28
{29
// Populate the customer dropdown list30
FillCustomerIdDropdown();31

32
// Load all the form controls with data from the database.33
LoadPage();34
}35
}36

37
protected void CustomerID_SelectedIndexChanged(object sender, System.EventArgs e)38
{39
// Load all the form controls with data from the database.40
LoadPage();41
}42

43
protected void SaveCustomerLinkButton_Click(object sender, System.EventArgs e)44
{45
// Save all the form control data to the database.46
SavePage();47

48
// Fill the Customer dropdown again in case customer changed. Before 49
// filling, get the current customer. After filling, re-select customer.50
string selectedCustomer = CustomerID.SelectedValue;51
FillCustomerIdDropdown();52
CustomerID.SelectedValue = selectedCustomer;53
}54

55
private void FillCustomerIdDropdown()56
{57
// Get a collection of CustomerListItem objects to fill the dropdown.58
string sql =59
"SELECT CustomerID, CompanyName " +60
" FROM Customers " +61
" ORDER BY CompanyName";62
DataTable table = new DataTable();63
SqlDataAdapter adapter = new SqlDataAdapter(sql, connectString);64
adapter.Fill(table);65

66
// Use the table as the data source for the dropdown67
CustomerID.DataSource = table;68
CustomerID.DataValueField = "CustomerID";69
CustomerID.DataTextField = "CompanyName";70

71
CustomerID.DataBind();72
}73

74
private void LoadPage()75
{76
string sql =77
"SELECT * " +78
" FROM Customers " +79
" WHERE CustomerID = @CustomerID ";80
DataTable table = new DataTable();81
SqlDataAdapter adapter = new SqlDataAdapter(sql, connectString);82
adapter.SelectCommand.Parameters.AddWithValue("@CustomerID", CustomerID.SelectedValue);83
adapter.Fill(table);84

85
CompanyName.Text = table.Rows[0]["CompanyName"] as string;86
ContactName.Text = table.Rows[0]["ContactName"] as string;87
ContactTitle.Text = table.Rows[0]["ContactTitle"] as string;88
Phone.Text = table.Rows[0]["Phone"] as string;89
Fax.Text = table.Rows[0]["Fax"] as string;90
Address.Text = table.Rows[0]["Address"] as string;91
City.Text = table.Rows[0]["City"] as string;92
Region.Text = table.Rows[0]["Region"] as string;93
PostalCode.Text = table.Rows[0]["PostalCode"] as string;94
Country.Text = table.Rows[0]["Country"] as string;95
}96

97
private void SavePage()98
{99
string sql =100
"UPDATE Customers " +101
" SET CompanyName = @CompanyName, " +102
" ContactName = @ContactName, " +103
" ContactTitle = @ContactTitle, " +104
" Phone = @Phone, " +105
" Fax = @Fax, " +106
" Address = @Address, " +107
" City = @City, " +108
" Region = @Region, " +109
" PostalCode = @PostalCode, " +110
" Country = @Country " +111
" WHERE CustomerID = @CustomerID ";112
DataTable table = new DataTable();113
SqlConnection connection = new SqlConnection(connectString);114
SqlCommand command = new SqlCommand(sql, connection);115
command.Parameters.AddWithValue("@CustomerID", CustomerID.SelectedValue);116
command.Parameters.AddWithValue("@CompanyName", CompanyName.Text);117
command.Parameters.AddWithValue("@ContactName", ContactName.Text);118
command.Parameters.AddWithValue("@ContactTitle", ContactTitle.Text);119
command.Parameters.AddWithValue("@Phone", Phone.Text);120
command.Parameters.AddWithValue("@Fax", Fax.Text);121
command.Parameters.AddWithValue("@Address", Address.Text);122
command.Parameters.AddWithValue("@City", City.Text);123
command.Parameters.AddWithValue("@Region", Region.Text);124
command.Parameters.AddWithValue("@PostalCode", PostalCode.Text);125
command.Parameters.AddWithValue("@Country", Country.Text);126
connection.Open();127
command.ExecuteNonQuery();128
connection.Close();129
}130
}131
}132

使用Dali的例子:
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11![]()
12
using Revelation.Dali;
13![]()
14
namespace DaliWebDemoCS
15
{
16
[DaliClass(TableName = "Customers")]
17
public partial class Customer : System.Web.UI.Page
18
{
19
private void Page_Load(object sender, System.EventArgs e)
20
{
21
if (!IsPostBack)
22
{
23
// Populate the customer dropdown list
24
FillCustomerIdDropdown();
25![]()
26
// Load all the form controls with data from the database.
27
DataManager.Default.Load(Page);
28
}
29
}
30![]()
31
protected void CustomerID_SelectedIndexChanged(object sender, System.EventArgs e)
32
{
33
// Load all the form controls with data from the database.
34
DataManager.Default.Load(Page);
35
}
36![]()
37
protected void SaveCustomerLinkButton_Click(object sender, System.EventArgs e)
38
{
39
// Save all the form control data to the database.
40
DataManager.Default.Save(Page);
41![]()
42
// Fill the Customer dropdown again in case customer changed. Before
43
// filling, get the current customer. After filling, re-select customer.
44
string selectedCustomer = CustomerID.SelectedValue;
45
FillCustomerIdDropdown();
46
CustomerID.SelectedValue = selectedCustomer;
47
}
48![]()
49
private void FillCustomerIdDropdown()
50
{
51
// Get a collection of CustomerListItem objects to fill the dropdown.
52
ArrayList customers = DataManager.Default.Find(
53
typeof(CustomerListItem), null, "CompanyName");
54![]()
55
// Use the list as the data source for the dropdown
56
CustomerID.DataSource = customers;
57
CustomerID.DataValueField = "CustomerID";
58
CustomerID.DataTextField = "CompanyName";
59![]()
60
CustomerID.DataBind();
61
}
62
}
63
}
64![]()
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
using Revelation.Dali;13

14
namespace DaliWebDemoCS15
{16
[DaliClass(TableName = "Customers")]17
public partial class Customer : System.Web.UI.Page18
{19
private void Page_Load(object sender, System.EventArgs e)20
{21
if (!IsPostBack)22
{23
// Populate the customer dropdown list24
FillCustomerIdDropdown();25

26
// Load all the form controls with data from the database.27
DataManager.Default.Load(Page);28
}29
}30

31
protected void CustomerID_SelectedIndexChanged(object sender, System.EventArgs e)32
{33
// Load all the form controls with data from the database.34
DataManager.Default.Load(Page);35
}36

37
protected void SaveCustomerLinkButton_Click(object sender, System.EventArgs e)38
{39
// Save all the form control data to the database.40
DataManager.Default.Save(Page);41

42
// Fill the Customer dropdown again in case customer changed. Before 43
// filling, get the current customer. After filling, re-select customer.44
string selectedCustomer = CustomerID.SelectedValue;45
FillCustomerIdDropdown();46
CustomerID.SelectedValue = selectedCustomer;47
}48

49
private void FillCustomerIdDropdown()50
{51
// Get a collection of CustomerListItem objects to fill the dropdown.52
ArrayList customers = DataManager.Default.Find(53
typeof(CustomerListItem), null, "CompanyName");54

55
// Use the list as the data source for the dropdown56
CustomerID.DataSource = customers;57
CustomerID.DataValueField = "CustomerID";58
CustomerID.DataTextField = "CompanyName";59

60
CustomerID.DataBind();61
}62
}63
}64

CustomerListItem类:
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
* CustomerListItem.cs
3
* -------------------
4
*
5
* This file is part of the Revelation Technologies Dali code samples.
6
*
7
* Copyright ?2005 Revelation Technologies, LLC. All rights reserved.
8
*
9
* This source code is intended only as a supplement to Revelation
10
* Technologies Dali development tools and documentation. It is intended
11
* to demonstrate specific concepts and may not be suitable for all
12
* applications. You may freely use this source code in whole or in
13
* part. Use of this code in whole or in part acknowledges your acceptance
14
* of the following terms and conditions:
15
*
16
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
17
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
19
* PURPOSE.
20
*
21
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22![]()
23
using System;
24![]()
25
using Revelation.Dali;
26![]()
27
namespace DaliWebDemoCS
28
{
29
// This class is used to retrieve the CustomerID and CompanyName for all
30
// customers. This data is used to populate the dropdown list. It uses
31
// Properties instead of simple fields because the List's data binding
32
// only works with Properties.
33![]()
34
// Map this class to the customers table
35
[DaliClass(TableName = "Customers")]
36
public class CustomerListItem
37
{
38
public string CustomerID
39
{
40
set { customerID_ = value; }
41
get { return customerID_; }
42
}
43
private string customerID_;
44![]()
45
public string CompanyName
46
{
47
set { companyName_ = value; }
48
get { return companyName_; }
49
}
50
public string companyName_;
51
}
52
}
53![]()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *2
* CustomerListItem.cs3
* -------------------4
* 5
* This file is part of the Revelation Technologies Dali code samples.6
*7
* Copyright ?2005 Revelation Technologies, LLC. All rights reserved.8
*9
* This source code is intended only as a supplement to Revelation 10
* Technologies Dali development tools and documentation. It is intended11
* to demonstrate specific concepts and may not be suitable for all 12
* applications. You may freely use this source code in whole or in 13
* part. Use of this code in whole or in part acknowledges your acceptance14
* of the following terms and conditions:15
*16
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY17
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR 19
* PURPOSE. 20
* 21
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */22

23
using System;24

25
using Revelation.Dali;26

27
namespace DaliWebDemoCS28
{29
// This class is used to retrieve the CustomerID and CompanyName for all30
// customers. This data is used to populate the dropdown list. It uses31
// Properties instead of simple fields because the List's data binding 32
// only works with Properties.33

34
// Map this class to the customers table35
[DaliClass(TableName = "Customers")]36
public class CustomerListItem37
{38
public string CustomerID39
{40
set { customerID_ = value; }41
get { return customerID_; }42
}43
private string customerID_;44

45
public string CompanyName46
{47
set { companyName_ = value; }48
get { return companyName_; }49
}50
public string companyName_;51
}52
}53

更重要的是没有数据库映射配置文件。
不过,表关系现在看还处理不了,只能对单表进行处理,希望听棠大哥的SPL能借荐一下它的思想,把SPL做得更方便实用。:)
东西不复杂,但价格还是高了,$149,网站:http://www.revtechnologies.com/有兴趣的可以去看看。
这里有个下载好的版本,可以直接从这里下载。


浙公网安备 33010602011771号