Exemplo n.º 1
0
        private void ValidateEntity(object o, object id, PersistenceOperationEnum persistenceOperation, IList <string> properties, IList <IType> types, IList <object> currentState, IList <object> previousState)
        {
            if (ReferenceEquals(_session, null))
            {
                return;
            }
            if (!ReferenceEquals(_session.Transaction, null) && _session.Transaction.WasRolledBack)
            {
                return;
            }
            var factoryName = _session.GetSessionImplementation().Factory.Settings.SessionFactoryName;
            var epc         = new EntityPersistenceContext {
                FactoryName = factoryName, Id = id
            };

            if (properties != null)
            {
                for (var i = 0; i < properties.Count; i++)
                {
                    if (currentState != null)
                    {
                        epc.CurrentState.Add(properties[i], currentState.ElementAtOrDefault(i));
                    }
                    if (types != null)
                    {
                        epc.Types.Add(properties[i], types.ElementAtOrDefault(i));
                    }
                }
            }
            switch (persistenceOperation)
            {
            case PersistenceOperationEnum.Adding:
                epc.IsBeingAdded = true;
                break;

            case PersistenceOperationEnum.Updating:
                epc.IsBeingModified = true;
                if (properties == null || previousState == null)
                {
                    break;
                }
                for (var i = 0; i < properties.Count; i++)
                {
                    epc.PreviousState.Add(properties[i], previousState.ElementAtOrDefault(i));
                }
                break;

            case PersistenceOperationEnum.Removing:
                epc.IsBeingRemoved = true;
                break;

            default:
                throw new ArgumentOutOfRangeException(persistenceOperation.ToString());
            }
            var validationResults = EntityValidator.DoMemberValidation(o, epc).ToList();

            if (validationResults.Count() == 0)
            {
                if (_validationResults.ContainsKey(o))
                {
                    //remove the validation errors from a previous flush
                    _validationResults.Remove(o);
                }
                return;
            }
            if (!_validationResults.ContainsKey(o))
            {
                //add the validation results for the entity
                _validationResults.Add(o, validationResults);
            }
            else
            {
                //replace the validation results for the entity
                _validationResults[o] = validationResults;
            }
        }