private void DropReferences(object instance) { IPersistentObject managed = (IPersistentObject)instance; this.uow.Objects.Remove(managed.Id); IList references = managed.GetReferences(); ArrayList tmp = new ArrayList(references); foreach (ObjectReference reference in tmp) { if (!IsLoaded(reference.ObjectId)) { continue; } IPersistentObject referencedObject = (IPersistentObject)this.Get(reference.ObjectId); referencedObject.SetUnloaded(reference.Property, true); Type referencedObjectType = referencedObject.GetType(); PropertyInfo pi = referencedObjectType.GetProperty(reference.Property); bool stackMute = referencedObject.Mute; referencedObject.Mute = true; pi.SetValue(referencedObject, null, null); referencedObject.Mute = stackMute; } }
public object HandleCall(Puzzle.NAspect.Framework.MethodInvocation call) { if (call.Method.DeclaringType == typeof(IPersistentObject)) { return(call.Proceed()); } InterceptedParameter valueParameter = (InterceptedParameter)call.Parameters[0]; string property = call.Method.Name.Substring(4); IPersistentObject managed = (IPersistentObject)call.Target; object res = call.Proceed(); managed.SetReference(property, valueParameter.Value); if (!managed.Mute && !managed.Initializing) { managed.Context.RegisterDirty(managed); } managed.SetUnloaded(property, true); return(res); }
public IPersistentObject Get(string id) { if (uow.Objects.ContainsKey(id)) { WeakReference reff = (WeakReference)uow.Objects[id]; IPersistentObject instance = (IPersistentObject)reff.Target; if (reff.IsAlive) { return(instance); //return object if possible } else { uow.Objects.Remove(id); //clear dead ref } } //try to load object string filePath = dbPath + @"\" + id + ".txt"; if (System.IO.File.Exists(filePath)) { //try to load and deserialize object System.IO.StreamReader sr = new System.IO.StreamReader(filePath); string serializedData = sr.ReadToEnd(); sr.Close(); SerializedObject so = JSONSerializer.DeSerialize(serializedData); IPersistentObject managed = (IPersistentObject)engine.CreateProxy(so.Type); managed.Context = this; managed.Id = id; RegisterObject(managed); managed.Initializing = true; foreach (var entry in so.Data) { string property = (string)entry.Key; PropertyInfo pi = so.Type.GetProperty(property); if (entry.Value is PersistentId) { managed.SetReference(property, entry.Value); managed.SetUnloaded(property, true); } else { pi.SetValue(managed, entry.Value, null); managed.SetUnloaded(property, false); //setup list owner if (entry.Value is IPersistentList) { IPersistentList list = entry.Value as IPersistentList; list.Owner = managed; } } } managed.Initializing = false; return(managed); } else { return(null); //file didnt exist } }