Lightweight ORM framework-petapoco

Source: Internet
Author: User

The ORM framework plays a very important role in development. I have used several of them before. Some of them are developed by the company itself, and some are third-party, however, in a project that is not very large, I think this petapoco is completely competent and easy to use. Next I will give a brief introduction.

First, add petapoco to the project:

Then configure the database connection parameters and enter the namespace (of course, you must add nodes to the configuration file ):

Then, the corresponding entities of the database will be automatically generated (very convenient) after saving. Below is a simple addition, deletion, modification, and query for testing:

 1             //查询 2             var v = db.Fetch<BTest>("select * from BTest where Keyid=‘123‘")[0]; 3             int Id = v.Id; 4             string Keyid = v.Keyid; 5             string name = v.Bname; 6             v.Bname = "李四"; 7             //修改,添加 8             v.Save(); 9             //删除10             //v.Delete(); 11             //单个实体查询12             //var c = db.SingleOrDefault<BTest>("where Keyid=‘123‘");13             //Response.Write(c.Bname);14             //非常简单的事务15             //事务开始16             //db.BeginTransaction();17             //try18             //{19             //    var c = db.Execute("update btest set bname=‘张三‘  where keyid=‘123‘");20             //    int f = db.Execute("insert into btest(bname,keyid)values(‘老四‘,‘234‘)");21             //    提交事务22             //    db.CompleteTransaction();23             //    int d = db.Execute("insert into btest(bname,keyid)(‘老三‘,‘234‘)");24             //}25             //catch26             //{27                    //事务回滚28             //    db.AbortTransaction();29             //}    
View code

The following is a petapoco page:

1. First, let's look at the paging model:

    
 1        public class Page<T> 2     { 3         /// <summary> 4         /// 当前页码 5         /// </summary> 6         public long CurrentPage  7         {  8             get;  9             set; 10         }11 12         /// <summary>13         ///总页码14         /// </summary>15         public long TotalPages 16         { 17             get; 18             set; 19         }20 21         /// <summary>22         /// 总条数23         /// </summary>24         public long TotalItems 25         { 26             get; 27             set; 28         }29 30         /// <summary>31         /// 每页显示条数32         /// </summary>33         public long ItemsPerPage 34         { 35             get; 36             set; 37         }38 39         /// <summary>40         /// 记录集合41         /// </summary>42         public List<T> Items 43         { 44             get; 45             set; 46         }47 48         /// <summary>49         ///自定义任何值50         /// </summary>51         public object Context 52         { 53             get; 54             set; 55         }56     }
View code

2. demo for paging:

    
 1             //cts是指请求的页码,ts是指没页显示的条数 2            Page<UsersT> pf = db.Page<UsersT>(cts, ts, "select * from userst order by id  desc"); 3             content.Text = "当前第" + pf.CurrentPage + "页" + " " + "总共" + pf.TotalPages + "页" + " " + "总共" + pf.TotalItems + "条记录" + " " + "每页显示" + pf.ItemsPerPage + "条记录"; 4             foreach (var item in pf.Items) 5             { 6                 string str = "</br>" + item.ID + " "; 7                 str += item.UserZhangFu + " "; 8                 str += item.Mobile; 9                 str += "</br>";10                 content.Text += str;11             }    
View code

I set the breakpoint and read it. Finally, the database is requested in the form of this SQL statement:

 

The final effect is as follows:

Sometimes retrieving data from a database may involve multiple tables, and the database does not have a corresponding view. Therefore, a temporary object is required, which is similar to the view model in MVC. Below is a test object I added:

    
1     public class TestDB2     {3         public string UserZhangFu { get; set; }4         public string Mobile { get; set; }5         public string Bname { get; set; }6     }
View code

Similar to the previous one, you do not need to perform any configuration easily:

    
1             var v = db.Fetch<TestDB>("select UserZhangFu,Mobile,Bname from Userst as a left join Btest as b on a.id=b.id where a.id=2 ")[0];2             Response.Write(v.UserZhangFu+"  "+v.Mobile+"   "+v.Bname);            
View code

The output result is as follows:

The above is a very simple introduction to petapoco, and this framework is fully open-source, so during use, you can F12 to see how to build SQL statements at the underlying layer, how to assign values to object attributes and so on. In fact, most of the ORM frameworks are similar. Familiar with one, you can basically understand the whole idea of ORM for your reference!

Lightweight ORM framework-petapoco

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.