Object-Oriented Programming sample (C #)

Source: Internet
Author: User
Object Oriented Programming , OOP

This article describes how to use object-oriented thinking Programming

The following order is used as an example.Web, Fill in the contact information, the service you have purchased (just a fewCheckboxTo buy a few services, select a fewCheckbox, The price is calculated based on the selected service), online payment; after clicking the "Submit" button, the following operations will be performed: save to the database, pay by credit card, generate an orderPDFAnd send an email to notify the customer.

 

AverageCodeFor example:

Public Class Ordercontroller

{

OrderdalDal =New Orderdal();

 

Public GuidPlaceorder (OrderinfoOrderinfo)

{

// Verify the function, omitted

 

GuidOrderid =Guid. Empty;

BoolSaveok =False;

Using(TransactionscopeTS =New Transactionscope())

{

Orderid = Dal. insertorder (orderinfo );

If(! Orderid. Equals (Guid. Empty ))

If(Paymentservice. Pay (orderinfo. Price ))

Saveok =True;

 

If(Saveok)

TS. Complete ();

}

 

If(Saveok)

{

Generateneworderpdf (orderinfo );

Sendemail2client (orderinfo );

}

 

ReturnOrderid;

}

 

Private VoidSendemail2client (OrderinfoOrderinfo)

{

Throw New Notimplementedexception();

}

 

Private VoidGenerateneworderpdf (OrderinfoOrderinfo)

{

Throw New Notimplementedexception();

}

}

 

The above code isMartin folwerSaid"Transaction Script Mode"That is, a business method is written in a function, and a large part is written from start to end.

 

What should I do if I want to write in object-oriented mode? The answer is (I personally think...Haha): Draw a sequence diagram and encode it.

This is because the use of sequence diagrams can help you find objects and communication methods between objects, as shown below:

This figure seems complicated. Therefore, only when the system is complicated (business logic) Can we choose to split objects in an object-oriented manner. The benefits include:

1.Each object is actually a separation of concerns, so it is easy to maintain

A.For example, to verify it, the modification is very targeted and it is easy to locate the class to be modified.

B.The calculation price is the same, and it is easy to locate the location where the change is needed.

2.In unit testing, you can test each class separately without havingPlaceorderWrite countless test functions (divide and conquer)

A.For "transaction Script Type ",PlaceorderWrite unit test&&Code coverage is required& Gt; = 70%That requires a lot of verification.

B. OOP the code can be used to write unit tests for each class , code coverage = 70% easier to achieve

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.