标签:style blog class code c java
[AttributeUsage(AttributeTargets.Class)] public class TableAttribute : Attribute { public TableAttribute(string tableName) { Name = tableName; } public string Name { get; private set; } } [AttributeUsage(AttributeTargets.Property)] public class KeyAttribute : Attribute { public KeyAttribute() : this(false) { } public KeyAttribute(bool isIdentity) { IsIdentity = isIdentity; } public bool IsIdentity { get; private set; } } [AttributeUsage(AttributeTargets.Property)] public class IgnoreAttribute : Attribute { } [AttributeUsage(AttributeTargets.Property)] public class ColumnAttribute : Attribute { public ColumnAttribute(string columnName) { Name = columnName; QueryName = columnName; } public ColumnAttribute(string columnName, string queryName) : this(columnName) { QueryName = queryName; } public string Name { get; private set; } public string QueryName { get; private set; } }
要使用TypeInfo的类必须在Entity上标志以上属性,特别是Key属性作为数据库表的主键必须存在,否则以全部字段进行匹配,可能会产生不一致的问题
下面是实体增删改功能的实现
public static class EntityMapper { private static readonly ConcurrentDictionary<RuntimeTypeHandle, TypeInfo> TypeTableInfo = new ConcurrentDictionary<RuntimeTypeHandle, TypeInfo>(); public static T Get<T>(SSGClass.ExpressOpr exp) where T : new() { var info = TryGetInfo(typeof(T)); var reader = SSGClass.DBConnect.ServerDb.ExecuteReader(info.SelectText + exp.ToParameters(), exp.Parameters); if (reader.Read()) { return info.GenEntity<T>(reader); } throw new ExceptionTecnical(string.Format(Global.Consts.OutOfRing, typeof(T).Name)); } static TypeInfo TryGetInfo(Type type) { TypeInfo info; RuntimeTypeHandle handle = type.TypeHandle; if (!TypeTableInfo.TryGetValue(handle, out info)) { info = new TypeInfo(type); TypeTableInfo[handle] = info; } return info; } public static List<T> GetList<T>(SSGClass.ExpressOpr exp = null) where T:new() { List<T> lst = new List<T>(); var info = TryGetInfo(typeof(T)); System.Data.Common.DbDataReader reader; if (exp == null) reader = SSGClass.DBConnect.ServerDb.ExecuteReader(info.SelectText, null); else reader = SSGClass.DBConnect.ServerDb.ExecuteReader(info.SelectText + exp.ToParameters(), exp.Parameters); while (reader.Read()) { lst.Add(info.GenEntity<T>(reader)); } return lst; } public static int Delete<T>(SSGClass.ExpressOpr exp = null) { string deleteText = TryGetInfo(typeof(T)).DeleteText; if (exp == null) return SSGClass.DBConnect.ServerDb.ExcuteCommand(deleteText, null); else return SSGClass.DBConnect.ServerDb.ExcuteCommand(deleteText + exp.ToParameters(), exp.Parameters); } public static int DeleteAll<T>() { return Delete<T>(); } public static int Delete(object entity) { var info = TryGetInfo(entity.GetType()); return Delete(info.GetDeleteExpress(entity)); } public static int Update(object entity) { var info = TryGetInfo(entity.GetType()); return SSGClass.DBConnect.ServerDb.ExcuteCommand(info.UpdateText, info.GetEntityPatams(entity)); } public static int Insert(object entity) { var info = TryGetInfo(entity.GetType()); return SSGClass.DBConnect.ServerDb.ExcuteCommand(info.InsertText, info.GetEntityPatams(entity)); } }
Attribute定义及EntityMapper,布布扣,bubuko.com
标签:style blog class code c java
原文地址:http://www.cnblogs.com/Kiver/p/3732101.html