public override bool Perform(object instance, object fieldValue)
        {
            ActiveRecordValidationBase arInstance = (ActiveRecordValidationBase)instance;
            ActiveRecordModel          model      = ActiveRecordBase.GetModel(arInstance.GetType());

            while (model != null)
            {
                if (model.Ids.Count != 0)
                {
                    _pkModel = model.Ids[0] as PrimaryKeyModel;
                }

                model = model.Parent;
            }

            if (_pkModel == null)
            {
                throw new ValidationFailure("We couldn't find the primary key for " + arInstance.GetType().FullName +
                                            " so we can't ensure the uniqueness of any field. Validatior failed");
            }

            _fieldValue = fieldValue;

            return((bool)arInstance.Execute(new NHibernateDelegate(CheckUniqueness)));
        }
Example #2
0
        /// <summary>
        /// Perform the check that the property value is unqiue in the table
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="fieldValue"></param>
        /// <returns><c>true</c> if the field is OK</returns>
        public override bool IsValid(object instance, object fieldValue)
        {
            Type instanceType       = instance.GetType();
            ActiveRecordModel model = ActiveRecordBase.GetModel(instance.GetType());

            while (model != null)
            {
                if (model.PrimaryKey != null)
                {
                    pkModel = model.PrimaryKey;
                }

                model = model.Parent;
            }

            if (pkModel == null)
            {
                throw new ValidationFailure("We couldn't find the primary key for " + instanceType.FullName +
                                            " so we can't ensure the uniqueness of any field. Validatior failed");
            }

            IsUniqueValidator.fieldValue = fieldValue;

            SessionScope scope        = null;
            FlushMode?   originalMode = null;

            if (SessionScope.Current == null /*||
                                              * SessionScope.Current.ScopeType != SessionScopeType.Transactional*/)
            {
                scope = new SessionScope();
            }
            else
            {
                originalMode = ActiveRecordBase.holder.CreateSession(instanceType).FlushMode;
                ActiveRecordBase.holder.CreateSession(instanceType).FlushMode = FlushMode.Never;
            }

            try
            {
                return((bool)ActiveRecordMediator.Execute(instanceType, CheckUniqueness, instance));
            }
            finally
            {
                if (scope != null)
                {
                    scope.Dispose();
                }

                if (originalMode != null)
                {
                    ActiveRecordBase.holder.CreateSession(instanceType).FlushMode = originalMode ?? FlushMode.Commit;
                }
            }
        }