views:

119

answers:

2

I have used the Entity Framework with VS2010 to create a simple person class with properties, firstName, lastName, and email. If I want to attach DataAnnotations like as is done in this blog post I have a small problem because my person class is dynamically generated. I could edit the dynamically generated code directly but any time I have to update my model all my validation code would get wiped out.

First instinct was to create a partial class and try to attach annotations but it complains that I'm trying to redefine the property. I'm not sure if you can make property declarations in C# like function declarations in C++. If you could that might be the answer. Here's a snippet of what I tried:

namespace PersonWeb.Models
{
  public partial class Person
  {
    [RegularExpression(@"(\w|\.)+@(\w|\.)+", ErrorMessage = "Email is invalid")]
    public string Email { get; set; } 
    /* ERROR: The type 'Person' already contains a definition for 'Email' */
  }
}
A: 

You need to either use a metadata "buddy" class or (my preference) project onto a presentation model instead of binding views directly to entities.

Craig Stuntz
Can you give some more detail on your approach to metadata "buddy" classes. I'd prefer if the presentational code and validation had a separation of concerns.
dcompiled
"My approach" to "buddy" classes is to not use them. Like I said, I use presentation models. If you want *real* separation of concerns, that's the way to go.
Craig Stuntz
Its just a matter of style, I think all the validation code and associated metadata should solely remain in the model. It seems you prefer to have validation code mixed with the presentation model.
dcompiled
No, that completely misunderstands what I said. Data integrity validations, which belong in the model, are not the same thing as presentation model metadata. In MVC, this distinction is illustrated by the difference between read-only properties in the model and associated metadata providers in the presentation. Model rules should surface in the presentation automatically; manually decorating via attributes should only be necessary when the rules for a particular scenario are more restrictive than the model on general.
Craig Stuntz
+2  A: 

A buddy class is more or less the direction your code snippet is journeying, except your manually coded partial Person class would have an inner class, like:

[MetadataType(typeof(Person.Metadata))]
public partial class Person {
    private sealed class MetaData {
        [RegularExpression(...)]
        public string Email { get; set; }
    }
}

Or you could have your manually partial Person class and a separate Meta class like:

[MetadataType(typeof(PersonMetaData))]
public partial class Person { }

public class PersonMetaData {
[RegularExpression(...)]
public string Email;
}

These are workarounds and having a mapped Presentation class may be more suitable.

Michael Finger