public override bool ValidValue(object dataValue)
        {
            Check.Require(dataValue != null, string.Format(CommonStrings.XMustNotBeNull, "dataValue"));
            IRmType rmType = dataValue as IRmType;

            Check.Require(rmType != null, string.Format(AmValidationStrings.ValueMustImplementIRmType, dataValue.GetType().ToString()));

            bool result = true;

            rmType.Constraint = this;

            if (!IsSameRmType(rmType))
            {
                result = false;
                ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.IncorrectRmType, RmTypeName, rmType.GetRmTypeName()));
            }

            if (!result || !AnyAllowed())
            {
                OpenEhr.RM.Common.Archetyped.Impl.Locatable locatable = dataValue as OpenEhr.RM.Common.Archetyped.Impl.Locatable;

                if (locatable != null)
                {
                    ValidationUtility.PopulateLocatableAttributes(this, locatable);

                    if (Parent != null && ArchetypeNodeId != locatable.ArchetypeNodeId)
                    {
                        result = false;
                        ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.IncorrectNodeId, ArchetypeNodeId, locatable.ArchetypeNodeId));
                    }
                }

                System.ComponentModel.PropertyDescriptorCollection propertyDescriptorCollection = System.ComponentModel.TypeDescriptor.GetProperties(dataValue);

                if (Attributes != null)
                {
                    foreach (CAttribute cAttribute in Attributes)
                    {
                        object attributeObject = null;
                        string attributeName   = RmFactory.GetOpenEhrV1RmName(cAttribute.RmAttributeName);
                        System.ComponentModel.PropertyDescriptor property = propertyDescriptorCollection.Find(attributeName, true);

                        // if the attributeName is not a class property, it must be a class function.
                        if (property == null)
                        {
                            System.Reflection.MethodInfo method = dataValue.GetType().GetMethod(attributeName);

                            if (method == null)
                            {
                                result = false;
                                ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.UnexpectedAttributeX, attributeName));
                                continue;
                            }
                            else
                            {
                                attributeObject = method.Invoke(dataValue, null);
                            }
                        }
                        else
                        {
                            attributeObject = property.GetValue(dataValue);
                        }

                        if (attributeObject == null)
                        {
                            if (cAttribute.Existence.Lower > 0)
                            {
                                result = false;
                                ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.TmExpectedConstraintMissing, cAttribute.RmAttributeName));
                            }
                        }
                        else if (cAttribute.Existence.Upper == 0)
                        {
                            result = false;
                            ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.TmForbiddenConstraint, cAttribute.RmAttributeName));
                        }
                        else if (!cAttribute.ValidValue(attributeObject))
                        {
                            result = false;
                        }
                        else
                        {
                            DvCodedText codedText = dataValue as DvCodedText;

                            if (codedText != null && cAttribute.RmAttributeName == "defining_code")
                            {
                                // validate the code string before validating the coded value
                                if (codedText.DefiningCode.TerminologyId.Value == "local")
                                {
                                    CObject        parentObject   = cAttribute.parent;
                                    CArchetypeRoot cArchetypeRoot = ValidationUtility.GetCArchetypeRoot(parentObject);

                                    if (!cArchetypeRoot.TermDefinitions.HasKey(codedText.DefiningCode.CodeString))
                                    {
                                        result = false;
                                        string code = codedText.DefiningCode == null ? "" : codedText.DefiningCode.CodeString;
                                        ValidationContext.AcceptValidationError(this, string.Format("code {0} is not existing archetype term", code));
                                    }
                                }
                                if (result && !ValidationUtility.ValidValueTermDef(codedText, cAttribute, ValidationContext.TerminologyService))
                                {
                                    result = false;
                                    string code = codedText.DefiningCode == null ? "" : codedText.DefiningCode.CodeString;
                                    ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.TextValueXInvalidForCodeY, codedText.Value, code));
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }