Object-oriented application in database applications (dotnet)

Source: Internet
Author: User
Tags count dotnet inheritance table name
Program | objects | data | Application of database object-oriented in database application (dotnet)



Today's application is a large part of the database-related programs, and write database programs will involve a lot of data tables, access and manipulation of data tables constitute the most common database application action, so, write efficient program for programmers is to have to consider. This article will discuss this topic, hope to be helpful to the reader friend.



Object-oriented is the universal programming idea of today's program, he has three basic features: encapsulation, inheritance and polymorphism. Inheritance is very effective for the reuse of code, polymorphism refers to a variety of functions (methods), can be overridden by subclasses of the parent class to change the behavior of the object, in object-oriented programming plays a very important role. So, in the practical application, in the end what role can play? Perhaps the best way to express this idea is by using examples.



For example, we want to write a simple forum program, after analysis, you can draw the following table: User table (users), plate table (Bbsblock), reply table (Reply), topic table (TOPIC), etc. (for the convenience of code inheritance, I set the ID number of all the tables to the same name: ID. For these tables, there are some of the same actions: browsing, deleting, adding, and modifying. So, do we write the corresponding method for each table to implement it? Shows that this method is clumsy. And inheritance will play a very important role here. The idea is: Write a parent class, write these basic actions, and then abstract each table into a class and inherit the parent class that you just created, at which point all subclasses have these basic operations.



We can define the parent class like this:

public class Dbbaseclass

{

Protected string tablename;//table name

Protected SqlConnection con;//Connection object



Public Dbbaseclass (): This ("Users")

{

}

<summary>

Constructors

</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>

Get Data Set

</summary>

<param name= "Count" > If Count is 0, get all datasets or get a record of the specified number of bars (from top) </param>

<returns> return to 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 to get the dataset, while defining two fields that work very well, one for table names and one for connection objects. When other classes inherit this class, they no longer need to define tables and connection objects again, and most of all, these two fields play a key role in our better implementation of inheritance.



Next, we create a subclass: Users. This class is an abstraction of table users:

public class User:dbbaseclass

{

<summary>

Non-parametric constructor

</summary>

Public User (): Base ("Users")

{

}



<summary>

Constructors

</summary>

<param name= "tablename" > table name </param>

Public User (String tablename): Base (tablename)

{

}

}



Now, as you can see, we just wrote the two constructors for the class, and we have the functionality to return all the datasets in the table because the table inherits Dbbaseclass.



Again, we write a subclass: Topic, which is an abstraction of the table Topic.

public class Topic:dbbaseclass

{

Public Topic (): Base ("Topic")

{

}



Public Topic (String tablename): Base (tablename)

{

}

}



As with user, the class also has the ability to return all datasets.



When instantiating subclasses, a simple object factory design pattern is used to return different types of objects.

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 ();

}

}

}



Here's a look at how to use:

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 have in mind after reading? If you are an experienced programmer, this approach is sure to be used often, and if you're just in touch, it's good to understand this idea.



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.