Comparison of data models, using reflection to be a happy programmer

Source: Internet
Author: User

When I read "big talk Design Patterns", I said it reflected the joy of the programmer. At that time, I didn't quite believe that I would not reflect it after all. I don't even know when to use reflection. I can only check some information and know that there are some reflection.

I have been working on a talent management system over the past few days. One of the reasons is the operation log, especially when I modify each field. Well, there were two decisions at that time. 1. Create a structure that is the same as that of the original table. Use a foreign key and the primary key of the original table to record it. 2. store two models when reading data, one New Model and the other Old Model, and then compare them.

Finally, the second method is selected for various reasons. Well, I used the second method to come up with a question. How can I compare two models? Do I compare fields one by one? My God, this is not a nightmare. if I have 20 fields, I don't want to write more than 20 and compare more than 20 IF fields, it's obviously not the best code, however, there is no good way for me to ask du Niang and GOOOGLE. In this Internet age, I have to make good use of search engines.

The Code should be like this at the beginning. There are two entity variables that store the information before and after modification. For example:

InfoModel oldModel== InfoModel();

You need to traverse the values of all the attributes of these two object classes for comparison and list the differences.

A solution is to use reflection for comparison.

System. Reflection. PropertyInfo [] mPi = (I =; I <mPi. Length; I ++ = pi. GetValue (oldModel, = pi. GetValue (newModel, (oldValue! =View Code

Here, a small problem is that typeOf is used to store the MODEL! There is also what the oldModel and newModel are. If this is the case, it is not difficult to write a method like this for every data model class, although the previous comparison won't use so many IF items, it still cannot be reused, I only know what maintenance, reusability, scalability, and flexibility can be achieved in software design.

Well, this obviously cannot be reused. The best thing to do is that all classes can use this method. At this time, I instantly think of generics in my mind. [Both generic and generic methods have reusability, type security, and efficiency. This is not possible for non-generic and non-generic methods. Generics are usually used together with collections and Methods acting on collections. The. NET Framework 2.0 Class Library provides a new namespace System. Collections. Generic, which contains several new Generic-based collection classes. Generic is the most powerful function of C #2.0. You can define a type-safe data structure through the generic type without using the actual data type. This significantly improves performance and produces higher quality code because you can reuse data processing algorithms without replicating specific types of code. In terms of concept, generics are similar to C ++ templates, but there are significant differences in implementation and functions .] This is an introduction to generics from MSDN. Well, I think this function should meet my requirements and change the code.

CompareModel <T> StringBuilder strLog = System. reflection. propertyInfo [] mPi = (I =; I <mPi. length; I ++ System. reflection. propertyInfo pi = strName = log = (pi. getValue (oldModel ,)! = & Pi. GetValue (newModel ,)! = Name = pi. Name; oldValue = pi. GetValue (oldModel,). ToString (); newValue = pi. GetValue (newModel, (oldValue! = {StrLog. AppendFormat (}View Code

In this way, different data can be listed one by one, but there is another problem here: the NAME is in English, and it is impossible for the customer to see English. Let's try another way. Here, we can see that PropertyInfo has a GetCustomAttributes aspect. This is not the method for obtaining features. I know the features, I just want to [Display (Name = "username")] in the field of the Model class in MVC. Well, I also have this, althoughAttributesIt can be customized, but I am still a little lazy to use MVC. It is too troublesome to write it myself, and I am not very familiar with it myself. I can only take it as an example. Happy to citeUsing System. ComponentModel. DataAnnotations;

[Display (Name = UserName {; ExternalLoginData {; CompareModel <T> (T newModel, T oldModel) T:, StringBuilder strLog = System. reflection. propertyInfo [] mPi = (I =; I <mPi. length; I ++ System. reflection. propertyInfo pi = strName = log = (pi. getValue (oldModel ,)! = & Pi. GetValue (newModel ,)! = StrName = Attributes. GetDisplayInfo <T> oldValue = pi. GetValue (oldModel, newValue = pi. GetValue (newModel, (oldValue! = (GetNameByDictonary (strName, oldValue, newValue, strLog. appendFormat (GetDisplayInfo <T> (name) T:, Type objType = strName = PropertyInfo propInfo = [] attrs = propInfo. getCustomAttributes (DisplayAttribute),); (attrs. length> DisplayAttribute a = attrs [] strName =}View Code

 

This Code also basically meets the requirements, but the younger brother is puzzled by the fact that some fields of the data entity type are foreign keys of other tables, how can we display the data normally? I hope you will give us some guidance. I hope you will discuss some good solutions and give your valuable suggestions. Thank you!

Related Article

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.