DataAnnotations

3 posts, 0 answers
  1. Mac
    Mac avatar
    36 posts
    Member since:
    Nov 2006

    Posted 01 Aug 2009 Link to this post


    I've been looking at DataAnnotations in an MVC project which uses OpenAccess.

    What's the best way to extend the generated classes to include the additional UI agnostic validation that DataAnnotations provide?

    Something like this is the first thing off the top of my head as I've used a similar approach elsewhere

        using System; 
        using System.Collections.Generic; 
        using System.ComponentModel; 
        using System.ComponentModel.DataAnnotations; 
        using System.Linq; 
     
        [MetadataType(typeof(CountryMetaData))] 
        public partial class Country : IValidatedModel 
        { 
        } 
     
        public class CountryMetaData 
        { 
            [Required(ErrorMessage = "CountryId is required")] 
            [Range(1, Int32.MaxValue, ErrorMessage = "CountryId must be a positive integer")] 
            public int CountryId 
            { 
                get
                set
            } 
        } 
     
        /// <summary> 
        /// A basic class for encapsulating a validation error message 
        /// </summary> 
        public class ValidationMessage 
        { 
            public string PropertyName { getset; } 
            public string ErrorMessage { getset; } 
        } 
     
        /// <summary> 
        /// This is just provided as a means for extension methods 
        /// </summary> 
        public interface IValidatedModel 
        { 
        } 
     
        public static class ExtensionMethods 
        { 
            /// <summary> 
            /// Get all validation errors for the given instance 
            /// </summary> 
            public static IEnumerable<ValidationMessage> GetErrors(this IValidatedModel instance) 
            { 
                var metadataAttrib = instance.GetType().GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().FirstOrDefault(); 
                var typeObject = metadataAttrib != null ? metadataAttrib.MetadataClassType : instance.GetType(); 
                var typeProperties = TypeDescriptor.GetProperties(typeObject).Cast<PropertyDescriptor>(); 
                var classProperties = TypeDescriptor.GetProperties(instance.GetType()).Cast<PropertyDescriptor>(); 
     
                return from property in typeProperties 
                       join modelProp in classProperties on property.Name equals modelProp.Name 
                       from attribute in property.Attributes.OfType<ValidationAttribute>() 
                       where !attribute.IsValid(modelProp.GetValue(instance)) 
                       select new ValidationMessage { PropertyName = property.Name, ErrorMessage = attribute.FormatErrorMessage(string.Empty) }; 
            } 
     
            /// <summary> 
            /// Are there any validation errors? 
            /// </summary> 
            public static bool IsValid(this IValidatedModel model) 
            { 
                return !model.GetErrors().Any(); 
            } 
        } 

    This approach loses the benefit of reverse mapping the data source though. Is there a better way to bake in the validation by extending OpenAccess somehow and is there any plans to support dataannotations in the openaccess roadmap?

    Thanks in advance
    Mac.
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 04 Aug 2009 Link to this post

    Hello Mac,

    As I suggested you in the support ticket (Ticket ID: 231981) you can edit the templates that Telerik OpenAccess ORM uses to generate the classes so this functionality can be embedded in each reverse mapped class. More information on how to edit the templates can be found in this blog post.

    Sincerely yours,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Mac
    Mac avatar
    36 posts
    Member since:
    Nov 2006

    Posted 04 Aug 2009 Link to this post

    Interesting post, thanks for the link.

    Mac.
Back to Top