才毕业不久,找了份搞C#的工作,因为刚毕业的缘故,还有就是这是我在博客园的第一篇随笔,希望大家拍砖。
今天就把最近学的三层架构里面的东西和反射联系到了一起,虽然反射效率不是很高,但是对于小型项目,用在数据访问层还是可以的,下面就来代码了:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 根据指定单据获取此单据对应的账户利息结算记录(结息单的对应利息结算记录)
/// </summary>
/// <param name="i_PV"> 结息单据记录对象 </param>
/// <returns> 利息结算记录 </returns>
public static IList < T > GetAccountBalance < T > (Investment_PrincipalVouch i_PV)
{
SqlParameter[] paras = new SqlParameter[ 2 ];
paras[ 0 ] = new SqlParameter( " @VouchDate " , DAHelper.ReadParam(i_PV.VouchDate));
paras[ 1 ] = new SqlParameter( " @AccountID " , DAHelper.ReadParam(i_PV.OutAccountID));
string strQuery = " SELECT * FROM dbo.KD_T_AccountBalance WHERE AccountID=@AccountID AND BalanceDate=@VouchDate " ;
SqlDataReader reader = DAHelper.ExecuteReader(strQuery, CommandType.Text, paras);
IList < T > list = new List < T > ();
Type type = typeof (T);
PropertyInfo[] properties = type.GetProperties();
while (reader.Read()) // 读取传入的数据
{
T t = Activator.CreateInstance < T > (); // 构造泛型实例
foreach (PropertyInfo propertity in properties) // 遍历属性集合
{
try
{
object obj = reader[propertity.Name]; // 从reader中获取列名等于属性名的值
if (obj.Equals(DBNull.Value)) // 为空则设置属性为null
{
propertity.SetValue(t, null , null );
}
else // 不为空
{
Type propertyType = propertity.PropertyType; // 获取属性的类型
if (propertyType.IsGenericType) // 如果此属性是泛型(这里判断原因是防止有类似int?的类型)
{
propertyType = propertyType.GetGenericArguments()[ 0 ];
}
propertity.SetValue(t, Convert.ChangeType(obj, propertyType), null ); // 设置属性值
}
}
catch (Exception ex) // 抛出异常的原因是Model里的字段在数据库不存在,所以此异常不用捕获,直接跳过
{
}
}
list.Add(t); // 将泛型对象加入到列表中去
}
return list;
}
/// 根据指定单据获取此单据对应的账户利息结算记录(结息单的对应利息结算记录)
/// </summary>
/// <param name="i_PV"> 结息单据记录对象 </param>
/// <returns> 利息结算记录 </returns>
public static IList < T > GetAccountBalance < T > (Investment_PrincipalVouch i_PV)
{
SqlParameter[] paras = new SqlParameter[ 2 ];
paras[ 0 ] = new SqlParameter( " @VouchDate " , DAHelper.ReadParam(i_PV.VouchDate));
paras[ 1 ] = new SqlParameter( " @AccountID " , DAHelper.ReadParam(i_PV.OutAccountID));
string strQuery = " SELECT * FROM dbo.KD_T_AccountBalance WHERE AccountID=@AccountID AND BalanceDate=@VouchDate " ;
SqlDataReader reader = DAHelper.ExecuteReader(strQuery, CommandType.Text, paras);
IList < T > list = new List < T > ();
Type type = typeof (T);
PropertyInfo[] properties = type.GetProperties();
while (reader.Read()) // 读取传入的数据
{
T t = Activator.CreateInstance < T > (); // 构造泛型实例
foreach (PropertyInfo propertity in properties) // 遍历属性集合
{
try
{
object obj = reader[propertity.Name]; // 从reader中获取列名等于属性名的值
if (obj.Equals(DBNull.Value)) // 为空则设置属性为null
{
propertity.SetValue(t, null , null );
}
else // 不为空
{
Type propertyType = propertity.PropertyType; // 获取属性的类型
if (propertyType.IsGenericType) // 如果此属性是泛型(这里判断原因是防止有类似int?的类型)
{
propertyType = propertyType.GetGenericArguments()[ 0 ];
}
propertity.SetValue(t, Convert.ChangeType(obj, propertyType), null ); // 设置属性值
}
}
catch (Exception ex) // 抛出异常的原因是Model里的字段在数据库不存在,所以此异常不用捕获,直接跳过
{
}
}
list.Add(t); // 将泛型对象加入到列表中去
}
return list;
}
上面的代码是我在一个项目中用到的,不是太难,上面的注释很清楚,在这里就不多说了,主要有2点需要注意,一就是Model层里可能有类似int?的类型,这里需要一个处理propertyType = propertyType.GetGenericArguments()[0];二是Model层不是标准的对应数据库中的字段,所以这里的处理就是抛出异常,但是并不捕获。
好了,三层里面就这点收获,其他的基本上就是大家都熟悉的了,还有就是用到了Developer Express第三方控件,这个很是强大,希望大家可以看看。