public bool CompareLists(IList newList, IList oldList) { bool stackNewMute = false; bool stackOldMute = false; IInterceptableList iNewList = newList as IInterceptableList; if (iNewList != null) { stackNewMute = iNewList.MuteNotify; iNewList.MuteNotify = true; } IInterceptableList iOldList = newList as IInterceptableList; if (iOldList != null) { stackOldMute = iOldList.MuteNotify; iOldList.MuteNotify = true; } bool result = DoCompareLists(newList, oldList); if (iNewList != null) { iNewList.MuteNotify = stackNewMute; } if (iOldList != null) { iOldList.MuteNotify = stackOldMute; } return(result); }
protected void OverwriteOriginalList() { bool stackMute = false; IList orgList = this.cachedOriginalValue as IList; IList freshList = this.freshValue as IList; IInterceptableList mList = orgList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } orgList.Clear(); foreach (object value in freshList) { orgList.Add(value); } if (mList != null) { mList.MuteNotify = stackMute; } }
public void CheckPartiallyLoadedList(string propertyName, ITransaction transaction) { IList partialList = GetPartiallyLoadedList(propertyName, transaction); int count = GetCount(propertyName, transaction); if (count == partialList.Count) { IContext context = ((IInterceptable)this.target).GetInterceptor().Context; IObjectManager om = context.ObjectManager; IInterceptableList iList = om.GetPropertyValue(target, propertyName) as IInterceptableList; if (iList == null) { throw new NPersistException(string.Format("Object of type {0} and identity {1} does not have an interceptable list injected in property {2}", target.GetType(), om.GetObjectIdentity(target), propertyName)); } bool stackMute = iList.MuteNotify; iList.MuteNotify = true; if (iList.Count < 1) { IList orgList = new ArrayList(); foreach (object refObj in partialList) { iList.Add(refObj); orgList.Add(refObj); } iList.MuteNotify = stackMute; om.SetPropertyValue(target, propertyName, iList); om.SetOriginalPropertyValue(target, propertyName, orgList); om.SetNullValueStatus(target, propertyName, false); om.SetUpdatedStatus(target, propertyName, false); context.InverseManager.NotifyPropertyGet(target, propertyName); } else { iList.MuteNotify = stackMute; } } }
public static IInterceptableList CreateProxy(Type baseType, IObjectFactory objectFactory, params object[] ctorArgs) { if (objectFactory == null) { throw new Exception("apa"); } Type proxyType = GetProxyType(baseType); IInterceptableList result = (IInterceptableList)objectFactory.CreateInstance(proxyType, ctorArgs); if (result == null) { result = (IInterceptableList)Activator.CreateInstance(proxyType, ctorArgs); } //attach the an interceptor to the list //result.SetInterceptor(new ListInterceptor()); return(result); }
//[DebuggerStepThrough()] protected virtual void DoNotifyPropertySet(object obj, string propertyName, ref object value, object oldValue, bool hasOldValue, ref bool cancel) { IContext ctx = this.Context; IObjectManager om = ctx.ObjectManager; IPersistenceEngine pe = ctx.PersistenceEngine; PropertyCancelEventArgs e; if (hasOldValue) { e = new PropertyCancelEventArgs(obj, propertyName, value, oldValue, this.Context.ObjectManager.GetNullValueStatus(obj, propertyName)); } else { e = new PropertyCancelEventArgs(obj, propertyName, value, this.Context.ObjectManager.GetPropertyValue(obj, propertyName), this.Context.ObjectManager.GetNullValueStatus(obj, propertyName)); } this.Context.EventManager.OnWritingProperty(this, e); if (e.Cancel) { cancel = true; return; } value = e.NewValue; IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap; string prevId; string newId; propertyMap = classMap.MustGetPropertyMap(propertyName); if (propertyMap.ReferenceType != ReferenceType.None && value != null) { if (propertyMap.ReferenceType == ReferenceType.OneToMany || propertyMap.ReferenceType == ReferenceType.OneToOne) { //parent object IInterceptable ivalue = value as IInterceptable; if (ivalue == null) { throw new NPersistException(string.Format("Object is not a NPersist managed object, do not use 'new' on Entities. (Property='{0}', Owner={1})", propertyName, obj)); } else { if (ivalue.GetInterceptor().Context != this.Context) { throw new NPersistException(string.Format("Object does not belong to the same context object as the property owner. (Property='{0}', Owner={1})", propertyName, obj)); } ObjectStatus valueObjectStatus = om.GetObjectStatus(value); if (valueObjectStatus == ObjectStatus.UpForDeletion || valueObjectStatus == ObjectStatus.Deleted) { throw new DeletedObjectException(string.Format("Object has been deleted. (Object={0})", value), value); } } } else if (propertyMap.ReferenceType == ReferenceType.ManyToOne || propertyMap.ReferenceType == ReferenceType.ManyToMany) { IInterceptableList ivalue = value as IInterceptableList; if (ivalue == null) { throw new NPersistException(string.Format("List is not a NPersist managed list, do not use 'new' to initialize lists, NPersist does this for you. (Property='{0}', Owner={1})", propertyName, obj)); } else if (ivalue.Interceptable.GetInterceptor().Context != this.Context) { throw new NPersistException(string.Format("List does not belong to the same context object as the property owner. (Property='{0}', Owner={1})", propertyName, obj)); } } } if (propertyMap.IsReadOnly) { //Let read-only inverse properties through if (!(propertyMap.ReferenceType != ReferenceType.None && propertyMap.Inverse.Length > 0 && propertyMap.NoInverseManagement == false)) { //Special - if someone forgot to make their ManyOne read-only, //why bug them about it? (so don't add an "else" with an exception...) if (propertyMap.ReferenceType != ReferenceType.ManyToOne) { throw new ReadOnlyException("Property '" + classMap.Name + "." + propertyName + "' is read-only!"); // do not localize } } } PropertyStatus propStatus = PropertyStatus.Clean; ObjectStatus objStatus = om.GetObjectStatus(obj); bool hasPropertyStatus = false; if (objStatus == ObjectStatus.Deleted) { throw new DeletedObjectException("The object has been deleted!", obj, propertyName); // do not localize } else if (objStatus == ObjectStatus.UpForDeletion) { throw new DeletedObjectException("The object has been registered as up for deletion!", obj, propertyName); // do not localize } this.Context.ObjectCloner.EnsureIsClonedIfEditing(obj); if (objStatus == ObjectStatus.UpForCreation) { } else if (objStatus == ObjectStatus.Clean) { propStatus = om.GetPropertyStatus(obj, propertyName); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } if (!(hasOldValue)) { if (!(om.ComparePropertyValues(obj, propertyName, value, om.GetPropertyValue(obj, propertyName)))) { this.Context.UnitOfWork.RegisterDirty(obj); } } } else if (objStatus == ObjectStatus.NotLoaded) { propertyMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()).MustGetPropertyMap(propertyName); if (!(propertyMap.IsIdentity)) { propStatus = this.Context.ObjectManager.GetPropertyStatus(obj, propertyName); hasPropertyStatus = true; //it would be sweet to be able to determine beforehand if this property would be part of the span //that is loaded with LoadObject and only call LoadObject if that is the case.... if (propStatus == PropertyStatus.NotLoaded) { hasPropertyStatus = false; //this.Context.PersistenceEngine.LoadObject(ref obj); this.Context.IdentityMap.LoadObject(ref obj, true); if (obj == null) { throw new ObjectNotFoundException("Object not found!"); // do not localize } } if (!hasPropertyStatus) { propStatus = om.GetPropertyStatus(obj, propertyName); } if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } } if (!(hasOldValue)) { if (!(om.ComparePropertyValues(obj, propertyName, value, om.GetPropertyValue(obj, propertyName)))) { this.Context.UnitOfWork.RegisterDirty(obj); } } } else if (objStatus == ObjectStatus.Dirty) { propStatus = om.GetPropertyStatus(obj, propertyName); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } } if (propertyMap.IsIdentity) { prevId = om.GetObjectIdentity(obj); newId = om.GetObjectIdentity(obj, propertyMap, value); if (prevId != newId) { ctx.IdentityMap.UpdateIdentity(obj, prevId, newId); } } om.SetNullValueStatus(obj, propertyName, false); om.SetUpdatedStatus(obj, propertyName, true); if (hasOldValue) { ctx.InverseManager.NotifyPropertySet(obj, propertyName, value, oldValue); ctx.UnitOfWork.RegisterDirty(obj); } else { ctx.InverseManager.NotifyPropertySet(obj, propertyName, value); } }
protected virtual void DeserializeClone(object obj, ReadOnlyClone clone) { IObjectManager om = this.Context.ObjectManager; IDomainMap dm = this.Context.DomainMap; IAssemblyManager am = this.Context.AssemblyManager; IClassMap classMap = dm.MustGetClassMap(obj.GetType()); IListManager lm = this.Context.ListManager; foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.ReferenceType == ReferenceType.None) { if (propertyMap.IsCollection) { IList values = (IList)clone.PropertyValues[propertyMap.Name]; IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); bool stackMute = false; IInterceptableList mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Clear(); foreach (object value in values) { list.Add(value); } IList cloneList = new ArrayList(list); if (mList != null) { mList.MuteNotify = stackMute; } om.SetOriginalPropertyValue(obj, propertyMap.Name, cloneList); om.SetNullValueStatus(obj, propertyMap.Name, false); } else { object value = clone.PropertyValues[propertyMap.Name]; om.SetPropertyValue(obj, propertyMap.Name, value); om.SetOriginalPropertyValue(obj, propertyMap.Name, value); om.SetNullValueStatus(obj, propertyMap.Name, (bool)clone.NullValueStatuses[propertyMap.Name]); } } else { IClassMap refClassMap = propertyMap.MustGetReferencedClassMap(); if (refClassMap.IsReadOnly) { if (propertyMap.IsCollection) { IList values = (IList)clone.PropertyValues[propertyMap.Name]; IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); bool stackMute = false; IInterceptableList mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Clear(); foreach (SerializedReference refId in values) { object value = null; if (refId != null) { refClassMap = dm.MustGetClassMap(refId.Type); Type refType = am.GetTypeFromClassMap(refClassMap); value = this.Context.GetObjectById(refId.Identity, refType, true); list.Add(value); } } IList cloneList = new ArrayList(list); if (mList != null) { mList.MuteNotify = stackMute; } om.SetOriginalPropertyValue(obj, propertyMap.Name, cloneList); om.SetNullValueStatus(obj, propertyMap.Name, false); } else { object value = null; SerializedReference refId = (SerializedReference)clone.PropertyValues[propertyMap.Name]; if (refId != null) { refClassMap = dm.MustGetClassMap(refId.Type); Type refType = am.GetTypeFromClassMap(refClassMap); value = this.Context.GetObjectById(refId.Identity, refType, true); } om.SetPropertyValue(obj, propertyMap.Name, value); om.SetOriginalPropertyValue(obj, propertyMap.Name, value); om.SetNullValueStatus(obj, propertyMap.Name, (bool)clone.NullValueStatuses[propertyMap.Name]); } } } } }
private void LoadReferenceList(IList list, IList orgList, IObjectManager om, object obj, IPropertyMap propertyMap) { IList objectsToRemove = new ArrayList(); IList objectsToAdd = new ArrayList(); foreach (object itemOrgObj in orgList) { string itemOrgObjId = om.GetObjectIdentity(itemOrgObj); bool found = false; foreach (object itemObj in list) { string itemObjId = om.GetObjectIdentity(itemObj); if (itemObjId == itemOrgObjId) { found = true; break; } } if (!found) { objectsToRemove.Add(itemOrgObj); } } foreach (object itemObj in list) { string itemObjId = om.GetObjectIdentity(itemObj); bool found = false; foreach (object itemOrgObj in orgList) { string itemOrgObjId = om.GetObjectIdentity(itemOrgObj); if (itemObjId == itemOrgObjId) { found = true; break; } } if (!found) { object itemOrgObj = this.Context.GetObjectById(itemObjId, itemObj.GetType(), true); objectsToAdd.Add(itemOrgObj); } } if (objectsToRemove.Count > 0 || objectsToAdd.Count > 0) { bool stackMute = false; IInterceptableList mList = orgList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } foreach (object itemOrgObj in objectsToRemove) { orgList.Remove(itemOrgObj); } foreach (object itemOrgObj in objectsToAdd) { orgList.Add(itemOrgObj); } if (mList != null) { mList.MuteNotify = stackMute; } } bool iStackMute = false; IInterceptableList iList = orgList as IInterceptableList; if (iList != null) { iStackMute = iList.MuteNotify; iList.MuteNotify = true; } IList listClone = new ArrayList(orgList); if (iList != null) { iList.MuteNotify = iStackMute; } om.SetOriginalPropertyValue(obj, propertyMap.Name, listClone); }