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)