Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Buddy Classes, ViewModels and Validation
    primarykey
    data
    text
    <p>I'm currently working on my first MVC project which is nothing more than a glorified blog for my own learning purposes. I started out with a database-first approach as I had no idea code-first even existed at that point. That's left me with some confusion as to how I should be handling validation.</p> <p>I have an <code>Articles</code> table in the database which has a corresponding <code>Article</code> class which has been generated for me by EF. I also have an <code>ArticleViewModel</code> class which has a matching property for each of the properties in <code>Article</code>. Whilst I have simple CRUD operations working, they are currently using a strongly-typed <code>Article</code> model instead of <code>ArticleViewModel</code> and there is no data validation taking place.</p> <p>After doing some research today to evaluate my options, I've found the <a href="https://stackoverflow.com/questions/5700464/asp-mvc3-database-first">following</a> <a href="https://stackoverflow.com/questions/5797312/ef-model-first-or-code-first-approach">questions</a> and <a href="http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx" rel="nofollow noreferrer">this</a> blog post. So it looks as though I can apply the <code>MetadataType</code> attribute to the EF generated classes and put my validation there, but I keep reading that buddy classes containing validation are bad practice. I have no idea why that is, but if that's the case, would that mean I should ignore buddy classes and be doing something like the following?</p> <pre><code>[MetadataType(typeof(ArticleViewModel))] public partial class Article { public Article(ArticleViewModel avm) { // Copy the properties from the ArticleViewModel into an Article. } } </code></pre> <p>Then applying DataAnnotations to the ArticleViewModel as necessary and updating my controllers/views to use <code>ArticleViewModel</code>?</p> <p>Thanks in advance for any input. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload