c# - Can I do automatic basic DataAnnotations validation with the IValidatableObject interface? -


i'm using lot of ef4 poco entities default dataannotations validationattribute (required, stringlength, regularexpression, etc) on properties (this generated t4 script use) , want custom validate of entities via ivalidatableobject interface.

my question is: can call, in implementation of ivalidatable.validate() method, base method validating properties on assositiated validationattribute? cost time writing same code validating each property.

example validation:

    public ienumerable<validationresult> validate(validationcontext validationcontext)     {         icollection<validationresult> validationresults = new list<validationresult>();          if (string.isnullorempty(databasename))         {             validationresults.add(new validationresult(messages.error_orderedcomponentdatabase_databasename_required, new string[] { "databasename" }));         }         else if (databasename.length > 50)         {             validationresults.add(new validationresult(messages.error_orderedcomponentdatabase_databasename_toolong, new string[] { "databasename" }));         }          return validationresults;     } 

preferred implementation (pseudo code):

    public ienumerable<validationresult> validate(validationcontext validationcontext)     {         icollection<validationresult> validationresults = someinstance.validateondataannotations(this);        // custom validation...          return validationresults;     } 

any suggestion welcome! in advance.

found solution. made linq query based on following post: dataannotations validation class.

question answered.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -