Talk randomly
As a novice, I have never had the courage to write a share. There are many reasons: such as: your own limited level, inaccurate language expression, and no technical point of writing is ridiculed. Today I listened to a sharing from internal employees in the company. The most important point is: the best way to improve one's own level is communication. No matter what channel you use, whether it is communication, sorting it into text sharing, etc. are all good ways. Therefore, today I will write a sharing of my own experience. Welcome the advice of all the great masters!
Requirement background
There is a requirement in the requirements I received today, as shown in the figure below, and it is necessary to print out documents such as those presented in Excel content.
Hands-on version 1
In order to realize this business, data of three tables is required. (Table of storing documents, review opinion form, review status form)
The relationship between three tables: Document Table 1: 1 audit status table, Document Table 1: N audit opinion table
In order to make the View page tidy, I defined a SpecialPrintModel class
public class SpecialPrintModel { /// <summary> /// Supplier undertakes /// </summary> public string SupplierUnderTaker { get; set; } /// <summary> /// Customer Order Number /// </summary> public string CustomerSerialNumber { get; set; } /// <summary> /// Payment amount /// </summary> public decimal PayAmount { get; set; } /// <summary> /// Account opening bank /// </summary> public string OpeningBank { get; set; } /// <summary> /// Payment unit /// </summary> public string CollectionMonad { get; set; } /// <summary> /// Bank account number /// </summary> public string BankAccount { get; set; } /// <summary> /// The person in charge /// </summary> public string ResponseiblePerson { get; set; } /// <summary> /// The leader in charge /// </summary> public string Leader { get; set; } /// <summary> /// Financial audit /// </summary> public string FinanceApproval { get; set; } /// <summary> /// Financial Manager Review /// </summary> public string FinanceManagerApproval { get; set; } /// <summary> /// Review by the Financial Director /// </summary> public string FinanceDirectorApproval { get; set; } /// <summary> /// CEO review /// </summary> public string CEOApproval { get; set; } /// <summary> ///Spring number /// </summary> public string SerialNumber { get; set; } }
public List<ShipSpecialPrintModel> GetTobePaidRecepit(ShipSpecialSearch search) { List<ShipSpecialPrintModel> curiseShipModel = new List<ShipSpecialPrintModel>(); var toBePaidModel = (search);//Find out the document table information to be paid ArrayList serialArray=new ArrayList();//Define a list of flow numbers ((u) => { (); }); var toBePaidComment = (serialArr);//Find out the review opinion table of the documents to be paid (how many review opinions are required for one document) foreach (var item in toBePaidModel) { ShipSpecialPrintModel temp = new ShipSpecialPrintModel() { SupplierUnderTaker = supplierUnderTaker; CustomerSerialNumber = ; PayAmount = ; OpeningBank = ; CollectionMonad = ; ResponseiblePerson = ; SerialNumber = ; }; (temp); } foreach (var curise in curiseShipModel) { foreach (var comment in toBePaidComment) { if ( == ) { if ( == (int)) { = ; } else if ( == (int)) { = ; } else if ( == (int)) { = ; } else if ( == (int)) { = ; } else if ( == (int)) { = ; } } } } return curiseShipModel }
Haha, the above code basically fulfills the business needs, but if the business needs to print out the name of the CTO and the name of the CIO, then add it to if else. Although it is very simple, it violates the principle of open-closing. Therefore, I decided to use reflection to complete this if...else thing.
Because if...else determines whether the level of the current document's review opinion table is equal to the level corresponding to the SpecialPrintModel field. If it is equal, the corresponding name will be written in the corresponding field. I decided to modify the SpecialPrintModel class.
Hands-on version 2
public class ShipSpecialPrintModel { /// <summary> /// Supplier undertakes /// </summary> public string SupplierUnderTaker { get; set; } /// <summary> /// Customer Order Number /// </summary> public string CustomerSerialNumber { get; set; } /// <summary> /// Payment amount /// </summary> public decimal PayAmount { get; set; } /// <summary> /// Account opening bank /// </summary> public string OpeningBank { get; set; } /// <summary> /// Payment unit /// </summary> public string CollectionMonad { get; set; } /// <summary> /// Bank account number /// </summary> public string BankAccount { get; set; } /// <summary> /// The person in charge /// </summary> public string ResponseiblePerson { get; set; } /// <summary> /// The leader in charge /// </summary> [LevelAttribute(Level = 1)] public string Leader { get; set; } /// <summary> /// Financial audit /// </summary> [LevelAttribute(Level = 2)] public string FinanceApproval { get; set; } /// <summary> /// Financial Manager Review /// </summary> [LevelAttribute(Level = 3)] public string FinanceManagerApproval { get; set; } /// <summary> /// Review by the Financial Director /// </summary> [LevelAttribute(Level = 4)] public string FinanceDirectorApproval { get; set; } /// <summary> /// CEO review /// </summary> [LevelAttribute(Level = 5)] public string CEOApproval { get; set; } /// <summary> ///Spring number /// </summary> public string SerialNumber { get; set; } } public class LevelAttribute : Attribute { public int Level { get; set; } }
var toBePaidComment = (((u => ).ToList())); var specialPropertyInfo = (from property in typeof(CuriseShipSpecialPrintModel).GetProperties() where (typeof(LevelAttribute), false).Count() > 0 select property).ToList(); ((item)=>{ ShipSpecialPrintModel temp = new ShipSpecialPrintModel() { SupplierUnderTaker = supplierUnderTaker; CustomerSerialNumber = ; PayAmount = ; OpeningBank = ; CollectionMonad = ; ResponseiblePerson = ; SerialNumber = ; }; var thisComments=(u=> = ).ToList(); ((cm)=> { if(==((typeof(LevelAttribute),false).First() as LevelAttribute).Level) { (model,,null); } }); })
However, I saw that propertyInfos basically needs to reflect and find the elements every time it cycles. In order to avoid such performance consumption, I decided to modify it and define a dictionary to store the fields marked with the SpecialPrintModel.
Hands-on version 3
Dictionary<int, PropertyInfo> dic = new Dictionary<int, PropertyInfo>(); ((myProperty) => { (((typeof(LevelAttribute),false).First() as LevelAttribute).Level,myProperty)); } ); ((cm) => { if (()) { dic[].SetValue(model, , null); } });
After three modifications in general, the code of if...else has been avoided. In this way, it is also more suitable for later, such as printing the name of the CTO review. In that case, you only need to fill in the fields in the Model class and add a special effect to the fields.
Summarize
Just as my colleague said, just think about it and knock it a few more times when doing tasks. Some problems are not problems. OK, let’s stop writing today. Good night everyone!
Please take some time to share the article with your friends or leave a comment. We will sincerely thank you for your support!