Sunsonic 3.0 ORM open-source framework Learning

Source: Internet
Author: User
Tags connectionstrings

Introduction to subsonic 3.0

The subsonic3.0 ORM framework was introduced to the series of C # frameworks (links here) developed by allempty from scratch. The content of this article comes from allempty.

Subsonic is an open source ORM framework. The author is robe conery, written in C. It includes a T4 template to quickly generate data layer entity classes, which is a practical and rapid development framework. Allows developers to splice SQL statements in principle and focus on the implementation of business logic. Subsonic 3.0 supports three databases: MSSQL, MySQL, and SQLite. The database is flexible and the generated SQL statements are automatically optimized. Although it is an ORM framework, however, there is not much performance loss. It is easy to use and is a great ORM development framework.

My personal experience in the use process is: one-time writing and lifelong use. Write the required functions in the T4 template, and use the powerful custom functions of the T4 template to quickly and accurately generate your own entity classes. You can query select classes and object classes, support for LINQ, support for Aggregate functions, support for stored procedures, and use the distinct keyword. (Some features are added to allempty. I use his modified version ).

Subsonic 3.0 Configuration

Create a project (the download link for the entire solution is later in this article), reference subsonic. Core and Castle. dll, and add the subsonic folder. The file structure of the entire project is as follows:

Then you need to configure the database connection. Here we will introduce MSSQLServer. Open setting. ttinclude In the subsonic folder of the subsonictest project. The file location is shown in the red box:

Open the file and find the following content at the beginning:

The database connection string pointed by the Red Arrow corresponds to the connectionstrings name in the web. config configuration file, and the data name corresponds to the database in connectstring. The connectionstrings configuration method (take my example) is shown in the red box:

The database connection string name in must be consistent with that in the web. config configuration file. Otherwise, an error occurs. After the problem persists, use the T4 template to generate the database entity class as follows:

The generated object classes are as follows:

Subsonic 3.0 is simple and practical

Create An ASPX Form file in the subsonictest project. The name is test. aspx. The corresponding CS Background File Code is as follows:

Using system; using system. collections. generic; using system. data; using system. LINQ; using system. text; using subsonic. LINQ. structure; using subsonic. query; using system. web; using system. web. ui; using system. web. UI. webcontrols; using solution. dataaccess. datamodel; namespace subsonictest {public partial class test: system. web. UI. page {protected void page_load (Object sender, eventargs e) {// Add record var branch = new branch {deptcode = SPs. p_branch_getmaxbranchdeptcode (2, 1 ). executescalar () + "", name = "Network Department", comment = "Network Problem Solving center", parentid = 1, sort = 8, depth = 2 }; // save to database branch1.save (); // print out s printimpinfo ("record added successfully, added id =" + branch1.id ); // query the added record and modify var branchmodel = branch. singleordefault (x => X. comment = "Network Problem Solving Center"); branchmodel. name = "after-sales service department"; branchmodel. save (); // Delete the branch record you just added. delete (x => X. comment = "Network Problem Solving Center"); If (branch. exists (x => X. comment = "Network Problem Solving Center") {printimpinfo ("");} else {printimpinfo (" ");} // query all data in branch var list = branch. all (); foreach (VAR branch in list) {printimpinfo (string. format ("name = {0}, comment = {1}, add date = {2}", Branch. name, Branch. comment, Branch. adddate. tostring ("yyyy-mm-dd hh: mm: SS ")));} /// use the function attached to the class object to query printimpinfo ("Use the function query attached to the Class Object"); var braanchlist = branch. find (x => X. id> 0); foreach (VAR branch in braanchlist) {printimpinfo (branch. id. tostring (); printimpinfo (branch. name); printimpinfo (branch. adddate. tostring ("yyyy-mm-dd hh: mm: SS");} printimpinfo ("----------------------"); // paging function, get 2nd page records (5 records per page) printimpinfo ("Get 2nd page records (5 records per page"); var IL = branch. getpaged ("id desc", 2, 4); foreach (VAR branch in IL) {printimpinfo (branch. id. tostring (); printimpinfo (branch. name); printimpinfo (branch. sort. tostring (); printimpinfo (branch. depth. tostring (); printimpinfo (branch. parentid. tostring (); printimpinfo (branch. adddate. tostring ("yyyy-mm-dd hh: mm: SS");} printimpinfo ("------------------------"); // using the database name + dB, you can directly call the aggregate function printimpinfo ("database name + dB can directly call the aggregate function"); var DB = new subsonictestdb (); // average value printimpinfo ("average value" + dB. AVG <position> (x => X. ID ). where <position> (x => X. ID <12 ). executescalar () + ""); // calculates the number of printimpinfo ("calculated quantity" + dB. count <position> (x => X. ID ). executescalar () + ""); // calculates the total printimpinfo ("total computing volume" + dB. sum <position> (x => X. ID ). executescalar () + ""); // maximum printimpinfo ("maximum" + dB. max <position> (x => X. ID ). executescalar () + ""); // minimum value printimpinfo ("minimum value" + dB. min <position> (x => X. ID ). executescalar () + ""); // method of calling Stored Procedure var OBJ = SPs. p_branch_getmaxbranchdeptcode (1, 0 ). executescalar (); printimpinfo ("Stored Procedure Call OBJ =" + OBJ); // you can use printimpinfo ("LINQ query method") to query printimpinfo "); vaR query = new query <branch> (dB. provider); var posts = from P in query where p. deptcode. startswith ("0101") select P; foreach (VAR branch in posts) {printimpinfo (branch. tostring ();} query = dB. getquery <branch> (); posts = from P in query where p. id> 3 & P. ID <6 select P; foreach (VAR branch in posts) {printimpinfo (branch. tostring ();} var newbranchlist = query. tolist <branch> (); foreach (VAR branch in newbranchlist) {printimpinfo (branch. tostring ();} // The join query method of the LINQ multi-Table Association (printimpinfo ("join query method of the LINQ multi-Table Association"); var query1 = from B in position. all () join P in branch. all () on B. branch_id equals P. ID where p. deptcode = "0101" select P; foreach (VAR branch in query1) {printimpinfo (branch. tostring ());}} /// <summary> /// print the information to the output window in debug mode. /// </Summary> /// <Param name = "info"> </Param> private void printimpinfo (string info) {system. diagnostics. debug. writeline (Info );}}}

Add breakpoint debugging (debug is required) to output the printed content, as shown in the following figure (the shortcut key for opening the output window is CTRL + ALT + O ):

Thanks to allempty for letting me understand the subsonic ORM framework.

The entire solution is as follows:

Click here to download

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.