2

I'm very new to ASP.NET MVC (as well as ASP.NET in general), and going to gain some knowledge for this technology, so I'm sorry I can ask some trivial questions. I have installed ASP.NET MVC 3 RC1 and I'm trying to do the following.

Let's consider that I have a model that's completely auto-generated from a table using the "LINQ to SQL Classes" template in VS2010. The template generates 3 files (two .cs files and one .layout file respectively), and the generated partial class is expected to be used as an MVC model. Let's also consider, a single DB column, that's mapped into the model, may look like this:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Name", DbType = "VarChar(128)")]
public string Name {
    get {
        return this._Name;
    }
    set {
        if ( (this._Name != value) ) {
            // ... generated stuff goes here
        }
    }
}

The ASP.NET MVC engine also provides a beautiful declarative way to specify some additional stuff, like RequiredAttribute, DisplayNameAttribute and other nice attributes. But since the mapped model is a purely auto-genereated model, I've realized that I should not change the model manually, and specify the fields like:

[Required]
[DisplayName("Project name")]
[StringLength(128)]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Name", DbType = "VarChar(128)")]
public string Name {
    ...

though this approach works perfectly... until I change the model in the DBML-designer removing the ASP.NET MVC attributes automatically. So, how do I specify ASP.NET MVC attributes for the DBML models and their fields safely?

Thanks in advance, and Merry Christmas.


Update #1:

While expecting for responses, I have found such stuff:

Community
  • 1
  • 1
Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105

2 Answers2

1

The more correct approach is to make corresponding DTO's (ViewModels or whatsoever you call it) and Map those DTO's to your Data Model Objects with tools like AutoMapper(translate your fat data model objects to clean objects and vice versa).

There exists some tools that will do this task(Making DTO's) for you and generate those DTOs.

Look at VS gallery and chances are you find many options.

Maybe finding a nice sample like MicrosoftNLayerApp employing all these techniques will guide you throughout this process.

And finally your answer is to annotate DTO class properties with DisplayName attribute.

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • +1, the key is to give up on the idea of having one data obejct to rule them all. It's a difficult struggle at first, but worth it. – Kirk Woll Dec 26 '10 at 19:31
  • Thank you for the reply, Jani. However, I have found an example, that's closer to mine. Taken from http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx , uses `MetadataTypeAttribute`, but for some reason it doesn't work. – Lyubomyr Shaydariv Dec 26 '10 at 19:37
  • @Lyubomyr I thought it's just for validation not has the functionality of DisplayName, Am I wrong? – Jahan Zinedine Dec 26 '10 at 20:03
  • No, not just for validation. E.g. `RequiredAttribute`, `DisplayName`, `StringLength`... However, I can't make `MetadataTypeAttribute` work, and can't even see the reason why. – Lyubomyr Shaydariv Dec 26 '10 at 20:09
  • after a couple of re-reads I like the answer, but I'm really not fond of the terminology. DTO (particularly in the context of .NET) implies something used in the likes of web services, much prefer view model as being more appropriate. Use of the term "DTO" meant that on the first two attempts I completely didn't get what you were suggesting as I was distracted by it being (from my background) inappropriate to context. – Murph Dec 27 '10 at 13:45
0

Since I do not want to create any helper classes, I found the article that meets my requirements the most. However, that approach does not work for some reason, but I still believe this is because of my lack of ASP.NET MVC experience (see Step 4: Creating a Custom [Email] Validation Attribute):

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105