How to organize viewmodels and mappings in the project?

Asked

Viewed 117 times

-1

Good morning, everyone ! I am studying about mapping (model and viewmodel), using implicit Operators. But a question arose: How to organize the viewmodels and mappings within the project ? Where should I save the classes ? Inside the model class file or create your own or even a folder only for viewmodels ? And about mapping, which "location" should I write the code ?

Thanks in advance for the help !

  • Hello @Renato, this question is a little wide. It would be better to separate into smaller questions and exemplify what you want, so it will be easier to help you.

  • Example: I have the User model and the viewmodel Login. As I will map "in hand" (implicit Operators), the doubt is organizational. First question: create the viewmodel class Login in the User model file ? Second question: The mapping, where should I create it ? In the User model file ? Forgive me for not being more explicit.

  • So Renato, this question is wide. Do you want to know how to do it? Where to do it? Name of the directories where to put the classes? All these are different questions, which leaves your question wide open

  • As I said, it is an organizational question of how the project structure should look. I create a folder only for viewmodels ?

  • A folder is usually created for ViewModels, thus leaving a better organization. That is, if you are using a standard MVC structure.

  • Yes, it’s MVC. I appreciate the guidance !

Show 1 more comment

1 answer

0


Whereas Viewmodels serve only to serve the modeling/typing of Views, it should stay in the presentation project - if it is MVC, in the Web project, if it is Webapi, in the API project. They should not be in the same project as the models, because they belong to another context.

To help in mapping Models <-> ViewModels, I use the Automapper. Very easy to implement and very fast. Follows a file from Eduardo Pires about Automapper and Viewmodels. It will help you to implement this.

  • Thanks for the indication ! I will study the Automapper

  • Okay, I was curious. Because of the negative?

  • Buddy, I must have been mistaken in "tickar". Corrected. But, negatively my pq ? rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.