Exemple #1
0
 /// <summary>
 ///     Sets current the <see cref="IFieldInterceptionContext" /> that will be associated with the
 ///     <see cref="FieldInterceptorRegistry" />.
 /// </summary>
 /// <param name="interceptor">The field interceptor that will be used to preempt field getter and setter calls.</param>
 public static void SetInterceptor(IFieldInterceptor interceptor)
 {
     lock (_lock)
     {
         _interceptor = interceptor;
     }
 }
Exemple #2
0
        /// <summary>
        /// Check if the property is initialized. If the named property does not exist
        /// or is not persistent, this method always returns <tt>true</tt>.
        /// </summary>
        /// <param name="proxy">The potential proxy </param>
        /// <param name="propertyName">the name of a persistent attribute of the object </param>
        /// <returns>
        /// true if the named property of the object is not listed as uninitialized;
        /// false if the object is an uninitialized proxy, or the named property is uninitialized
        /// </returns>
        public static bool IsPropertyInitialized(object proxy, string propertyName)
        {
            object entity;

            if (proxy is INHibernateProxy)
            {
                ILazyInitializer li = ((INHibernateProxy)proxy).HibernateLazyInitializer;
                if (li.IsUninitialized)
                {
                    return(false);
                }
                else
                {
                    entity = li.GetImplementation();
                }
            }
            else
            {
                entity = proxy;
            }

            if (FieldInterceptionHelper.IsInstrumented(entity))
            {
                IFieldInterceptor interceptor = FieldInterceptionHelper.ExtractFieldInterceptor(entity);
                return(interceptor == null || interceptor.IsInitializedField(propertyName));
            }
            else
            {
                return(true);
            }
        }
 /// <summary>
 /// Sets current the <see cref="IFieldInterceptionContext"/> that will be associated with the <see cref="FieldInterceptorRegistry"/>.
 /// </summary>
 /// <param name="interceptor">The field interceptor that will be used to preempt field getter and setter calls.</param>
 public static void SetInterceptor(IFieldInterceptor interceptor)
 {
     lock (_lock)
     {
         _interceptor = interceptor;
     }
 }
Exemple #4
0
 private void MarkInterceptorDirty(object entity, IEntityPersister persister, IEventSource source)
 {
     if (FieldInterceptionHelper.IsInstrumented(entity))
     {
         IFieldInterceptor interceptor = FieldInterceptionHelper.InjectFieldInterceptor(entity, persister.EntityName, persister.GetMappedClass(source.EntityMode), null, null, source);
         interceptor.MarkDirty();
     }
 }
        public static void MarkDirty(object entity)
        {
            IFieldInterceptor interceptor = ExtractFieldInterceptor(entity);

            if (interceptor != null)
            {
                interceptor.MarkDirty();
            }
        }
        public object Intercept(InvocationInfo info)
        {
            var methodName = info.TargetMethod.Name;

            if (FieldInterceptor != null)
            {
                if (ReflectHelper.IsPropertyGet(info.TargetMethod))
                {
                    if ("get_FieldInterceptor".Equals(methodName))
                    {
                        return(FieldInterceptor);
                    }

                    object propValue = info.InvokeMethodOnTarget();

                    var result = FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), propValue);

                    if (result != AbstractFieldInterceptor.InvokeImplementation)
                    {
                        return(result);
                    }
                }
                else if (ReflectHelper.IsPropertySet(info.TargetMethod))
                {
                    if ("set_FieldInterceptor".Equals(methodName))
                    {
                        FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
                        return(null);
                    }
                    FieldInterceptor.MarkDirty();
                    FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), info.Arguments[0]);
                }
            }
            else
            {
                if ("set_FieldInterceptor".Equals(methodName))
                {
                    FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
                    return(null);
                }
            }

            object returnValue;

            try
            {
                returnValue = info.InvokeMethodOnTarget();
            }
            catch (TargetInvocationException ex)
            {
                throw ReflectHelper.UnwrapTargetInvocationException(ex);
            }
            return(returnValue);
        }
 private void MarkInterceptorDirty(object entity, object target)
 {
     if (FieldInterceptionHelper.IsInstrumented(entity))
     {
         IFieldInterceptor interceptor = FieldInterceptionHelper.ExtractFieldInterceptor(target);
         if (interceptor != null)
         {
             interceptor.MarkDirty();
         }
     }
 }
        // 6.0 TODO: merge into IFieldInterceptor
        public static object Intercept(this IFieldInterceptor interceptor, object target, string fieldName, object value, bool setter)
        {
            if (interceptor is AbstractFieldInterceptor fieldInterceptor)
            {
                return(fieldInterceptor.Intercept(target, fieldName, value, setter));
            }
#pragma warning disable 618
            return(interceptor.Intercept(target, fieldName, value));

#pragma warning restore 618
        }
 public override bool HasUninitializedLazyProperties(object entity)
 {
     if (EntityMetamodel.HasLazyProperties)
     {
         IFieldInterceptor callback = FieldInterceptionHelper.ExtractFieldInterceptor(entity);
         return(callback != null && !callback.IsInitialized);
     }
     else
     {
         return(false);
     }
 }
		public object Intercept(InvocationInfo info)
		{
			var methodName = info.TargetMethod.Name;
			if (FieldInterceptor != null)
			{
				if (ReflectHelper.IsPropertyGet(info.TargetMethod))
				{
					if("get_FieldInterceptor".Equals(methodName))
					{
						return FieldInterceptor;
					}

					object propValue = info.InvokeMethodOnTarget();

					var result = FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), propValue);

					if (result != AbstractFieldInterceptor.InvokeImplementation)
					{
						return result;
					}
				}
				else if (ReflectHelper.IsPropertySet(info.TargetMethod))
				{
					if ("set_FieldInterceptor".Equals(methodName))
					{
						FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
						return null;
					}
					FieldInterceptor.MarkDirty();
					FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), info.Arguments[0]);
				}
			}
			else
			{
				if ("set_FieldInterceptor".Equals(methodName))
				{
					FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
					return null;
				}
			}

			object returnValue;
			try
			{
				returnValue = info.InvokeMethodOnTarget();
			}
			catch (TargetInvocationException ex)
			{
				throw ReflectHelper.UnwrapTargetInvocationException(ex);
			}
			return returnValue;
		}
