Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Model Patterns and usage in ASP.NET MVC3 (Also, using EF 4.1)

I have been searching for an answer to this question for days and it is driving me insane. Currently I am working on a project using ASP.NET MVC 3 and am trying to utilize a ViewModel per controller approach as has been suggested by so many articles and tutorials I have checked out. To better illistrate what I am asking I will outline below:

Lets say I have a pretty simple and straight forward model. Users, Customers, Addresses, Phone Numbers, Orders, Products, Categories, etc... When a user registers for a new account on my site I would like to: 1) create an account for them (this is just an account id, customer type) 2) Add their customer demographic data to Customers 3) Add N-addresses and address types 4) Add N-phone numbers with type as well.

As far as I have got is deciding that I need a RegisterCustomerForRegistrationControllerViewModel. My predicament is what does this model look like? I am trying to be as DRY as possible yet when implementing this pattern I seem to repeat myself at each turn. At what level do I put DataAnnotations for validation? So do I simply new up a new Customer() even if I only want to use one property from the class in a given ViewModel?

I'm not even confident at this point that this is a correct assumption. There seems to be so much opinion on the topic yet so few concrete examples of implementation. I am hoping someone can point me in the right direction and maybe present some code snippets along the way... I hope this is all clear enough and if not please feel free to ask follow up questions.

Again, Thanks in advance!

like image 613
Clayton Bench Avatar asked Jul 12 '11 16:07

Clayton Bench


People also ask

What is EF in MVC?

It is a data access framework which used to create and test data in the visual studio. It is part of . NET Framework and Visual Studio. The latest package is shipped as Entity Framework NuGet Package.

What is ASP Net mvc3?

ASP.NET MVC 3 is a framework for developing highly testable and maintainable Web applications by leveraging the Model-View-Controller (MVC) pattern.

Which command of EF core in MVC application will create the context classes for application data model?

Create the database context The main class that coordinates EF functionality for a given data model is the DbContext database context class. This class is created by deriving from the Microsoft. EntityFrameworkCore. DbContext class.

What is mvc3 application?

ASP.NET MVC 3 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the . NET Framework.


1 Answers

Repeating simple properties across two distinct layers of an application is not a violation of DRY. Its just good design.

DataAnnotations go on ViewModels.

ViewModel will look something like

public class RegisterCustomerViewModel
{
    [Required]
    public string Name { get; set; }
    public List<AddressViewModels> Addresses { get; set; }
    public List<PhoneNumberViewModel> PhoneNumbers { get; set; |

} 
like image 109
John Farrell Avatar answered Oct 27 '22 00:10

John Farrell