5

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!

2 Answers 2

4

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; |

} 
Sign up to request clarification or add additional context in comments.

16 Comments

That's all well and good, but now you've got business logic in the UI layer. Your business layer needs to also enforce the Name Required rule, or you'll lose it when you throw away your MVC UI for whatever UI is next.
@Andy: You should perform validation at every level in your application. I don't think @jfar was implying otherwise.
@Andy, thats not business logic, thats input sanitation which you are required to do in the UI level anyway. DataAnnotations attributes can be used in another UI layer. There is no limitation there and are designed for that purpose.
I think it's business logic. Would you ever allow someone to register without entering thier name? Who makes that call? Probably the business, and its a rule you'd want to keep regardless of UI. Yes, you can reuse DataAnnotations in other layers, but you have to figure out how to run them yourself. I'm not aware of any engine that takes care of that for you. Also, unless you want your business layer to see your VM (which I personally don't), you'll have to repeat the annotation on your business model, in other words, you need to repeat it in two places or you'll have problems.
@jfar Hmmmm that is interesting. It never even crossed my mind that I would need to create classes for Address or PhoneNumber view models to be included inside of the ViewModel. I would have just used the main class. Thank you for your input.
|
1

Just like jfar, I would take a simple approache: one view, one view model with DataAnnotations.

That being said... I know how you feel (not confident) and I understand because I've been through that myself. My conclusions: unless you consider your web application to require the overhead of so many layers, principles and patterns, keep it simple. I believe that there is no perfect architecture. There's just what works and there's overhead. Sometime, what works is indeed complexe. Ask yourself if you need that complexity.

Take my first sentence as an answer to your question and the rest as my humble opinion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.