ORM新实现——Dali
今天在 http://0daycheck.eastgame.net/ 上看到一个 ORM 工具—— Dali ,号称最大能省 80% 的代码。 Down 下来看看,原来它将传统 ORM 中的配置文件省了,而且在实体映射类中加入了与数据操作相关的事件,还有代码生成工具。做简单的应用确实方便了很多。
今天在
http://0daycheck.eastgame.net/上看到一个ORM工具——Dali,号称最大能省80%的代码。Down下来看看,原来它将传统ORM中的配置文件省了,而且在实体映射类中加入了与数据操作相关的事件,还有代码生成工具。做简单的应用确实方便了很多。
把它给的两个例了贴出来,大家一看便知道有多方便了。
不使用Dali的例子:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13namespace 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
使用Dali的例子: