internal static void HandleObjectChange <T>(IUnifiedIMObject obj) { string key = obj.ObjectID; Type t = typeof(T); if (UnifiedSystem.AllowReferences || UnifiedSystem.AllowIndexes) { //Loop over Changes foreach (KeyValuePair <string, UIMPropertyState> changeState in obj.PropertyStates) { object cVal = Property.Get(obj, changeState.Key) ?? null; changeState.Value.HasChangedAndUpdate(cVal, (oldVal, newVal) => { //Index Updating if (AllowIndexes) { FixIndex(obj, t, changeState.Key, changeState.Value.LastState); } //Reference Updating if (AllowReferences) { if (HostReferences.ContainsKey(t)) { UnifiedIMReference hRefs = HostReferences[t].FirstOrDefault(x => x.HostProperty == changeState.Key); if (hRefs != null) { DeleteReference(obj, hRefs); ResolveReference(obj, hRefs, t); } } if (TargetReferences.ContainsKey(t)) { List <UnifiedIMReference> tRefs = TargetReferences[t].Where(x => x.TargetProperty == changeState.Key).ToList(); foreach (UnifiedIMReference reff in tRefs) { DeleteReference(obj, reff); ResolveReference(obj, reff); } } } }); } } }
internal static void RegisterType(Type type) { if (AllowReferences) { PropertyInfo[] props = type.GetPropertiesCached() .Union(type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)) .ToArray(); foreach (PropertyInfo prop in props) { UnifiedIMReference reff = prop.GetCustomAttribute <UnifiedIMReference>(); UnifiedIMIndex indx = prop.GetCustomAttribute <UnifiedIMIndex>(); bool alreadyIndexed = false; if (reff != null) { if (reff.HostPropertyType == null) { reff.HostPropertyType = props.FirstOrDefault(x => x.Name == reff.HostProperty)?.PropertyType; } if (reff.HostType == null) { reff.HostType = prop.DeclaringType; } reff.SetProperty = prop.Name; reff.SetPropertyType = prop.PropertyType; if (reff.SetPropertyType == null) { throw new ArgumentException("Set Property does not exist"); } References.Add(reff); if (!HostReferences.ContainsKey(type)) { HostReferences.Add(type, new List <UnifiedIMReference>()); } HostReferences[type].Add(reff); if (!TargetReferences.ContainsKey(reff.TargetType)) { TargetReferences.Add(reff.TargetType, new List <UnifiedIMReference>()); } TargetReferences[reff.TargetType].Add(reff); if (!TypeReferences.ContainsKey(reff.HostType)) { TypeReferences.Add(reff.HostType, new List <UnifiedIMReference>()); } if (!TypeReferences.ContainsKey(reff.TargetType)) { TypeReferences.Add(reff.TargetType, new List <UnifiedIMReference>()); } TypeReferences[reff.TargetType].Add(reff); TypeReferences[reff.HostType].Add(reff); if (AllowIndexes && CreateReferenceIndexes) { if (reff.TargetProperty != "ObjectID" && !HasIndex(reff.TargetType, reff.TargetProperty)) { AddIndex(reff.TargetType, reff.TargetProperty); } if (reff.HostProperty != "ObjectID" && !HasIndex(reff.HostType, reff.HostProperty)) { AddIndex(reff.HostType, reff.HostProperty); } if (reff.TargetType == type || reff.HostType == type) { alreadyIndexed = true; } } } if (!alreadyIndexed && prop.Name != "ObjectID" && indx != null) { AddIndex(type, prop.Name); } } } if (!_databaseGetters.ContainsKey(type)) { _databaseGetters.Add(type, ((IUnifiedIMObject)Activator.CreateInstance(type)).DatabaseBase); } }