ORM新实现——Dali

 

    今天在http://0daycheck.eastgame.net/上看到一个ORM工具——Dali,号称最大能省80%的代码。Down下来看看,原来它将传统ORM中的配置文件省了,而且在实体映射类中加入了与数据操作相关的事件,还有代码生成工具。做简单的应用确实方便了很多。

    把它给的两个例了贴出来,大家一看便知道有多方便了。

    不使用Dali的例子:


  1 None.gif using  System;
  2 None.gif using  System.Data;
  3 None.gif using  System.Configuration;
  4 None.gif using  System.Collections;
  5 None.gif using  System.Web;
  6 None.gif using  System.Web.Security;
  7 None.gif using  System.Web.UI;
  8 None.gif using  System.Web.UI.WebControls;
  9 None.gif using  System.Web.UI.WebControls.WebParts;
 10 None.gif using  System.Web.UI.HtmlControls;
 11 None.gif using  System.Data.SqlClient;
 12 None.gif
 13 None.gif namespace  DaliWebDemoCS
 14 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 15InBlock.gif    // This code uses SQL Server. To use a different database you will need to
 16InBlock.gif    // change all the SQL Server specific objects such as SqlDataAdapter, 
 17InBlock.gif    // SqlConnection and SqlCommand as well as the using statement above.
 18InBlock.gif
 19InBlock.gif    public partial class CustomerNoDali : System.Web.UI.Page
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 21InBlock.gif        private string connectString;
 22InBlock.gif
 23InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            connectString = (string)Application["ConnectString"];
 26InBlock.gif
 27InBlock.gif            if (!IsPostBack)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 29InBlock.gif                // Populate the customer dropdown list
 30InBlock.gif                FillCustomerIdDropdown();
 31InBlock.gif
 32InBlock.gif                // Load all the form controls with data from the database.
 33InBlock.gif                LoadPage();
 34ExpandedSubBlockEnd.gif            }

 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif
 37InBlock.gif        protected void CustomerID_SelectedIndexChanged(object sender, System.EventArgs e)
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 39InBlock.gif            // Load all the form controls with data from the database.
 40InBlock.gif            LoadPage();
 41ExpandedSubBlockEnd.gif        }

 42InBlock.gif
 43InBlock.gif        protected void SaveCustomerLinkButton_Click(object sender, System.EventArgs e)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 45InBlock.gif            // Save all the form control data to the database.
 46InBlock.gif            SavePage();
 47InBlock.gif
 48InBlock.gif            // Fill the Customer dropdown again in case customer changed. Before 
 49InBlock.gif            // filling, get the current customer. After filling, re-select customer.
 50InBlock.gif            string selectedCustomer = CustomerID.SelectedValue;
 51InBlock.gif            FillCustomerIdDropdown();
 52InBlock.gif            CustomerID.SelectedValue = selectedCustomer;
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif
 55InBlock.gif        private void FillCustomerIdDropdown()
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 57InBlock.gif            // Get a collection of CustomerListItem objects to fill the dropdown.
 58InBlock.gif            string sql =
 59InBlock.gif                "SELECT CustomerID, CompanyName " +
 60InBlock.gif                "  FROM Customers " +
 61InBlock.gif                " ORDER BY CompanyName";
 62InBlock.gif            DataTable table = new DataTable();
 63InBlock.gif            SqlDataAdapter adapter = new SqlDataAdapter(sql, connectString);
 64InBlock.gif            adapter.Fill(table);
 65InBlock.gif
 66InBlock.gif            // Use the table as the data source for the dropdown
 67InBlock.gif            CustomerID.DataSource = table;
 68InBlock.gif            CustomerID.DataValueField = "CustomerID";
 69InBlock.gif            CustomerID.DataTextField = "CompanyName";
 70InBlock.gif
 71InBlock.gif            CustomerID.DataBind();
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74InBlock.gif        private void LoadPage()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            string sql =
 77InBlock.gif                "SELECT * " +
 78InBlock.gif                "  FROM Customers " +
 79InBlock.gif                " WHERE CustomerID = @CustomerID ";
 80InBlock.gif            DataTable table = new DataTable();
 81InBlock.gif            SqlDataAdapter adapter = new SqlDataAdapter(sql, connectString);
 82InBlock.gif            adapter.SelectCommand.Parameters.AddWithValue("@CustomerID", CustomerID.SelectedValue);
 83InBlock.gif            adapter.Fill(table);
 84InBlock.gif
 85InBlock.gif            CompanyName.Text = table.Rows[0]["CompanyName"as string;
 86InBlock.gif            ContactName.Text = table.Rows[0]["ContactName"as string;
 87InBlock.gif            ContactTitle.Text = table.Rows[0]["ContactTitle"as string;
 88InBlock.gif            Phone.Text = table.Rows[0]["Phone"as string;
 89InBlock.gif            Fax.Text = table.Rows[0]["Fax"as string;
 90InBlock.gif            Address.Text = table.Rows[0]["Address"as string;
 91InBlock.gif            City.Text = table.Rows[0]["City"as string;
 92InBlock.gif            Region.Text = table.Rows[0]["Region"as string;
 93InBlock.gif            PostalCode.Text = table.Rows[0]["PostalCode"as string;
 94InBlock.gif            Country.Text = table.Rows[0]["Country"as string;
 95ExpandedSubBlockEnd.gif        }

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

130ExpandedSubBlockEnd.gif    }

131ExpandedBlockEnd.gif}

132 None.gif

  使用 Dali 的例子:
 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Configuration;
 4 None.gif using  System.Collections;
 5 None.gif using  System.Web;
 6 None.gif using  System.Web.Security;
 7 None.gif using  System.Web.UI;
 8 None.gif using  System.Web.UI.WebControls;
 9 None.gif using  System.Web.UI.WebControls.WebParts;
10 None.gif using  System.Web.UI.HtmlControls;
11 None.gif
12 None.gif using  Revelation.Dali;
13 None.gif
14 None.gif namespace  DaliWebDemoCS
15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
16InBlock.gif    [DaliClass(TableName = "Customers")]
17InBlock.gif    public partial class Customer : System.Web.UI.Page
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            if (!IsPostBack)
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                // Populate the customer dropdown list
24InBlock.gif                FillCustomerIdDropdown();
25InBlock.gif
26InBlock.gif                // Load all the form controls with data from the database.
27InBlock.gif                DataManager.Default.Load(Page);
28ExpandedSubBlockEnd.gif            }

29ExpandedSubBlockEnd.gif        }

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

36InBlock.gif
37InBlock.gif        protected void SaveCustomerLinkButton_Click(object sender, System.EventArgs e)
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            // Save all the form control data to the database.
40InBlock.gif            DataManager.Default.Save(Page);
41InBlock.gif
42InBlock.gif            // Fill the Customer dropdown again in case customer changed. Before 
43InBlock.gif            // filling, get the current customer. After filling, re-select customer.
44InBlock.gif            string selectedCustomer = CustomerID.SelectedValue;
45InBlock.gif            FillCustomerIdDropdown();
46InBlock.gif            CustomerID.SelectedValue = selectedCustomer;
47ExpandedSubBlockEnd.gif        }

48InBlock.gif
49InBlock.gif        private void FillCustomerIdDropdown()
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            // Get a collection of CustomerListItem objects to fill the dropdown.
52InBlock.gif            ArrayList customers = DataManager.Default.Find(
53InBlock.gif                typeof(CustomerListItem), null"CompanyName");
54InBlock.gif
55InBlock.gif            // Use the list as the data source for the dropdown
56InBlock.gif            CustomerID.DataSource = customers;
57InBlock.gif            CustomerID.DataValueField = "CustomerID";
58InBlock.gif            CustomerID.DataTextField = "CompanyName";
59InBlock.gif
60InBlock.gif            CustomerID.DataBind();
61ExpandedSubBlockEnd.gif        }

62ExpandedSubBlockEnd.gif    }

63ExpandedBlockEnd.gif}

64 None.gif

  CustomerListItem类:
 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 2InBlock.gif * CustomerListItem.cs
 3InBlock.gif * -------------------
 4InBlock.gif * 
 5InBlock.gif * This file is part of the Revelation Technologies Dali code samples.
 6InBlock.gif *
 7InBlock.gif * Copyright ?2005 Revelation Technologies, LLC.  All rights reserved.
 8InBlock.gif *
 9InBlock.gif * This source code is intended only as a supplement to Revelation 
10InBlock.gif * Technologies Dali development tools and documentation.  It is intended
11InBlock.gif * to demonstrate specific concepts and may not be suitable for all 
12InBlock.gif * applications.  You may freely use this source code in whole or in 
13InBlock.gif * part.  Use of this code in whole or in part acknowledges your acceptance
14InBlock.gif * of the following terms and conditions:
15InBlock.gif *
16InBlock.gif * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
17InBlock.gif * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18InBlock.gif * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR 
19InBlock.gif * PURPOSE. 
20InBlock.gif *  
21ExpandedBlockEnd.gif * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

22 None.gif
23 None.gif using  System;
24 None.gif
25 None.gif using  Revelation.Dali;
26 None.gif
27 None.gif namespace  DaliWebDemoCS
28 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
29InBlock.gif    // This class is used to retrieve the CustomerID and CompanyName for all
30InBlock.gif    // customers.  This data is used to populate the dropdown list.  It uses
31InBlock.gif    // Properties instead of simple fields because the List's data binding 
32InBlock.gif    // only works with Properties.
33InBlock.gif
34InBlock.gif    // Map this class to the customers table
35InBlock.gif    [DaliClass(TableName = "Customers")]
36InBlock.gif    public class CustomerListItem
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
38InBlock.gif        public string CustomerID
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ customerID_ = value; }
41ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn customerID_; }
42ExpandedSubBlockEnd.gif        }

43InBlock.gif        private string customerID_;
44InBlock.gif
45InBlock.gif        public string CompanyName
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ companyName_ = value; }
48ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn companyName_; }
49ExpandedSubBlockEnd.gif        }

50InBlock.gif        public string companyName_;
51ExpandedSubBlockEnd.gif    }

52ExpandedBlockEnd.gif}

53 None.gif

    更重要的是没有数据库映射配置文件。

    不过,表关系现在看还处理不了,只能对单表进行处理,希望听棠大哥的SPL能借荐一下它的思想,把SPL做得更方便实用。:)

    东西不复杂,但价格还是高了,$149,网站:http://www.revtechnologies.com/有兴趣的可以去看看。

    这里有个下载好的版本,可以直接从这里下载

转载于:https://www.cnblogs.com/jinyong/archive/2006/02/20/334156.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值