Refactoring the 23rd day Reference parameter object (introduce Parameter object)

Source: Internet
Author: User

Understanding : Sometimes one of our methods requires a lot of parameters, too many parameters, not easy to read and understand, we can encapsulate multiple parameters into an object.

Detailed :

Refactoring Pre-code:

 1  public  class   registration  2   { 3  public  void  Create (decimal  amount, Student Student, ienumerable<course> courses, decimal   credits)  4   { 5  //  do work  6  }  7 } 

It is often helpful to create a class of user-passed parameters in this case, which makes the code easier to understand and more flexible, because when you need to add a parameter, you only need to give the parameter Class A property. Note that this refactoring should be applied only when you find that the parameters of the method are relatively long, and it is not necessary to apply this refactoring if the method has fewer parameters, because the refactoring increases the number of classes in the system and also increases the maintenance burden. So it depends on the condition of the parameter.
The refactoring code is as follows:

1  Public classRegistrationcontext2     {3          Public decimalAmount {Get;Set; }4          PublicStudent Student {Get;Set; }5          PublicIenumerable<course> Courses {Get;Set; }6          Public decimalCredits {Get;Set; }7     }8 9      Public classRegistrationTen     { One          Public voidCreate (registrationcontext registrationcontext) A         { -             // do work -         } the}

When the parameters of a method are more, whether it is a large or medium-sized project or a small project, you will encounter this scenario, it is recommended that you use this refactoring. This encapsulation idea is also used in SOA, which encapsulates the input message, encapsulates the output message, and the interaction between the message and the message and the message forms the entire application system.

Refactoring the 23rd day Reference parameter object (introduce Parameter object)

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.