Exemple #11
0
        private FieldInterceptorObjectReference(SerializationInfo info, StreamingContext context)
        {
            _proxyFactoryInfo = info.GetValue <NHibernateProxyFactoryInfo>(nameof(_proxyFactoryInfo));
            _fieldInterceptor = info.GetValue <IFieldInterceptor>(nameof(_fieldInterceptor));

            if (info.GetBoolean(HasAdditionalDataName))
            {
                _deserializedProxy = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null);

                var additionalMembers = info.GetValue <MemberInfo[]>(AdditionalMemberName);
                if (additionalMembers == null)
                {
                    return;
                }

                foreach (var member in additionalMembers)
                {
                    switch (member)
                    {
                    case FieldInfo field:
                        field.SetValue(
                            _deserializedProxy,
                            info.GetValue(GetAdditionalMemberName(field), field.FieldType));
                        break;

                    case PropertyInfo property:
                        property.SetValue(
                            _deserializedProxy,
                            info.GetValue(GetAdditionalMemberName(property), property.PropertyType));
                        break;

                    default:
                        throw new NotSupportedException(
                                  $"Deserializing a member of type {member.GetType()} is not supported.");
                    }
                }
            }
            else
            {
                // Base type has a custom serialization, we need to call the proxy deserialization for deserializing
                // base type members too.
                var proxyType = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null).GetType();
                var deserializationConstructor = proxyType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                    null,
                    new[] { typeof(SerializationInfo), typeof(StreamingContext) },
                    null);
                _deserializedProxy = deserializationConstructor.Invoke(new object[] { info, context });
            }
            ((IFieldInterceptorAccessor)_deserializedProxy).FieldInterceptor = _fieldInterceptor;
        }
        // 6.0 TODO: merge into IFieldInterceptor
        internal static ISet <string> GetUninitializedFields(this IFieldInterceptor interceptor)
        {
            if (interceptor is AbstractFieldInterceptor fieldInterceptor)
            {
                return(fieldInterceptor.GetUninitializedFields());
            }

            if (interceptor.IsInitialized)
            {
                return(CollectionHelper.EmptySet <string>());
            }

            return(null);            // The caller should use all lazy properties as the result
        }
Exemple #13
0
        public object Intercept(InvocationInfo info)
        {
            if (ReflectHelper.IsPropertyGet(info.TargetMethod))
            {
                if (IsGetFieldInterceptorCall(info.TargetMethod))
                {
                    return(FieldInterceptor);
                }

                if (FieldInterceptor != null)
                {
                    object propValue = info.InvokeMethodOnTarget();

                    var result = FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), propValue);

                    if (result != AbstractFieldInterceptor.InvokeImplementation)
                    {
                        return(result);
                    }
                }
            }
            else if (ReflectHelper.IsPropertySet(info.TargetMethod))
            {
                if (IsSetFieldInterceptorCall(info.TargetMethod))
                {
                    FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
                    return(null);
                }

                if (FieldInterceptor != null)
                {
                    FieldInterceptor.MarkDirty();
                    FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), info.Arguments[0], true);
                }
            }

            return(info.InvokeMethodOnTarget());
        }
Exemple #14
0
        public static bool IsPropertyInitialized(object proxy, string propertyName)
        {
            object entity;

            if (!IsProxyFactoryConfigurated())
            {
                //if the proxy provider it's not configurated, can't be a proxy neither an instrumented field.
                return(true);
            }

            if (proxy.IsProxy())
            {
                ILazyInitializer li = ((INHibernateProxy)proxy).HibernateLazyInitializer;
                if (li.IsUninitialized)
                {
                    return(false);
                }
                else
                {
                    entity = li.GetImplementation();
                }
            }
            else
            {
                entity = proxy;
            }

            if (FieldInterceptionHelper.IsInstrumented(entity))
            {
                IFieldInterceptor interceptor = FieldInterceptionHelper.ExtractFieldInterceptor(entity);
                return(interceptor == null || interceptor.IsInitializedField(propertyName));
            }
            else
            {
                return(true);
            }
        }
 public FieldInterceptorObjectReference(NHibernateProxyFactoryInfo proxyFactoryInfo, IFieldInterceptor fieldInterceptorField)
 {
     _proxyFactoryInfo = proxyFactoryInfo;
     _fieldInterceptor = fieldInterceptorField;
 }