DataReader mapper

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Hi,

Ive found datareader on net and modify it a little bit to meet my requirements but Im currently stock on something. You see I have a BusinessObjects which treats a class as a Property.

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; class User
{
<span style="color:Blue; private <span style="color:Blue; string _username;
<span style="color:Blue; private <span style="color:Blue; string _password;
<span style="color:Blue; private Role _role;

<span style="color:Blue; public <span style="color:Blue; string Username
{
<span style="color:Blue; get { <span style="color:Blue; return _username; }
<span style="color:Blue; set { _username = value; }
}

<span style="color:Blue; public <span style="color:Blue; string Password
{
<span style="color:Blue; get { <span style="color:Blue; return _password; }
<span style="color:Blue; set { _password = value; }
}

<span style="color:Blue; public Role Role
{
<span style="color:Blue; get
{
<span style="color:Blue; if(_role == <span style="color:Blue; null)
{
_role = <span style="color:Blue; new Role();
}
<span style="color:Blue; return _role;
}
}
}

[/code]

and assume the members of Role is just roleId and roleName. One main problem on the mapper is that it doesnt read the Role Property since Role field doesnt exist on the Schema that I am reading instead the members of Role is being return by the schema.
I cant seem to modify the mapper to read other classes related to the parent class. I cant use inheritance since there are times that multiple relationship are being used. I cant also use nHibernate because I havent talked to our DBA yet.

Heres the mapper that I found :


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; static Hashtable LoadProperties<T>()
{
Type businessEntityType = <span style="color:Blue; typeof(T);
Hashtable hashTable = <span style="color:Blue; new Hashtable();
PropertyInfo[] properties = businessEntityType.GetProperties();

<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < properties.Length; i++)
{
PropertyInfo info = properties;
hashTable[info.Name.ToUpper()] = info;
}
<span style="color:Blue; return hashTable;
}

<span style="color:Blue; public <span style="color:Blue; static T MapDataToBusinessEntityCollection<T>(IDataReader reader) <span style="color:Blue; where T : <span style="color:Blue; new()
{
Hashtable hashTable = LoadProperties<T>();
T newObject = <span style="color:Blue; new T();

<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < reader.FieldCount; i++)
{
PropertyInfo info = (PropertyInfo)hashTable[reader.GetName(i).ToUpper()];
<span style="color:Blue; if ((info != <span style="color:Blue; null) && (reader[info.Name] != DBNull.Value) && info.CanWrite)
{
info.SetValue(newObject, reader.GetValue(i), <span style="color:Blue; null);
}
}
<span style="color:Blue; return newObject;
}
[/code]

the mapper uses reflection to access properties. Ive also tried to encapsulate the Role class on a Generic List and check on the mapper if one of the property implements an interface then getting its properties but the properties always being returned to
me is the properties that IList has not the Class that was encapsulated.

Please Advice.

Thank You,
noObzoR



View the full article
 
Back
Top