public static void SetLoaded(this IIdentifiedEntity me, string propertyName) { var loadCheck = new PropertyLoadCheck(propertyName); if (!me.GetAnnotations <PropertyLoadCheck>().Contains(loadCheck)) { me.AddAnnotation(loadCheck); } }
/// <summary> /// Delay load property /// </summary> public static object LoadProperty(this IIdentifiedEntity me, string propertyName, bool forceReload = false) { if (me == null) { return(null); } var propertyToLoad = me.GetType().GetRuntimeProperty(propertyName); if (propertyToLoad == null) { return(null); } var currentValue = propertyToLoad.GetValue(me); var loadCheck = new PropertyLoadCheck(propertyName); if (!forceReload && (me.GetAnnotations <PropertyLoadCheck>().Contains(loadCheck) || me.GetAnnotations <String>().Contains(SanteDBConstants.NoDynamicLoadAnnotation))) { return(currentValue); } else if (forceReload) { currentValue = null; } try { if (typeof(IList).IsAssignableFrom(propertyToLoad.PropertyType)) // Collection we load by key { if ((currentValue == null || (currentValue as IList)?.Count == 0) && me.Key.HasValue) { var mi = typeof(IEntitySourceProvider).GetGenericMethod(nameof(IEntitySourceProvider.GetRelations), new Type[] { propertyToLoad.PropertyType.StripGeneric() }, new Type[] { typeof(Guid?[]) }); object loaded = null; if (me is ITaggable taggable && taggable.TryGetTag(SanteDBConstants.AlternateKeysTag, out ITag altKeys)) { loaded = Activator.CreateInstance(propertyToLoad.PropertyType, mi.Invoke(EntitySource.Current.Provider, new Object[] { altKeys.Value.Split(',').Select(o => (Guid?)Guid.Parse(o)).Union(new Guid?[] { me.Key }).ToArray() })); } else { loaded = Activator.CreateInstance(propertyToLoad.PropertyType, mi.Invoke(EntitySource.Current.Provider, new object[] { new Guid?[] { me.Key.Value } })); } propertyToLoad.SetValue(me, loaded); return(loaded); } return(currentValue); } else if (currentValue == null) { var keyValue = propertyToLoad.GetSerializationRedirectProperty()?.GetValue(me) as Guid?; if (keyValue.GetValueOrDefault() == default(Guid)) { return(currentValue); } else { var mi = typeof(IEntitySourceProvider).GetGenericMethod(nameof(IEntitySourceProvider.Get), new Type[] { propertyToLoad.PropertyType }, new Type[] { typeof(Guid?) }); var loaded = mi.Invoke(EntitySource.Current.Provider, new object[] { keyValue }); propertyToLoad.SetValue(me, loaded); return(loaded); } } else { return(currentValue); } }
/// <summary> /// Returns true if the property has been loaded /// </summary> public static bool WasLoaded(this IIdentifiedEntity me, String propertyName) { var loadCheck = new PropertyLoadCheck(propertyName); return(me.GetAnnotations <PropertyLoadCheck>().Contains(loadCheck)); }