Design Model Learning (1)

Source: Internet
Author: User
Tags oracleconnection connectionstrings

Recently, the company developed an archive product to store data in two types of databases (SQL Server and Oracle). Although I did not develop this project, I am motivated to learn, I also began to think about how I would do this project if it was handed over to me (with the leastCodeDevelopment with minimal time to ensureProgramRunning Efficiency ).

Because I have learned the design model before, the first reaction is that the factory model and the abstract factory model can meet this requirement (but the use is too small, and the learning is not very profound, so I will re-learn, I hope you will discuss it together ).

According to my personal understanding, object-oriented design languages have three mechanisms:Encapsulation, inheritance, and Polymorphism. The first two features are easy to understand and are not explained here. What is polymorphism? My understanding is:Polymorphism-different objects implemented by inheritance call the same method and show different behaviors, called Polymorphism. If you do not understand, you can give an example.

From the programming point of view, a dog is an abstract class. A dog has a running attribute, but the running speed varies. VIP and hunting dogs are physical and specific dogs, both of which inherit the nature of a dog.

If the code is used, you can

Dog dog = new VIP dog () or dog = new hound ();

When we give the abstract class dog a specific instance, the running speed of the dog is different. This is one of the manifestations of polymorphism. Do you know what I mean ?.

So what is the design model? This is really abstract, too abstract. After hard work, I found the following paragraph (I think it is easy to understand ):

The design pattern describes a general solution to a certain type of common problems in the software design process. The object-oriented design pattern describes the common organizational relationships between classes and communication objects in specific scenarios during the object-oriented design process.

I personally understand that if there is not much change in the project requirements, we do not need to use design patterns or object-oriented design. Although the C # and Java we use are all object-oriented languages, but our programming habits are usually not much different from the process-oriented writing habits (please correct us if you understand the mistakes ).

I remember that when I was studying software engineering at the University, the teacher gave us a lecture and I always liked the coupling term. I didn't understand it at the time and thought these theories were useless, now we have a clear understanding.

The coupling relationship directly determines the behavior of the software in the face of changes, that is, the tight coupling between modules makes the software in the face of changes, the relevant modules must be changed accordingly, the loose coupling between modules makes it easier for some modules to be replaced or changed when the software is facing changes, so that other modules remain unchanged..

In this theory, the coupling between modules must be loose. It is a matter of discussion and worth thinking.

Several modes may be used in a product, or none of them are used. People will say that it is not good to use the model for the purpose of the model. a deep understanding of object-oriented means learning the inheritance of the design model well.

To learn factory method, you should have the following question: Why is this mode available? What is this mode used for? What is this mode? What should you pay attention to when using this mode.

Motivation: In software systems,An objectBecauseRequirementOfChange, ThisObjects often face drastic changes.But it has a comparisonStable interface.

Intent:Defines an interface used to create objects, so that the subclass determines which class to instantiate (gof ).

A class is changed, but this class has a stable interface. What is a stable interface.

The following is my thinking process.

First, write layers, which is inevitable. Second, different databases actually have different syntax differences between SQL Server and Oracle, such as placeholders. SQL Server is @, Oracle is :, the Database Help classes sqlhelpe are also different, so I will start from sqlhelper.

First, two classes are created, representing the SQL operation and ORACLE data operation classes.

SQL database operation classSqldbhelper

 1   Public   Class  Sqldbhelper   
2 { 3 Static Readonly String Constr = configurationmanager. connectionstrings [ " Constr " ]. Connectionstring; 4 Public Datatable getdatatable ( String SQL, Params Oracleparameter[] PARAM) 5 { 6 Using (Sqlconnection con = New Sqlconnection (constr )) 7 { 8 Con. open (); 9 Using (Sqlcommand cmd = Con. createcommand ()) 10 { 11 Cmd. commandtext = SQL; 12 If (Param! = Null ) 13 { 14 Cmd. Parameters. addrange (PARAM ); 15 } 16 Using (Sqldataadapter adapert = New Sqldataadapter (CMD )) 17 { 18 Datatable sqldt = New Datatable (); 19 Adapert. Fill (sqldt ); 20 Return Sqldt; 21 } 22 } 23 } 24 } 25 }

Oracle Database OperationsOracledbhelper

 1   Public   Class  Oracledbhelper:
2 { 3 Static Readonly String Constr = configurationmanager. connectionstrings [" Constr " ]. Connectionstring; 4 Public Datatable getdatatable ( String SQL, Params Sqlparameter[] PARAM) 5 { 6 Using (Oracleconnection con = New Oracleconnection (constr )) 7 { 8 Con. open (); 9 Using (Oraclecommand cmd = Con. createcommand ()) 10 { 11 Cmd. commandtext = SQL; 12 If (Param! =Null ) 13 { 14 Cmd. Parameters. addrange (PARAM ); 15 } 16 Using (Oracledataadapter adapert = New Oracledataadapter (CMD )) 17 { 18 Datatable extends leldt = New Datatable (); 19 Adapert. Fill (oracleldt ); 20 Return Oracleldt; 21 } 22 } 23 } 24 }

when the database is SQL Server, call the sqldbhelper method getdatatable (). When the database is Oracle, I call the getable able () method of oracledbhelper. Therefore, we need to write two different data operation classes here. I don't think it is very good. After searching, I found a method in the class that comes with net. In this way, programmers do not have to judge whether to call sqldbhelper or oracledbhelper when calling the database operation class in the data layer, please refer to my next blog.

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.