Introduction of the problem (hypothesis ):
In a project, we have a user information table that contains only the user phone number and user address.
Now, the boss finds that only the two methods are outdated. He asked us to get more contact information, such as QQ, email, and MSN.
Well, if the demand changes, the boss says a word, we have to re-open ourProgram, You have to open these.
Some people may think about it very quickly. It's simple. There are a few more fields, one for one, and it took a few hours to finish it. I thought the boss was coming again when everything went well, he said, "Oh, I forgot to say that. I didn't make up the phone. You can try again." Dizzy.
The problem arises. Is there a better way to do this (add a contact method and a field.
Recently I encountered the same problem. I provided a solution to this problem (of course, my requirements are more complex than this example ).
The solution is as follows:
1. I only add one field, and other fields remain unchanged (DecreaseCode);
2. I add a new entity class, and each of the attributes to be added corresponds to one-to-one contact method.
For example:
Public class userinfoadd {
Public String QQ {Get; set ;}
Public String email {Get; set ;}
Public String MSN {Get; Set}
Public String phone {Get; set ;}
}
3. My idea is to assign a value to the attribute of this object class and store it in the database. I will retrieve it from the database when using it. In this way, my goal is achieved.
The question is how to implement this idea? You cannot directly store the instance in the database (MS says he does not support it now)
Then we can find another method. Serialization can meet our needs (we save the results after the sequence to the database, and retrieve the results from the database when it is used for deserialization, we get our instance, with this entity, it is convenient to do anything you want (well, it feels good )).
Okay, so we can start serialization. There is another small problem: what format should we serialize into? XML, binary, or JSON.
In summary, I learned how to use JSON, which is smaller than XML (so that our database can save space) and more readable than binary (binary is not readable at all ). It has a format like this {QQ: 260717774, email: leleroyn@gmail.com, MSN: leleroyn@gmail.com}, a look at it to know what information is stored, readability is very good!
The problem is solved as follows:
1. Applicable scenarios: more than one field is added.(These fields cannot be reversed. In this example, you cannot use QQ to query user information ). Everyone seems to be discussing this issue. I'm sorry for not making it clear! // Modify this part
2. Use an entity class to carry our information
3. serialize the entity carrying the information and store it to the newly added field of the data.
4. retrieve this field from the database and deserialize it into an instance (this object carries information)
Article Write it here. Welcome.