using dxm.DAL;
using dxm.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ORMDemo
{
/// <summary>
/// 1.从ado.net到通用数据库访问层
/// 2.泛型+反射+ado.net完成通用主键查询&全表查询
/// 3.特性attribute完成映射
/// 4.泛型缓存完成性能提升
/// 5.委托完成代码重用
///
///
/// 泛型+反射+ado.net+特性
/// </summary>
class Program
{
static void Main(string[] args)
{
SqlHelper helper = new SqlHelper();
//UserInfo user = helper.Find<UserInfo>(1);
UserInfoModel user = helper.Find<UserInfoModel>(1);
ClassInfo classInfo = helper.Find<ClassInfo>(1);
}
}
}
/**********************dxm.Model库******************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dxm.Model
{
public class ClassInfo
{
public int Id { get; set; }
public string ClassName { get; set; }
}
}
using dxm.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dxm.Model
{
/// <summary>
/// 数据库名是UserInfo,实体名是UserInfoModel
/// </summary>
[Table("UserInfo")]
public class UserInfoModel
{
public int Id { get; set; }
/// <summary>
/// 数据库字段是UserName,类属性是Name
/// </summary>
[Column("UserName")]
public string Name { get; set; }
public string UserPwd { get; set; }
}
}
/****************************dxm.Framework*********************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dxm.Framework
{
/// <summary>
/// 数据库映射的特性
/// </summary>
public class BaseAttribute:Attribute
{
private string _Name = "";
public BaseAttribute(string name)
{
this._Name = name;
}
public virtual string GetName()
{
return this._Name;
}
}
}
/************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dxm.Framework
{
/// <summary>
/// 数据库映射的特性
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute:BaseAttribute
{
public ColumnAttribute(string name):base(name)
{
}
}
}
/**********/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dxm.Framework
{
/// <summary>
/// 数据库映射的特性
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
//public class TableAttribute:Attribute
public class TableAttribute:BaseAttribute
{
public TableAttribute(string name):base(name)
{
}
}
}
/********************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace dxm.Framework
{
public static class DBAttributeExtend
{
public static string GetMappingName<T>(this T t) where T:MemberInfo
{
if (t.IsDefined(typeof(BaseAttribute), true))
{
BaseAttribute attribute = (BaseAttribute)t.GetCustomAttribute(typeof(BaseAttribute), true);
return attribute.GetName();
}
else
{
return t.Name;
}
}
//public static string GetMappingName(this Type type)
//{
// if (type.IsDefined(typeof(TableAttribute),true))
// {
// TableAttribute attribute = (TableAttribute)type.GetCustomAttribute(typeof(TableAttribute), true);
// return attribute.GetName();
// }
// else
// {
// return type.Name;
// }
//}
//public static string GetMappingName(this PropertyInfo prop)
//{
// if (prop.IsDefined(typeof(ColumnAttribute), true))
// {
// ColumnAttribute attribute = (ColumnAttribute)prop.GetCustomAttribute(typeof(ColumnAttribute), true);
// return attribute.GetName();
// }
// else
// {
// return prop.Name;
// }
//}
}
}
/********************************************dxm.DAL************************************************/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using dxm.Framework;
namespace dxm.DAL
{
public class SqlHelper
{
string strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
public T Find<T>(int id)
{
try
{
using (SqlConnection conn = new SqlConnection(strConn))
{
Type type = typeof(T);
var props = type.GetProperties();
//string columnString = string.Join(",", props.Select(m => $"[{m.Name}]"));
string columnString = string.Join(",", props.Select(m => $"[{m.GetMappingName()}]"));
//string sql = $"SELECT {columnString} FROM [{type.Name}] WHERE Id={id}";
string sql = $"SELECT {columnString} FROM [{type.GetMappingName()}] WHERE Id={id}";//表名映射
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
var reader = cmd.ExecuteReader();
if (reader.Read())
{
T t = (T)Activator.CreateInstance(type);
foreach (var prop in props)
{
//prop.SetValue(t, reader[prop.Name] is DBNull ? null : reader[prop.Name]);
prop.SetValue(t, reader[prop.GetMappingName()] is DBNull ? null : reader[prop.GetMappingName()]);
}
return t;
}
else
{
return default(T);
}
}
}
catch (Exception)
{
return default(T);
}
}
}
}