In actual development, we often need to read data from the database and assign values to the corresponding attributes of the object class. There are a large number of suchCodeFor example:
Public Role [] getroles ( Int Blogid)
{
System. Collections. arraylist al = New System. Collections. arraylist ();
Idatareader Reader = Dbprovider. instance (). getroles (blogid );
Try
{
While (Reader. Read ())
{
Role role = New Role ();
If (Reader [ " Roleid " ] ! = Dbnull. value)
{
Role. roleid=(Int) Reader ["Roleid"];
}
If (Reader [ " Name " ] ! = Dbnull. value)
{
Role. Name=(String) Reader ["Name"];
}
If (Reader [ " Description " ] ! = Dbnull. value)
{
Role. Description=(String) Reader ["Description"];
}
// Readertoobject (reader, role );
Al. Add (role );
}
}
Finally
{
Reader. Close ();
}
Return (Role []) Al. toarray ( Typeof (Role ));
}
I think the above Code is not elegant:
1. Each time you assign a value to the role attribute, check whether the reader value is dbnull.
2. Each time a role attribute is assigned a value, type conversion is required. The type of the role attribute is known. Can this conversion be automatically completed?
3. Each time a role attribute is assigned a value, the role attribute corresponds to the database field. If we ensure that the same name is used for the database field and the object class attribute when designing the database and object class, we can use reflection to automatically map the attributes and fields through code. Even if the database field and attribute are different, we can change the query statement to achieve this.
Can I improve the code above to make the code more elegant? What kind of elegant code should be like? If we use the code line readertoobject (reader, role) Annotated in the above Code to replace its previous statement of assigning values to the role attribute, will it make the code more elegant? Readertoobject is used to automatically write the values in reader to the corresponding attributes in role (provided that the fields in reader have the same name as the attributes in role ). Now our task is to implement readertoobject. With the powerful weapon-reflection, our task becomes very easy and I will not talk about it anymore. The following code is my implementation method:
Private Void Readertoobject (idatareader reader, Object Targetobj)
{
For ( Int I = 0 ; I < Reader. fieldcount; I ++ )
{
System. reflection. propertyinfo = Targetobj. GetType (). getproperty (reader. getname (I ));
If (Propertyinfo ! = Null )
{
If (Reader. getvalue (I) ! = Dbnull. value)
{
Propertyinfo. setvalue (targetobj, reader. getvalue (I ),Null);
}
}
}
}
Readertoobject can read data from reader into any entity class. The ing principle between database fields and object attributes is the same name. Of course, we can also map the two through the configuration file.
personal thoughts: in development, it is often confusing to face so many design ideas and design patterns, when you focus more on which design idea or design pattern to choose, I don't think it is very important to ignore it and try to reduce repeated code as much as possible, as long as we can effectively reduce repeated code, the method we adopt is a good method, rather than worrying about which mode we adopt. It's like a lone sword. It's just because you get rid of the limitations of the traditional tricks that can be invincible!