private void Validate(INakedObjectAdapter adapter)
 {
     if (adapter.Spec.ContainsFacet <IValidateProgrammaticUpdatesFacet>())
     {
         string state = adapter.ValidToPersist();
         if (state != null)
         {
             throw new PersistFailedException(Log.LogAndReturn(string.Format(Resources.NakedObjects.PersistStateError, adapter.Spec.ShortName, adapter.TitleString(), state)));
         }
     }
 }
예제 #2
0
        public string ValidToPersist()
        {
            var objectSpec = Spec as IObjectSpec;

            Trace.Assert(objectSpec != null);

            IAssociationSpec[] properties = objectSpec.Properties;
            foreach (IAssociationSpec property in properties)
            {
                INakedObjectAdapter referencedObjectAdapter = property.GetNakedObject(this);
                if (property.IsUsable(this).IsAllowed&& property.IsVisible(this))
                {
                    if (property.IsMandatory && property.IsEmpty(this))
                    {
                        return(string.Format(Resources.NakedObjects.PropertyMandatory, objectSpec.ShortName, property.Name));
                    }
                    var associationSpec = property as IOneToOneAssociationSpec;
                    if (associationSpec != null)
                    {
                        IConsent valid = associationSpec.IsAssociationValid(this, referencedObjectAdapter);
                        if (valid.IsVetoed)
                        {
                            return(string.Format(Resources.NakedObjects.PropertyInvalid, objectSpec.ShortName, associationSpec.Name, valid.Reason));
                        }
                    }
                }
                if (property is IOneToOneAssociationSpec)
                {
                    if (referencedObjectAdapter != null && referencedObjectAdapter.ResolveState.IsTransient())
                    {
                        string referencedObjectMessage = referencedObjectAdapter.ValidToPersist();
                        if (referencedObjectMessage != null)
                        {
                            return(referencedObjectMessage);
                        }
                    }
                }
            }

            var validateFacet = objectSpec.GetFacet <IValidateObjectFacet>();

            return(validateFacet == null ? null : validateFacet.Validate(this));
        }