Asp. NET development, from two to three layers to object-oriented (3)

Source: Internet
Author: User
Tags access database

Continue to the previous article "ASP." NET development, from two to three layers to object-oriented (2) We learned how to create objects, separate the Bizbase class from the page level, and decouple the degree of coupling. Pro is more pro, sparse more sparse.

The above uses access data and will not reach the requirements for extensions and upgrades. While the program is evolving, it is possible to turn to SQL database or to outsource some modules. In this case, we need to customize the design specification interface. Take over this soft development team, where the logic layer, the implementation of this interface can be.

The following attempts to write an interface, the team that applies SQL Server in the program, whether it is writing SQL statements in the logical layer, or passing parameters in the logical layer to reference stored procedures, can implement a unified interface.

The return DbDataReader function, labeled 2 above, is for it to allow Access database (System.Data.OleDb.OleDbDataReader) and SQL database ( System.Data.SqlClient.SqlDataReader) can be implemented. Since both are inherited DbDataReader classes.

In this case, the previously written Access database logic class bizbase, the implementation of this interface, it has to be modified to meet the standards of the interface:

Below, implements a logical class that is handy for users who are accustomed to writing SQL statements in the program:

Next, implements a logical processing class, and the program is interoperable with the database, but it works with the SQL Server database's stored procedures. Of course, using stored procedures, it naturally has parameters to appear. Before you prepare, write a class Insus.NET.Parameter:

Now you can create this logic class, which has the Ibiz interface:

It has a property that writes two public, which is the stored procedure used to pass the reference to the program object, as well as the parameters. If the referenced stored procedure has no parameters, pass in null.

        Private string_procedurename; PrivateList<parameter>_parameters;  Public stringprocedurename {Get{return_procedurename;} Set{_procedurename =value;} }         PublicList<parameter>Parameters {Get{return_parameters;} Set{_parameters =value;} }
View Code

There is also a private method, which is used to create parameters. If size is passed in as-1, the system will use the default data type length.

 PrivateSqlParameter CreateParameter (stringParameterName, SqlDbType dbType, Int32 size,Objectvalue)            {SqlParameter SqlParameter; if(Size >0) SqlParameter=NewSqlParameter (parametername, dbType, size); ElseSqlParameter=NewSqlParameter (parametername, DbType); Sqlparameter.direction=ParameterDirection.Input; Sqlparameter.value=value; returnSqlParameter; }
View Code

BIZBASESQLSP in this category, each method or function has several lines of code redundancy:

To solve the redundancy, you can extract it and write a private method:

The BIZBASESQLSP class after refactoring:

If you do not want to integrate multiple databases, you fully extract one of the logical processing classes as your class library. Especially the last one. No need to ask the following code for this class library:
Of course, it certainly can be used in ASP.

This demo source program (Access DB;. NET Framework 4.0;):

Note: The article is too long, write a sequel, please pay attention to ...

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: 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.