A couple of questions about asp.net MVC model models

I am new to MVC.

I read this short bit detailing three ways to work with the view model in MVC:

http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

The bottom line seems to me that:

Method 1, pull an object from the database and use it as a model for your view. Quick and easy, but if you need data from multiple tables, you are completely screwed up (I can't think of a way around this without method 2).

Method 2, create a class that has references to several objects, and use this as your presentation model. This way you can access everything you need. The article says that when the views become complex, it breaks due to the mismatch of the impedance between the objects of the domain / view model ... I do not understand what this means. The impedance resistance in Google Gogling returned a lot of material, the essence of which was that you represent the database material using objects, and all this is not clean, but you probably ran into this problem even with method 1. Not sure what am I missing. It also seems to me that creating a class for each view to get the required data is not ideal from a service point of view, not sure if you have a choice.

Method 3, I still hug it, but I don’t quite understand why their example flag will not work in method 2 if you added the bool addAdditional class to the class that was not associated with the domain model. Method 3 seems to say rather than return the domain material directly, just pull out the properties you need, which I think is better, but it will be harder to maintain with them, since you will need large constructors that do this.x = domain.x , this.y = domain.y , etc.

I do not understand the builder, namely why the interface is used, but will continue to work on it.

Edit: I just realized that this is not a question, to my question, do I think correctly?

+4
source share
2 answers

The problem I faced with C # 2 is that I need to complete one of these two tasks:

  • Include each separate field for each object in the form - those that will not be displayed should be included, but hidden.

  • Include only those specific fields that I need, but use AutoMapper or something similar to map these fields to actual objects.

So, with No. 2, I see a discrepancy between what I want to do and what I have to do. If we move on to No. 3, this discrepancy will be removed (from what I understand, I looked briefly at it). It also fixes the problem that a certain hacker can send values, such as id fields or similar ones, when using method No. 2, if I was not very careful, it could be written to my data warehouse. In other words, you can update anything on any of the objects, unless you are very careful.

Using method # 3, you can use AutoMapper or similarly to do the dirty work of mapping a custom object to data warehouse objects without worrying about security issues / impedance identified by method # 2 (see the comment for more information on security issues with No. 2).

+1
source

You are probably right that the impedance mismatch is present in both methods 1 and 2 - it appears wherever you are between objects in the code and database objects with relational mapping. Jeff Atwood writes about this and cites this article , which is a fantastic discussion of everything related to object-relational mapping, or Vietnam Computer Science. What you pretty much end up doing is weighing the pros and cons of all these approaches and choosing the one that sounds like it fits your needs, and then realizes that you have chosen the wrong one. Or maybe you are more fortunate than me, and I can fix it the first time. In any case, this is a hairy problem.

0
source

All Articles