Example #1
0
		//[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);
			}
		}
Example #2
0
		public virtual void NotifyPropertyGet(object obj, string propertyName, ref object value, ref bool cancel)
		{
			if (this.isDisposed) { return; }
			if (notification == Notification.Disabled) { return; }
			ObjectStatus objStatus = this.Context.ObjectManager.GetObjectStatus(obj);
			IPropertyMap propertyMap;
			PropertyStatus propStatus = PropertyStatus.Clean;
			bool hasPropertyStatus = false;
			PropertyCancelEventArgs e = new PropertyCancelEventArgs(obj, propertyName, null, value, this.Context.ObjectManager.GetNullValueStatus(obj, propertyName));
			this.Context.EventManager.OnReadingProperty(this, e);
			if (e.Cancel)
			{
				cancel = true;
				return;
			}
			value = e.value;
			
			bool didLoadObject = 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
			}
			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
						}
						didLoadObject = true;						
					}
				}
			}
			if (!hasPropertyStatus)
				propStatus = this.Context.ObjectManager.GetPropertyStatus(obj, propertyName);
	
			if (propStatus == PropertyStatus.Clean)
			{
				if (didLoadObject)
				{
					value = this.Context.ObjectManager.GetPropertyValue(obj, propertyName);
				}
			}
			if (propStatus == PropertyStatus.Deleted)
			{
				if (obj is IObjectHelper)
				{
					throw new DeletedObjectException("The object has been deleted!", obj, propertyName); // do not localize
				}
			}
			else if (propStatus == PropertyStatus.NotLoaded)
			{
				if (!(objStatus == ObjectStatus.UpForCreation))
				{
					this.Context.PersistenceEngine.LoadProperty(obj, propertyName);
					value = this.Context.ObjectManager.GetPropertyValue(obj, propertyName);
				}
			}
			this.Context.InverseManager.NotifyPropertyGet(obj, propertyName);
		}
		public virtual void LoadProperty(object obj, string propertyName)
		{
			this.SqlEngineManager.Context.LogManager.Info(this, "Loading property", "Property: " + propertyName + ", Object Type: " + obj.GetType().ToString()); // do not localize
			IList parameters;
			object value;
			object orgValue;
			IContext ctx = m_SqlEngineManager.Context;
			PropertyCancelEventArgs e = new PropertyCancelEventArgs(obj, propertyName);
			ctx.EventManager.OnLoadingProperty(this, e);
			if (e.Cancel)
			{
				return;
			}
			IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType());
			IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName);
			if (propertyMap.IsCollection)
			{
				LoadCollectionProperty(obj, propertyMap);
				return;
			}
			if (!(propertyMap.ReferenceType == ReferenceType.None))
			{
				LoadReferenceProperty(obj, propertyMap);
				return;
			}
			if (!(propertyMap.MustGetTableMap() == classMap.MustGetTableMap()))
			{
				LoadNonPrimaryProperty(obj, propertyMap);
				return;
			}
			parameters = new ArrayList() ;
			IObjectManager om = ctx.ObjectManager;
			IPersistenceManager pm = ctx.PersistenceManager;
			string sql = GetSelectPropertyStatement(obj, propertyName, parameters);
			IDataSource ds = ctx.DataSourceManager.GetDataSource(obj);
			object[,] result = (object[,]) ctx.SqlExecutor.ExecuteArray(sql, ds, parameters);
			if (Util.IsArray(result))
			{
				orgValue = result[0, 0];
				value = pm.ManageLoadedValue(obj, propertyMap, orgValue);
				om.SetPropertyValue(obj, propertyName, value);
				om.SetOriginalPropertyValue(obj, propertyName, orgValue);
				om.SetNullValueStatus(obj, propertyName, DBNull.Value.Equals(orgValue));
			}
			else
			{
				throw new ObjectNotFoundException("Object not found!"); // do not localize
			}
			PropertyEventArgs e2 = new PropertyEventArgs(obj, propertyName);
			ctx.EventManager.OnLoadedProperty(this, e2);
		}
Example #4
0
 public virtual void OnWritingProperty(object sender, PropertyCancelEventArgs e)
 {
 }
 public void Context_WritingProperty_Cnt(object sender, PropertyCancelEventArgs e)
 {
     writingCnt++;
 }
 public void Context_WritingProperty_Cancel(object sender, PropertyCancelEventArgs e)
 {
     e.Cancel = true;
 }
 public void Context_ReadingProperty_Cnt(object sender, PropertyCancelEventArgs e)
 {
     readingCnt++;
 }
 public void Context_ReadingProperty_Cancel(object sender, PropertyCancelEventArgs e)
 {
     e.Value = "Permission denied";
     e.Cancel = true;
 }
Example #9
0
		//[DebuggerStepThrough()]
		public virtual void OnWritingProperty(object sender, PropertyCancelEventArgs e)
		{
			if (this.ValidationManager.ValidateOnWritingProperty)
			{
				if (e.EventObject is IValidatable)
				{
					((IValidatable) (e.EventObject)).Validate();
				}
			}
			if (!(m_RaiseEvents))
			{
				return;
			}
			if (!(m_RaiseBeforeEvents))
			{
				return;
			}
			if (!(m_RaisePropertyEvents))
			{
				return;
			}
			foreach (IObserver observer in m_Observers)
			{
				observer.OnWritingProperty(sender, e);
			}
			foreach (IObserver observer in m_AllTypeObservers)
			{
				observer.OnWritingProperty(sender, e);
			}
			foreach (IObserver observer in GetTypeObservers(e.EventObject))
			{
				observer.OnWritingProperty(sender, e);
			}
			foreach (IObserver observer in GetObjectObservers(e.EventObject))
			{
				observer.OnWritingProperty(sender, e);
			}
			if (e.EventObject is IObservable)
			{
				foreach (IEventListener eventListener in ((IObservable) (e.EventObject)).GetEventListeners())
				{
					eventListener.OnWritingProperty(sender, e);
				}
			}
			this.Observer.OnWritingProperty(sender, e);
			if (e.EventObject is IEventListener)
			{
				((IEventListener) (e.EventObject)).OnWritingProperty(sender, e);
			}
		}