object GetValueFor([NotNull] LocationInterceptionArgs args, LazyEntity parent, Func <LazyEntity, Type, ICollection <IDataCriterion>, IEnumerable <object> > read) { var criterion = CriterionFor(args); if (criterion == null) { return(null); } // var collection = GetLazy.Entity(RealLocationType, (ILazyEntity)args.Instance).By(criterion); var collection = read(parent, RealLocationType, new[] { criterion }); if (IsCollection) { var targetCollection = (IList)Activator.CreateInstance(TargetCollectionType); foreach (var item in collection) { targetCollection.Add(item); } return(targetCollection); } return(collection.FirstOrDefault()); }
void LazyLoadValueFromDatabase(LocationInterceptionArgs args, LazyEntity lazyEntity) { lock (_writeLock) { args.ProceedGetValue(); if (AlreadyLoaded) { return; } if (IsValueAlreadySpecified(args)) { AlreadyLoaded = true; return; } if (lazyEntity.Read == null) { AlreadyLoaded = true; //?if Read callback is null - it means that entity was created by the client code, and not was getted from db, so it's not persisted at all. return; } var value = GetValueFor(args, lazyEntity, lazyEntity.Read); args.SetNewValue(value); AlreadyLoaded = true; args.ProceedGetValue(); } }
void CopyLazyContext(LazyEntity parent, object entity) { var lazyEntity = entity as LazyEntity; if (lazyEntity != null) { lazyEntity.Context = parent.Context; SafetySetReadCallbackTo(lazyEntity); } }
IEnumerable <object> LazyLoad(LazyEntity parent, Type type, ICollection <IDataCriterion> criteria) { var info = _lazyFactory.InfoAbout(type); var repository = _lazyFactory.ReadingRepository(); foreach (var item in repository.Read(info, criteria)) { CopyLazyContext(parent, item); yield return(item); } }
void SafetySetReadCallbackTo(LazyEntity entity) { if (entity.Read == null) { lock (entity) { if (entity.Read == null) { entity.Read = LazyLoad; } } } }
void SetupLazyChildren(LazyEntity entity, IEntityInfo info, LazyEntity.LoadingContext context) { if (entity == null) { return; } entity.Context = context; SafetySetReadCallbackTo(entity); if (info.Inclusions == null) { return; } foreach (var inclusion in info.Inclusions) { object child; if (!entity.TryGet(inclusion.TargetPath, out child)) { Log.TraceEvent(TraceEventType.Warning, $"Can't get property {inclusion.TargetPath} on {_mainEntityInfo.EntityType} to setup it's loading context."); continue; } if (child == null) { continue; } if (!inclusion.IsCollection) { SetupLazyChildren(child as LazyEntity, inclusion.Info, context); continue; } foreach (var item in (IEnumerable)child) { SetupLazyChildren(item as LazyEntity, inclusion.Info, context); } } }