views:

469

answers:

2

I'm using a generated class as a model, and I wish to add DataAnnotation attributes to some of its properties. As it's a generated code, I don't want to add the annotations directly. Is there another way to attach them to a property?

I'd considered making the model an interface, and using a partial class to get the generated class to subscribe to it. Is there a less elaborate solution, assuming that would even work?

+4  A: 

Yes there is. You have to create metadata class that will have the same properties that your original model, and connect it to your model with MetadataType attribute:

[MetadataType(typeof(MyModelMetadata))]
public partial class OriginalMyModel
{
}

public class MyModelMetadata
{
    [Required]
    public string MyProperty;  

    // ...
}

In the example ebove OriginalModel is your proper model class, and MyModelMetadata is a class used only for annotating properties. MyModelMetadata should have the same properties that your model has.

PanJanek
You might want to use public class partial MyModelMetadata as quite often you're targeting partials on Entity Framework/LINQ to SQL ... even if not it's nice to have attributed out code separate.
Nissan Fan
Thank you. That's exactly what I"m looking for.Could I also do this to add serialization attributes to generated classes? Or is the specific to MVC? And when you say that hte metadata class should have the same properties, does that mean it has to have all of them? Or that the properties I use just have to line up?
Mike Pateras
DataAnnotation attributes are completely independent of MVC. They are just one of many ways to manage validation, and MVC2 default model binder use them by default. Metadata class doesn't have to have all the model properties - only these that need to be annotated are required. What did you mean by serialization attributes?
PanJanek
DataMember, for example. Thanks for the answers.
Mike Pateras
+2  A: 

You can use the MetadataType attribute on your class:

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

In practice, I've seen the metadata get out of sync with a generated model pretty frequently, though, which can lead to some headaches. You may want to look into an alternate validation mechanism instead of data annotations.

I've been using Fluent Validation, which is very easy to pick up and start using. There is even a Fluent Validation to xVal integration piece in Fluent Validation 2.0 (still in beta) that you can bring into your project for client-side validation.

Fluent Validation allows you to define your validation in a separate class. All you would need to do is add an attribute to your generated class telling it what validator to use, which could be accomplished through partial classes.

Alternatively, you could create view-specific models that are mapped to from your domain model that contain your data annotations. In that case, simplify the back-and-forth mapping using something like AutoMapper. Then, if your domain model changes, you get compile-time errors versus the metadata approach.

mkedobbs
DataAnnotationValidators are very flexible and pretty much the in-built validation framework for .NET going forward. Lots of real nice validation techniques out there like xVal etc., but I'm partial to using them especially with ASP.NET MVC.
Nissan Fan