Object-oriented applications in database applications (DOTNET)

Source: Internet
Author: User
Tags dotnet
Current Application Program A large part of them are database-related programs, and writing database programs involves many data tables. Accessing and manipulating Data Tables constitute the most common actions of database applications. Therefore, writing efficient programs has to be considered by programmers. This article will discuss this topic, hoping to benefit readers and friends.


Object-oriented is a universal programming concept in today's programming world. It has three basic features: encapsulation, inheritance, and polymorphism. For Code The multiplexing of is very effective. polymorphism refers to multiple forms of functions (methods ).
The method of rewriting the parent class by subclass to change the object behavior plays a very important role in object-oriented programming. So what role can it play in practical applications? Perhaps the example can best express this
Idea.


For example, we need to write a simple forum program. After analysis, we can obtain the following tables: User table (users), Forum table (bbsblock), and reply table (reply), topic table
(Topic) (to facilitate code inheritance, I set the idnumbers of all tables to the Same Name: ID ). These tables have the same operations: browsing, deleting, adding, and modifying. So,
Do we write corresponding methods for each table to implement it? This method is clumsy. Inheritance will play an extremely important role here. The idea is: Write a parent class and write these basic operations
Well, then, each table is abstracted into a class and inherits the parent class just created. At this time, all subclasses have these basic operations.

We can define the parent class as follows:

Public class dbbaseclass
{
Protected string tablename; // table name
Protected sqlconnection con; // connection object
Public dbbaseclass (): This ("users ")
{
}

/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "tablename"> table name </param>
Public dbbaseclass (string tablename)
{
This. tablename = tablename;
If (con = NULL)
{

Con = new
Sqlconnection ("Server = ACCP-lzh; database = missbbs; uid = sa; Pwd = sa ");
}
Else
{
If (con. State = connectionstate. open)
Con. Close ();
}
}

/// <Summary>
/// Obtain the dataset
/// </Summary>
/// <Param name = "count"> If count is 0, all datasets are obtained; otherwise, records of the specified number (from the top) are obtained. </param>
/// <Returns> return dataset </returns>
Public dataset select (INT count)
{
String SQL;
If (COUNT = 0)

SQL = "select * from" + this. tablename;
Else

SQL = "select top" + count. tostring () + "* from" + this. tablename +
"Oreder by id desc ";
Sqlcommand selectcmd = new sqlcommand (SQL, con );
Sqldataadapter adapter = new sqldataadapter ();
Adapter. selectcommand = selectcmd;
Dataset DS = new dataset ();
Try
{
Con. open ();
Adapter. Fill (DS, "bbstable ");
Con. Close ();
}
Catch (exception)
{
Return NULL;
}
Return Ds;
}
}


In this class, we define two overloaded constructors and a method for getting a dataset. At the same time, we define two fields that play a major role, one is the table name, one is the connection object. When other classes inherit this
When using a class, you no longer need to define tables and connection objects again. Most importantly, these two fields play a key role for us to better implement inheritance.

Next, we create a subclass: users. This class is the abstraction of the table users:
Public class user: dbbaseclass
{
/// <Summary>
/// No parameter Constructor
/// </Summary>
Public user (): Base ("users ")
{
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "tablename"> table name </param>
Public user (string tablename): Base (tablename)
{
}
}

Now, we can see that we only write two constructor functions of this class to return all data sets in the table, because the table inherits dbbaseclass.

Similarly, we will write another subclass: topic, which is the abstraction of the table topic.

Public class topic: dbbaseclass
{
Public topic (): Base ("topic ")
{
}
Public topic (string tablename): Base (tablename)
{
}
}

Like a user, this class also provides the ability to return all datasets.

During subclass instantiation, different types of objects are returned in the simple object factory design mode.

Public class factory
{
Public Factory ()
{
}

Public static dbbaseclass GetObject (string tablename)
{
Switch (tablename)
{
Case "users ":

Return new user ();
Case "topic ":

Return new topic ();
Case "bbsblock ":

Return new bbsblock ();
Case "reply ":

Return new reply ();
Case "bbsmaster ":

Return new bbsmaster ();
Default:

Return new dbbaseclass ();
}
}
}

Let's take a look at how to use it:
User user = (User) Factory. GetObject ("users ");
Dataset ds1 = new dataset ();
Ds1 = user. Select (0 );
Topic topic = (topic) Factory. GetObject ("topic ");
Dataset ds2 = new dataset ();
Ds2 = topic. Select (0 );

What do you think after reading it? If you are an experienced programmer, this method will always be used. If you are new to it, understanding this idea is quite helpful.
 

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.