[Recommended] experience subsonic

Source: Internet
Author: User
  1. Subsonic Introduction
  2. Subsonic Configuration
  3. Use sonic.exe to generate code
  4. Use substage to generate code
  5. Simple operation example

1. Subsonic Introduction

Subsonic is an open-source ORM framework. The author is robe conery, written in C. subsonic follows the Ruby on Rails principles, such as some conventions when creating a table (we recommend that you add createdon [datetime], createdby [nvarchar], modifyon [datetime] to each table. modifyby [nvarchar])..., for more information, see Ruby on Rails.

This ORM framework supports a variety of databases, including SQL Server, MySQL, Oracle, SQLite..., the latest version is V3.0, PS: In this test, I still use v2.2

2. Subsonic Configuration

My test environment: Window 7 + SQL Server 2005 + vs 2010 + subsonic 2.2

Before configuration, download subsonic. The official website Ms cannot be opened. I will publish another link here. You can download it: https://github.com/subsonic/subsonic-3.0,github. this website is very similar to svn :)

First look at the directory after download:

The root directory contains the main dll, and the subcommberdirectory contains an executable file of sonic.exe (based on command lines) to generate code. The other substage directory contains a substage UI tool, which can be used to generate code.

Prepare data in advance:

1: Create a new project using IDE and reference subsonic. dll.

2: Create a test database (SQL Server 2005 is used here). I only create one table for testing.

 

Create Table customer (ID int not null identity (1, 1) primary key, [name] nvarchar (30), [sex] bit)

 

3: Prepare for code generation. After the DLL is introduced in the project, change the web. config configuration. I will directly paste it here. First, let's take a look at the first section, which is mainly a section, which is created by subsonic. DLL subsonicsection for processing (PS: subsonicsection is only available after V2.0, previously named as: sectionservice in V1.0)

 

Note: The connnectionstringname in providers must be consistent with the name in connectionstrings in Web. config to generate an object based on the database to which the connection string points.

 

 

 

<Configsections> <section name = "subsonicservice" type = "subsonic. subsonicsection, subsonic "allowdefinition =" machinetoapplication "placement =" true "requirepermission =" false "/> </configsections> <subsonicservice defaultprovider =" sqldataprovider "> <providers> <Add name =" external ""type =" subsonic. sqldataprovider, subsonic "connectionstringname =" Steven connectstring "generatednamespace =" Steven. dal "> </Add> </providers> </subsonicservice> <connectionstrings> <Add name =" Steven connectstring "connectionstring =" Server = Steven; initial catalog = Steven; user ID = sa; Password = 123; "providername =" system. data. sqlclient "/> </connectionstrings>

 

Let's take a look at the second configuration. before talking about this configuration, let's talk about it first. abp-subsonic is used to generate database operation objects based on the abp-file. The abp-file is actually a basic text format. I only know its two usage methods:

 

A: * all tables in the specified database are generated.

 

B: If you do not need to generate all tables, you can list the names of the tables to be generated (one table name for each row)

 

 

<Compilation DEBUG = "true" targetframework = "4.0"> <buildproviders> <add extension = ". ABP "type =" subsonic. buildprovider, subsonic "/> </buildproviders> </compilation>

This determines when the compiler is building. A processing method of the ABP file, the specific work is thrown to subsonic. buildprovider, in fact, buildprovider according to our configuration. to generate code.

Let's take a look at the figure after talking about so many. ABP files.

 


 

This ABP file is very simple. The content is a "*" number, used to generate all the objects that manipulate the table. In fact, I am also a customer table, PS: the generated code can be generated only when the files are put under app_data by using the compilation mechanism (the compiler builds the folder during build.

 

3: Use sonic.exe to generate code

 

The reason for this step is that I encountered some problems during the build process, and the code was not successfully generated (the official explanation is the reason for trust level, subsonic must be in a full trust host environment, I tried to modify the trust level,

 

This includes modifying web. config and using the. NET Framework configuation tool to modify.... Some friends have told me that they have not yet done so :)

 

To use the sonic.exe (mentioned above) command to generate code, vs ide tool has a function that can load external tools into the "Tools" menu, you can also provide some input parameters for these external tools, as shown in the figure:

 

 

Use "tools-> external tools..." to open.

 

After successful addition, you can see that there is a menu item in the "tool" menu, for example:

 

 

Directly execute the sonic.exe command,

 

 

Here is the generate/Out Generation Code of a parameter, which can be viewed by running sonic.execommand directly.

 

After confirmation, the DTO object will be generated based on the content contained in the. ABP file. PS: note that after generation, it cannot be seen in VS solution and must be included.

 

4: Use substage to generate code

 

This GUI tool generates more powerful code, so I will take a separate section to introduce it. First, let's look at its overall UI,

 

 

A Brief Introduction:

 

Substage generates code based on provide in the project, and the provider must use the database connection string to find the specified database.

 

Code Generation steps:

 

A: firstly, new project first

 

B: create a connection string, for example:

 

 

C: Create a provider and use the connection string in B, for example:

 

 

D: Finally, generate the code. Click Generate code in the toolbar to generate the code,

 

 

 

As you can see, in addition to the table name generated into the external body, there are several additional classes, SPS. CS is used to process stored procedures, and customer is used to operate the customer table. This class also helps us to generate a customercollection collection class, which is not available to other ORM frameworks. :), allstruct. CS contains all table structures. you need to learn more about the details.

 

Next let's take a look at the most special part of this tool. After the code is generated, we can use the GUI tool to invoke our database structure to make all the tables... the list on the left is displayed as follows:

 

 

With the table, the tool provides a feature called scaffolds (stent). We can use this feature to perform curd operations on the table directly to witness the following:

 

 

PS: before the operation, you need to select "table" first, and then click the scaffolds tab to see how to generate the UI for us. We can directly use the UI to add a data, after the data is added, a list interface is displayed,

 

 

This is good. In addition to this, you can also use this tool like msdn and view it through api reference.

 

 

If you have other functions, you need to find them by yourself.

 

5. Simple operation example

 

After all the work is done, I wrote a very simple example to verify its correctness. The code is very simple.

 

Protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {binddata () ;}} private void binddata () {gridview1.datasource = customer. fetchall (); gridview1.databind ();} protected void btnadd_click (Object sender, eventargs e) {string custname = textbox1.text; customer model = new customer (); Model. name = custname; model. sex = chksex. checked; model. save (); binddata ();}

 

I add a new customer using the SAVE () method, and then fetchall () gets all the data for display. it is very simple. Of course, there are other methods such as fetchbyparameter, delete, select ...... I will not discuss it here. The main purpose of this article is to experience it ,:)

 

 

To sum up, sobsonic is only one of the r of many ORM frameworks. The specific application can be determined based on the project requirements. I have not found a major performance problem with subsonic in my current project, I like it more easily than other Orm, and it is open-source :). If you are interested, you can communicate more!

PS: we provide you with a game Trial Platform. If you are interested, you can check it out!Http://www.juxiangyou.com/r4357700

 

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.