Extaspnet application tips (11)-subsonic configuration and usage

Source: Internet
Author: User
Preface

I have tried many ORM tools in DOTNET (only in dotnet2.0), including ibatis. net, nhibic, mygeneration doodads, Microsoft entlib, subsonic, etc. I think the simplest option is doodads. The best option is subsonic.

Therefore, in the appbox project, I have no doubt about choosing subsonic AS THE ORM tool. To be compatible with dotnet2.0ProgramWe will not use the latest version of subsonic3.0, but subsonic2.2. Because Version 2.2 has been stable for about two years, there should be no major bugs and it is easy to find a solution on the Internet.

Conventions take precedence over configurations

This is a general programming rule developed by Ruby on Rails. Subsonic attaches great importance to this rule and has some useful Conventions when creating database tables:
1. Each table should contain the following four columns (not required), so that these fields can be easily modified when subsonic is used to create and update functions:

    • Createdon (datetime)
    • Createdby (nvarchar (50 ))
    • Modifiedon (datetime)
    • Modifiedby (nvarchar (50 ))

2. If you want to use logical deletion, you can add the column: deleted (BIT)
In this way, when you call the delete static method of the database table ing Class, subsonic first checks whether the table has the Deleted field. If yes, It is set to true. Otherwise, the destory function is called, that is, permanently deleted.

3. Table Name and column name conventions.

    • Table Name in singular format
    • The column name must not contain reserved words (such as system or string)
    • The column name must not be the same as the table name.

So how to use subsonic in the ASP. net2.0 webapplication project is as follows:

    • Create databases and tables
    • Configure web. config
    • Connect to the database through sonic.exe to generate all ing classes
    • Use the cring class to perform crud on the database

Create databases and tables

First, create a new database appbox in sqlserver2005 and then create a user table:

As you can see, we have added createdon, createdby, modifiedon, and modifiedby according to the subsonic conventions. However, logical deletion is not used.

Configure web. config

<Configsections> <section name = "subsonicservice" type = "subsonic. subsonicsection, subsonic "/> </configsections> <connectionstrings> <clear/> <Add name =" default "connectionstring =" Password = sa; persist Security info = true; user ID = sa; initial catalog = appbox; Data Source =. "/> </connectionstrings> <subsonicservice defaultprovider =" default "> <providers> <clear/> <Add name =" default "type =" subsonic. sqldataprovider, subsonic "connectionstringname =" default "generatednamespace =" appbox "/> </providers> </subsonicservice>

In the connectionstrings configuration section, we run the database connection string (Name: Default) and specify connectionstringname as default in the subsonicservice configuration section. This specifies the database connection for subsonic.

Generatednamespace = "appbox" is used to prepare for the next step.CodeThe automatically generated ing Class has the appbox namespace.

Generate all ing classes through sonic.exe

Save the following command line as a batch processing file (for example, generate. BAT), double-click to run the command to generate all database ing Class Code (you only need to re-execute this command once after the table is changed ).

 
. \ Library \ subsonic \ sonic \ sonic.exe generate/config. \/out. \ business \ subsonic

In this command:

    • /Config .\
      Run subsonicservice in Web. config in the current directory to establish a database connection. Therefore, generate. BAT should be in the same directory as Web. config.
    • /Out. \ business \ subsonic
      Specify the output directory as the subsonic subdirectory under the business directory in the current directory.

Let's take a look at the generated file:

In fact, we don't have to worry about what these files are. We just need to know how to call them. In working with activerecord and working with the query tool, you can read 99% of the call methods used throughout the project.
Believe me, it's really easy.

Use subsonic

Here is only an example of querying users. For more usage instructions, refer to the link given above.

String username = tbxusername. text. trim (); string Password = tbxpassword. text. trim (); xuser user = new select (). from <xuser> (). where (xuser. namecolumn ). isdue to (username ). and (xuser. enabledcolumn ). isdue to (true ). executesingle <xuser> ();

If you want to run the code used in this chapter, you can download all from the http://appbox.codeplex.com/SourceControl/ListDownloadableCommits.aspxSource code.

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.