public object Poco(object entity, Func <object, object> subPocoHandler) { var metadata = RepositoryFramework.GetDefineMetadata(entity.GetType()); var pa = PropertyAccessorFactory.GetPropertyAccess(metadata.EntityType); var poco = entity; var pocoed = false; if (ProxyProvider.IsProxy(entity) && metadata.ClassJoinDefines.Count > 0) { poco = pa.CreateInstance(); metadata.MergeData(entity, poco); pocoed = true; } foreach (var property in metadata.CascadeProperties) { var value = pa.GetGetter(property.Name).Get(entity); if (value != null) { var valuePoco = subPocoHandler(value); if (valuePoco != value || pocoed) { pa.GetSetter(property.Name).Set(poco, valuePoco); } } } return(poco); }
internal static void Proxy(object instance) { if (instance == null) { throw new ArgumentNullException("instance"); } var type = instance.GetType(); if (EntityUtil.IsPagingResult(type)) { var ta = TypeAccessor.GetAccessor(type); IEnumerable items = (IEnumerable)ta.GetProperty("Items", instance); foreach (var item in items) { Proxy(item); } } else if (EntityUtil.IsGenericList(type)) { IEnumerable items = (IEnumerable)instance; foreach (var item in items) { Proxy(item); } } else if (RepositoryFramework.GetDefineMetadata(type) != null) { ProxyJoins(instance); } }
public object Proxy(object entity, Func <object, object> subProxyHandler) { var metadata = RepositoryFramework.GetDefineMetadata(entity.GetType()); var proxy = entity; var proxyed = false; if (!ProxyProvider.IsProxy(entity) && metadata.ClassJoinDefines.Count > 0) { proxy = ProxyProvider.CreateEntityProxy(metadata.EntityType); metadata.MergeData(entity, proxy); proxyed = true; } var pa = PropertyAccessorFactory.GetPropertyAccess(metadata.EntityType); foreach (var property in metadata.CascadeProperties) { if (EntityInterceptor.PropertyInited(entity, property.Name)) { var value = pa.GetGetter(property.Name).Get(entity); if (value != null) { var valueProxy = subProxyHandler(value); if (valueProxy != value || proxyed) { pa.GetSetter(property.Name).Set(proxy, valueProxy); } } } } return(proxy); }
static void Fetch(object entity, Func <object, bool> cancelFetchFunc) { if (entity == null) { return; } var type = entity.GetType(); var metadata = RepositoryFramework.GetDefineMetadata(type); if (metadata != null) { if (metadata.FetchableObject != null) { (metadata.FetchableObject).Fetch(entity); } var joins = metadata.ClassJoinDefines.Values.Cast <ClassJoinDefineMetadata>().Where(o => o.JoinType == MethodJoinType.PropertyGet); var ta = TypeAccessor.GetAccessor(metadata.EntityType); foreach (var join in joins) { var value = ta.GetProperty(join.JoinName, entity); if (cancelFetchFunc != null && cancelFetchFunc(value)) { return; } var entityType = join.Method.ReturnType; if (EntityUtil.IsGenericList(entityType)) { var items = (IList)value; for (var i = 0; i < items.Count; i++) { var v = items[i]; var sv = GetSource(v); Fetch(sv, cancelFetchFunc); if (sv != v) { items[i] = sv; } } } else { var sv = GetSource(value); Fetch(sv, cancelFetchFunc); if (value != sv) { ta.SetProperty(join.JoinName, entity, sv); } } } } }
ClassDefineMetadata GetClassDefineMetadata(object entity) { if (entity != null) { return(RepositoryFramework.GetDefineMetadata(entity.GetType())); } return(null); }
static IEnumerable <IClassJoinCascadeProcessor> GetCascadeProcessors(Type type) { var metadata = RepositoryFramework.GetDefineMetadata(type); if (metadata == null) { throw new ArgumentNullException("metadata"); } return(metadata.ClassJoinDefines.Where(o => o.Value.JoinType == MethodJoinType.PropertyGet && o.Value.JoinCascade.CascataType != CascadeType.None) .Select(o => o.Value.JoinCascade.Processor)); }
public void SetRegions(List <CacheRegion> regions) { this.regions = regions; //验证缓存区域设置 foreach (var region in regions) { var metadata = RepositoryFramework.GetDefineMetadata(region.EntityType); if (metadata == null) { throw new NotSupportedException(String.Format("类型 {0} 未进行定义,因此无法实现仓储扩展。", region.EntityType.FullName)); } metadata.CheckCacheRegions(new[] { region }); } }
public void Intercept(IInvocation invocation) { var methodName = invocation.Method.Name; bool skip = methodName == "OpenSession" || methodName == "GetShardParams" || methodName == "CreateSpecification"; if (!skip && (methodName == "GetList" || methodName == "Get")) { var arguments = invocation.Method.GetParameters(); skip = arguments.Length != 2 || arguments[0].ParameterType != typeof(ShardParams); } if (!skip) { ProfilerContext.BeginWatch(invocation.TargetType.FullName + "." + invocation.Method.Name); } try { var entityType = ReflectionHelper.GetEntityTypeFromRepositoryType(invocation.TargetType); var metadata = RepositoryFramework.GetDefineMetadata(entityType); if (metadata != null) { var mi = invocations.FirstOrDefault(o => o.IsMatch(invocation)); if (mi != null) { mi.Process(invocation, metadata); ProxyEntity(invocation); return; } } if (skip) { invocation.Proceed(); } else { using (MonitorContext.Repository(invocation.Method)) invocation.Proceed(); } ProxyEntity(invocation); } finally { if (!skip) { ProfilerContext.EndWatch(); } } }
public void Fetch(object entity, Action <object> subFetchHandler) { var metadata = RepositoryFramework.GetDefineMetadata(entity.GetType()); var pa = PropertyAccessorFactory.GetPropertyAccess(metadata.EntityType); foreach (var property in metadata.CascadeProperties) { var value = pa.GetGetter(property.Name).Get(entity); if (value != null) { subFetchHandler(value); } } }
bool IsIdentityExpression(Expression <Func <TJoin, bool> > expression, out object value) { value = null; var metadata = RepositoryFramework.GetDefineMetadata(typeof(TJoin)); if (metadata == null || metadata.IdMember == null) { return(false); } var valider = new IdentityExpressionValider(metadata.IdMember, expression); value = valider.Value; return(valider.IsValid); }
public void Intercept(IInvocation invocation) { var entityType = ReflectionHelper.GetEntityTypeFromRepositoryType(invocation.TargetType); var metadata = RepositoryFramework.GetDefineMetadata(entityType); if (metadata == null) { throw new NotSupportedException(String.Format("类型 {0} 未进行定义,因此无法实现仓储扩展。", entityType.FullName)); } foreach (var type in providers) { AbstractCacheableRepositoryProvider <T> provider = (AbstractCacheableRepositoryProvider <T>)Projects.Tool.Reflection.FastActivator.Create(type); provider.Invocation = invocation; if (provider.IsMatch()) { provider.CacheKey = cacheKey; var regionStr = String.Join(",", regions.Select(o => o.CacheKey)); var cacheData = provider.GetCacheData(); if (cacheData != null && IsCacheValid(metadata, cacheData)) { log.DebugFormat("{0}.{1} query cache hit. #{2}", invocation.Method.DeclaringType.ToPrettyString(), invocation.Method.Name, provider.CacheKey); if (ProfilerContext.Current.Enabled) { ProfilerContext.Current.Trace("platform", String.Format("hit {1}\r\n{0}", ProfilerUtil.JsonFormat(provider.CacheKey), regionStr)); } //处理缓存 using (MonitorContext.Repository(invocation.Method, true)) provider.ProcessCache(cacheData); return; } //处理原始 var hasSource = provider.ProcessSource(); log.DebugFormat("{0}.{1} query cache missing. #{2}", invocation.Method.DeclaringType.ToPrettyString(), invocation.Method.Name, provider.CacheKey); if (ProfilerContext.Current.Enabled) { ProfilerContext.Current.Trace("platform", String.Format("missing {1}{2}\r\n{0}", ProfilerUtil.JsonFormat(provider.CacheKey), regionStr, hasSource ? "" : " for null")); } return; } } invocation.Proceed(); }
public static TJoin GetJoin <TJoin>(object entity, Expression <Func <TJoin> > memberExpr) { if (entity == null) { throw new ArgumentNullException("entity"); } var metadata = RepositoryFramework.GetDefineMetadata(entity.GetType()); var method = ReflectionHelper.GetMethod(memberExpr.Body); var joinDefine = metadata.GetClassJoinDefineMetadata(method); if (joinDefine == null) { throw new PlatformException("类型 {1} 方法 {0} 未进行关联的定义。", method.Name, method.DeclaringType.FullName); } return((TJoin)joinDefine.DataProcesser.Process(entity)); }
public static object GetId(object entity) { if (entity == null) { throw new ArgumentNullException("entity"); } var entityType = entity.GetType(); var metadata = RepositoryFramework.GetDefineMetadata(entityType); if (metadata == null) { throw new ArgumentNullException("metadata"); } var pa = GetPropertyAccess(metadata.EntityType); return(pa.GetGetter(metadata.IdMember.Name).Get(entity)); }
public static void Fetch(object entity) { if (entity == null) { return; } var type = entity.GetType(); var metadata = RepositoryFramework.GetDefineMetadata(type); if (metadata != null) { if (metadata.FetchableObject != null) { metadata.FetchableObject.Fetch(entity); Fetch(entity, metadata.FetchableObject.CheckFetchCancel); } else { Fetch(entity, null); } } }
public bool IsMatch(Type type) { return(RepositoryFramework.GetDefineMetadata(type) != null); }