/// <summary> /// 查询数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="obj"></param> /// <returns></returns> public virtual T GetInfos <T>(QueryInfo query, OrmObjectInfo obj = null) { obj = obj ?? Orm.GetOrm(query.FromExp); var infos = ExecuteInfos <T>(obj, query); return(infos); }
/// <summary> /// 附加到实体中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public virtual T Attach <T>(T entity) { object key = null; object value = null; var obj = Orm.GetOrm(entity.GetType().FullName); var property = entity.GetType().GetProperties().FirstOrDefault(it => it.Name.Equals(obj.PrimaryProperty.PropertyName)); if (property != null) { value = property.GetValue(entity, null); if (property.PropertyType.IsValueType && !value.Equals(0) || !property.PropertyType.IsValueType && value != null) { key = GetEntityCacheKey(obj, value); } } if (key == null) { return(entity); } if (!Local.HasEntity(key)) { Local.Entities.Add(key, entity); } if (obj.IsCache) { InsertCommonCache(obj, value, entity); } return((T)Local.GetEntity(key)); }
/// <summary> /// 得到Key /// </summary> public object GetKey(string name) { OrmObjectInfo orm = Orm.GetOrm(name); if (orm == null || orm.Key == null) { return(null); } return(GetKeyValue(orm)); }
/// <summary> /// 得到实体集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="obj"></param> /// <returns></returns> public IList <T> Gets <T>(QueryInfo query, OrmObjectInfo obj = null) { obj = obj ?? Orm.GetOrm(query.FromExp); var infos = ExecuteInfos <IList <T> >(obj, query, obj.IsCache); if (infos != null) { for (var i = 0; i < infos.Count; i++) { infos[i] = Attach(infos[i]); } } return(infos); }
/// <summary> /// 得到实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="type"></param> /// <param name="obj"></param> /// <returns></returns> public T Get <T>(object key, Type type, OrmObjectInfo obj = null) { if (key == null) { return(default(T)); } if (key.GetType().FullName.Equals(type.FullName)) { if (Local.HasStorage(key)) { return((T)Local.Storages[key].Entity); } return(default(T)); } obj = obj ?? Orm.GetOrm(type.FullName); string cacheKey = GetEntityCacheKey(obj, key); if (!Local.HasEntity(cacheKey)) { object entity = null; if (obj.IsCache) { entity = GetCache <T>(cacheKey); } if (entity == null) { var query = new QueryInfo { IsLazyLoad = true }.From(type.FullName); query.Where(string.Format("{0}==@{0}", obj.PrimaryProperty.PropertyName)) .SetParameter(obj.PrimaryProperty.PropertyName, key); var entities = ExecuteInfos <IList <T> >(obj, query, obj.IsCache); entity = entities.FirstOrDefault(); if (obj.IsCache && entity != null) { InsertCommonCache(obj, key, entity); } } Local.Entities.Add(cacheKey, entity); } return((T)Local.GetEntity(cacheKey)); }
/// <summary> /// 实在实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="sequence"></param> /// <param name="isBulkCopy"></param> /// <param name="obj"></param> /// <param name="inforamtion"></param> public virtual void Set <T>(T entity, EntityInfo inforamtion, int sequence = 0, bool isBulkCopy = false, OrmObjectInfo obj = null) { obj = obj ?? Orm.GetOrm(entity.GetType().FullName); if (inforamtion.SaveType == SaveType.Modify && string.IsNullOrEmpty(inforamtion.WhereExp) && obj.VersionProperty != null) { var dics = new Dictionary <string, string>(); if (inforamtion.Properties != null) { foreach (var property in inforamtion.Properties) { dics.Add(property, property); } } var hasLocker = obj.Properties.Any( it => it.IsOptimisticLocker && (inforamtion.Properties == null || dics.ContainsKey(it.PropertyName))); if (hasLocker) { var key = entity.GetProperty(obj.PrimaryProperty.PropertyName); var value = Get <T>(key, obj); if (value != null) { var version = value.GetProperty(obj.VersionProperty.PropertyName); entity.SetProperty(obj.VersionProperty.PropertyName, version); } } } if (!Local.HasStorage(entity)) { Local.Storages.Add(entity, new SaveInfo { Entity = entity, Sequence = sequence, IsBulkCopy = isBulkCopy, Information = inforamtion, Object = obj }); } }