views:

39

answers:

1

In ASP.NET MVC we can use the System.ComponentModel.DataAnnotations namespace to assist with form/ model input validation.

If I didn't want to be tied to any specific framework i.e. xVal, EntLib Val Block, etc. and I wanted to create basically a wrapper classes to protect my code from the direct dependency how would one accomplish that?

So instead of my model property looking like this;

[Required]
property string Name {get;set;}

I want it to be tied to my code where the validation framework preferred will be implemented.

[MyRequired]
property string Name {get;set;}
+1  A: 

One way to do it is to derive your attribute from the underlying ones. EG:

public class MyRequiredAttribute : EntLib.RequiredAttribute { }

That way you could modify the MyRequiredAttribute class later to derive from a different base class, and the rest of your code wouldn't need to change (you would probably however need to recompile it). The EntLib (etc) code that scans for EntLib.RequiredAttribute should still pick your derived attribute up

This relies on the underlying attribute not being sealed though.

Orion Edwards