1

I would like some suggestions of using models generated by EF T4 POCO Generator, and leveraging validation attributes in them like normal models do in /Models in MVC project.

Background: We are trying to design a web app, 2 or 3 developers will work on it. It is in a scrum style where we get requirement modifications periodically from our client as we build the web app. The tables, columns and relations may change accordingly.

Our Approach: We want to use EF T4 POCO Generation, as every time when new tables and columns are added to the db, EF T4 can update the POCO models automatically. We want to use these POCO models as the Model layer of the MVC project (in a different Models.dll and include it into the project, instead of sitting in the /Models/ folder of MVC project).

Problem: We want to add attributes to the model classes and properties. However, EF T4 will override the model classes and we can't (and don't think we are supposed to) add these attributes into those models. We don't know what is the correct approach.

I can only think of making attributes in wrappers ViewModels in the MVC project. They wrap around all the necessary models from the Models.dll done by EF T4. Then put attributes in front of them. But it looks ugly to me defeating the DRY principle. How would you do it? Thanks ahead for the ideas!

--- EDIT --- I did find a cool trick here assisting my wrapper idea. However, I didn't mention it because I want to be open to see different suggestions and compare them :)

Community
  • 1
  • 1
Tom
  • 15,781
  • 14
  • 69
  • 111

1 Answers1

2

Use buddy classes. Your EF classes are generated as partials. Create another partial class and annotate it with MetadataType attribute.

[MetadataType(typeof(FriendMeta))]
public partial class Friend
{
}

Then you can add data annotations attributes to the properties of the same name in the buddy class.

public class FriendMeta
{
    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [DisplayName("Date of Birth")]
    public DateTime BrithDate { get; set; }
}

ASP.NET MVC metadata provider will pick those attributes for you.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • So this buddy/wrapper/meta class seems like the way to go. Thanks :) – Tom May 23 '12 at 06:48
  • 1
    The problem with using metadata buddy-classes is, that if you are using a 3tier architecture you would propably put all your POCO classes in a separate library. the buddy classes then also needs to be in this library - and so, you have to define your displayname strings in the library that should only be acting as a dump container for your business objects. – Gerwald Aug 26 '12 at 10:42
  • @leo - you're right, but I don't think this is a problem OP is facing. – Jakub Konecki Aug 29 '12 at 21:36
  • 1
    @Jakub: what do you mean by OP? – Gerwald Aug 30 '12 at 07:23
  • 1
    @leo - http://meta.stackexchange.com/questions/79804/whats-stackexchange-ese-for-op – Jakub Konecki Aug 30 '12 at 09:08
  • @JakubKonecki OP: this was something I have long wanted to know but never took the time to Google about it... Now I know thanks to you! :) By the way... I got here trying to find a better way of adding data annotations to EF T4 POCO classes. I'd would be awesome if we could add the annotations using the Designer properties instead of having to create buddy classes. – Leniel Maccaferri Jan 29 '13 at 01:08