public virtual void EnsurePropertyIsLoaded(object obj, IPropertyMap propertyMap) { IObjectManager om = this.Context.ObjectManager; IPersistenceEngine pe = this.Context.PersistenceEngine; ObjectStatus objStatus; PropertyStatus propStatus; objStatus = om.GetObjectStatus(obj); if (objStatus != ObjectStatus.Deleted) { if (objStatus == ObjectStatus.NotLoaded) { pe.LoadObject(ref obj); if (pe == null) { throw new ObjectNotFoundException("Object not found!"); // do not localize } } propStatus = om.GetPropertyStatus(obj, propertyMap.Name); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyMap.Name); } } else { //We really ought to throw an exception here... } }
public IObjectClone CloneObject(object obj) { IObjectManager om = this.Context.ObjectManager; IObjectClone clone = new ObjectClone(); IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.IsCollection) { } else { if (om.HasOriginalValues(obj, propertyMap.Name)) { clone.SetPropertyValue(propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); clone.SetOriginalPropertyValue(propertyMap.Name, om.GetOriginalPropertyValue(obj, propertyMap.Name)); clone.SetNullValueStatus(propertyMap.Name, om.GetNullValueStatus(obj, propertyMap.Name)); clone.SetUpdatedStatus(propertyMap.Name, om.GetUpdatedStatus(obj, propertyMap.Name)); } } } clone.SetObjectStatus(om.GetObjectStatus(obj)); this.clonedObjects.Add(obj); return(clone); }
public void EnsureLoaded() { if (MuteNotify) { return; } IContext ctx = interceptable.GetInterceptor().Context; IObjectManager om = ctx.ObjectManager; PropertyStatus propStatus = om.GetPropertyStatus(interceptable, propertyName); if (propStatus == PropertyStatus.NotLoaded) { ObjectStatus objStatus = om.GetObjectStatus(interceptable); if (!(objStatus == ObjectStatus.UpForCreation)) { bool stackMute = MuteNotify; MuteNotify = true; ctx.PersistenceEngine.LoadProperty(interceptable, propertyName); MuteNotify = stackMute; } } }
public IObjectClone CloneObject(object obj) { IObjectManager om = this.Context.ObjectManager; IObjectClone clone = new ObjectClone(); IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.IsCollection) { //TODO: Implement this } else { clone.SetPropertyValue(propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); clone.SetNullValueStatus(propertyMap.Name, om.GetNullValueStatus(obj, propertyMap.Name)); clone.SetUpdatedStatus(propertyMap.Name, om.GetUpdatedStatus(obj, propertyMap.Name)); if (om.HasOriginalValues(obj, propertyMap.Name)) { clone.SetOriginalPropertyValue(propertyMap.Name, om.GetOriginalPropertyValue(obj, propertyMap.Name)); } } } clone.SetObjectStatus(om.GetObjectStatus(obj)); IIdentityHelper identityHelper = obj as IIdentityHelper; if (identityHelper != null) { clone.SetIdentity(identityHelper.GetIdentity()); if (identityHelper.HasIdentityKeyParts()) { foreach (object keyPart in identityHelper.GetIdentityKeyParts()) { clone.GetIdentityKeyParts().Add(keyPart); } } if (identityHelper.HasKeyStruct()) { clone.SetKeyStruct(identityHelper.GetKeyStruct()); } } this.clonedObjects.Add(obj); return(clone); }
public void ValidateObject(object obj, IList exceptions) { IObjectManager om = this.Context.ObjectManager; IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); ValidationMode objValidationMode = GetValidationMode(classMap); if (objValidationMode != ValidationMode.Off) { CustomValidateObject(obj, classMap, exceptions); ObjectStatus objStatus = om.GetObjectStatus(obj); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { ValidationMode validationMode = GetValidationMode(classMap, propertyMap); if (validationMode != ValidationMode.Off) { PropertyStatus propStatus = om.GetPropertyStatus(obj, propertyMap.Name); if (validationMode == ValidationMode.ValidateAll && objStatus != ObjectStatus.UpForCreation) { this.Context.ObjectManager.EnsurePropertyIsLoaded(obj, propertyMap); } if (objStatus == ObjectStatus.UpForCreation || propStatus != PropertyStatus.NotLoaded) { bool ok = false; if (validationMode == ValidationMode.Default || validationMode == ValidationMode.ValidateLoaded || validationMode == ValidationMode.ValidateAll) { ok = true; } else if (validationMode == ValidationMode.ValidateDirty) { if (propStatus == PropertyStatus.Dirty) { ok = true; } } if (ok) { DoValidateProperty(obj, propertyMap, exceptions); } } } } } }
public virtual void InvalidateProperty(object obj, IPropertyMap propertyMap) { IObjectManager om = this.Context.ObjectManager; IPersistenceEngine pe = this.Context.PersistenceEngine; ObjectStatus objStatus; PropertyStatus propStatus; objStatus = om.GetObjectStatus(obj); propStatus = om.GetPropertyStatus(obj, propertyMap.Name); if (propStatus == PropertyStatus.NotLoaded) { RemoveOriginalValues(obj, propertyMap.Name); //pe.LoadProperty(obj, propertyMap.Name); } }
public void ValidateObject(object obj, IList exceptions) { IObjectManager om = this.Context.ObjectManager; IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); CustomValidateObject(obj, classMap, exceptions); ObjectStatus objStatus = om.GetObjectStatus(obj); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { PropertyStatus propStatus = om.GetPropertyStatus(obj, propertyMap.Name); if (objStatus == ObjectStatus.UpForCreation || propStatus != PropertyStatus.NotLoaded) { DoValidateProperty(obj, propertyMap, exceptions); } } }
public virtual PropertyStatus GetPropertyStatus(object obj, string propertyName) { ObjectStatus objStatus = m_ObjectManager.GetObjectStatus(obj); if (objStatus == ObjectStatus.UpForCreation) { return(PropertyStatus.Dirty); } if (objStatus == ObjectStatus.Deleted) { return(PropertyStatus.Deleted); } if (m_ObjectManager.IsDirtyProperty(obj, propertyName)) { return(PropertyStatus.Dirty); } if (m_ObjectManager.HasOriginalValues(obj, propertyName)) { return(PropertyStatus.Clean); } return(PropertyStatus.NotLoaded); }
private void NullifyInverseReference(IPropertyMap propertyMap, object obj, IPropertyMap invPropertyMap, IObjectManager om) { bool stackMute = false; IInterceptableList mList; IList refList; IList list; object thisObj; object refObj; //Ensure that the property is loaded if (this.Context.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.NotLoaded) { this.Context.LoadProperty(obj, propertyMap.Name); } if (propertyMap.IsCollection) { list = (IList) om.GetPropertyValue(obj, propertyMap.Name); if (list == null) { list = this.Context.ListManager.CreateList(obj, propertyMap); } if (list != null) { if (invPropertyMap.IsCollection) { foreach (object itemRefObj in list) { ObjectStatus refObjStatus = om.GetObjectStatus(itemRefObj); if (!(refObjStatus.Equals(ObjectStatus.UpForDeletion) && refObjStatus.Equals(ObjectStatus.Deleted))) { //Ensure inverse is loaded PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(itemRefObj, invPropertyMap.Name); if (invPropertyMap.IsSlave && invPropertyStatus == PropertyStatus.NotLoaded) { AddAction(InverseActionType.Remove, itemRefObj, invPropertyMap.Name, obj, obj); } else { if (invPropertyStatus == PropertyStatus.NotLoaded) this.Context.LoadProperty(itemRefObj, invPropertyMap.Name); refList = ((IList) (om.GetPropertyValue(itemRefObj, invPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(itemRefObj, invPropertyMap.Name, true); } } } } } else { foreach (object itemRefObj in list) { ObjectStatus refObjStatus = om.GetObjectStatus(itemRefObj); if (!(refObjStatus.Equals(ObjectStatus.UpForDeletion) && refObjStatus.Equals(ObjectStatus.Deleted))) { PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(itemRefObj, invPropertyMap.Name); if (invPropertyMap.IsSlave && invPropertyStatus == PropertyStatus.NotLoaded) { AddAction(InverseActionType.Set, itemRefObj, invPropertyMap.Name, null, obj); } else { //Ensure inverse is loaded if (invPropertyStatus == PropertyStatus.NotLoaded) this.Context.LoadProperty(itemRefObj, invPropertyMap.Name); thisObj = om.GetPropertyValue(itemRefObj, invPropertyMap.Name); if (thisObj != null) { if (thisObj == obj) { om.SetPropertyValue(itemRefObj, invPropertyMap.Name, null); om.SetUpdatedStatus(itemRefObj, invPropertyMap.Name, true); } } } } } } } } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); if (refObj != null) { ObjectStatus refObjStatus = om.GetObjectStatus(refObj); if (!(refObjStatus.Equals(ObjectStatus.UpForDeletion) && refObjStatus.Equals(ObjectStatus.Deleted))) { PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(refObj, invPropertyMap.Name); //Ensure inverse is loaded if (invPropertyMap.IsSlave && invPropertyStatus == PropertyStatus.NotLoaded) { if (invPropertyMap.IsCollection) { AddAction(InverseActionType.Remove, refObj, invPropertyMap.Name, obj, obj); } else { AddAction(InverseActionType.Set, refObj, invPropertyMap.Name, null, obj); } } else { if (invPropertyStatus == PropertyStatus.NotLoaded) this.Context.LoadProperty(refObj, invPropertyMap.Name); if (invPropertyMap.IsCollection) { refList = ((IList) (om.GetPropertyValue(refObj, invPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) {mList.MuteNotify = stackMute;} om.SetUpdatedStatus(refObj, invPropertyMap.Name, true); } } else { thisObj = om.GetPropertyValue(refObj, invPropertyMap.Name); if (thisObj != null) { //only update back ref if it is actually pointing at me if (thisObj == obj) { om.SetPropertyValue(refObj, invPropertyMap.Name, null); om.SetUpdatedStatus(refObj, invPropertyMap.Name, true); } } } } } } } }
//[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); } }
public override void LoadProperty(object obj, string propertyName) { if (this.url.Length < 1) { throw new NPersistException("You must specify an url to your NPersist Web Service in your WebServiceRemotingEngine!"); } RemotingService rs = new RemotingService(this.Context, url); IMarshalingTransformer transformer = new MarshalingTransformer(Context); IObjectManager om = Context.ObjectManager; IClassMap classMap = Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); MarshalReference mr = transformer.FromObjectAsReference(obj); string xmlObject = (string)Formatter.Serialize(mr); if (useCompression && this.compressor != null) { xmlObject = this.compressor.Compress(xmlObject); } bool doUseCompression = this.useCompression; if (this.compressor == null) { doUseCompression = false; } string result = rs.LoadProperty(xmlObject, propertyName, this.domainKey, doUseCompression); if (useCompression && this.compressor != null) { result = this.compressor.Decompress(result); } if (propertyMap.IsCollection) { if (propertyMap.ReferenceType == ReferenceType.None) { } else { //Refresh issues!!! (the objects in the list being deserialized may exist in the cache!!) MarshalObjectList mol = (MarshalObjectList)Formatter.Deserialize(result, typeof(MarshalObjectList)); IList freshList = transformer.ToObjectList(mol, RefreshBehaviorType.DefaultBehavior, new ArrayList()); //Context.IdentityMap.RegisterLoadedObject(obj); IList orgList = (IList)om.GetPropertyValue(obj, propertyName); LoadReferenceList(freshList, orgList, om, obj, propertyMap); this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, orgList); } } else { if (propertyMap.ReferenceType == ReferenceType.None) { MarshalProperty mp = (MarshalProperty)Formatter.Deserialize(result, typeof(MarshalProperty)); transformer.ToProperty(obj, mp, propertyMap, RefreshBehaviorType.DefaultBehavior); } else { if (result.Length > 0) { MarshalObject mo = (MarshalObject)Formatter.Deserialize(result, typeof(MarshalObject)); string identity = transformer.GetIdentity(mo); IClassMap refClassMap = Context.DomainMap.MustGetClassMap(mo.Type); Type refType = Context.AssemblyManager.MustGetTypeFromClassMap(refClassMap); object refObject = Context.GetObjectById(identity, refType, true); if (om.GetObjectStatus(refObject) == ObjectStatus.NotLoaded) { transformer.ToObject(mo, ref refObject); } // object refObject = Context.TryGetObjectById(identity, refType, true); // if (refObject == null) // { // refObject = Context.GetObjectById(identity, refType, true); // transformer.ToObject(mo, ref refObject); // } om.SetPropertyValue(obj, propertyName, refObject); om.SetOriginalPropertyValue(obj, propertyName, refObject); om.SetNullValueStatus(obj, propertyName, false); this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, refObject); } else { om.SetPropertyValue(obj, propertyName, null); om.SetOriginalPropertyValue(obj, propertyName, null); om.SetNullValueStatus(obj, propertyName, true); } } } }