M
M
Michael2015-02-11 19:13:47
ASP.NET
Michael, 2015-02-11 19:13:47

How to generate a view model from a domain model in ASP.NET MVC?

Good day to all, I ran into a problem, it is necessary to display, using the view model, brief information about the announcement of the car.
There are entities in the domain model, BrandAuto, ModelAuto, SpecificaAuto, AdvertisingAuto. - this is all a domain model, they are interconnected - "one to many" and have their own fields, of course I do not need all these fields for presentation.
How can I create a ViewModel that would display:

  • Car name (this is the Brand entity)
  • Auto model name (ModelAuto entity)
  • The price from the AdvertisenmentAuto entity.

How to combine all this into a single view model?
I read a bunch of articles about the fact that you don’t need to use the domain model directly and unload data from it into the view, there are view models for this. But unfortunately, there is not a lot of one theory with examples, in the book consider the same thing, Jeffrey Palermo (for me personally) sometimes jokes with the reader, they say, think it out yourself, and you have to think it out.
In general, after sitting and thinking (it's been two days already), nothing good comes to mind.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Abakumov, 2015-02-11
@MickMS

Hello Michael again! In general, less words - more code. Toster_q187249.zip - here is the archive with the application. I modeled the subject area as I understood :)
To be honest, I have never heard the phrase "domain model". Where does this term come from? Usually, the terms "data", "model" are used to describe data.
Next: Read from cover to cover " ASP.NET MVC 3/4 Framework with C# Examples for Professionals " by Adam Freeman, Steven Sanderson. For MVC - in my opinion, this is the #1 book for learning ASP.NET MVC. Everything you need is there, and even more. You, however, may have difficulties with the Entity Framework, because there they do not explain in such detail how to work with it (the authors probably count on a little experience of the readers with this framework). After reading, everything will fall into place in your head.
There will be some questions about the project - write. It is, of course, primitive, but for learning purposes, in my opinion, it will do.
You probably already know, but, nevertheless, I will write: the controller should have a minimum of business logic. The controller should only receive data from somewhere, form a view model from it and give the view model to the view itself. All the logic of working with data should be placed in a separate layer (for example, in a separate dll). All business logic, for good, should also be placed in a separate layer. As a result, the controllers of the MVC application must “break” into the business logic layer, which, in turn, breaks into the layer for working with data, pulls them out, processes them and gives them “upstairs” to the client application (MVC-application). This allows you to create a three-level solution architecture and thereby create more scalable and flexible applications.
For example, the application that I sent you consists of 2 layers:
1) MVC-app;
2) Core.DAL - layer for working with data.
Ideally, you should add a new layer (for example, Core.BL or Core.BAL) - a layer of business logic.
Here's just in case:How to organize the architecture of the Project Management System applications?
Good luck in our hard work!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question