ASP.NET MVC - view model, domain model and data model [closed]
Asked Answered
B

2

7

I am using entity framework in my latest ASP.NET MVC 3 project. As it is DB first, the entity framework generates Database models. In my service (business) layer I Scaffold (MvcScaffolding) to generate service methods, views and controllers. Scaffolding also generates the domain models. When binding these models to the Views, I use view models.

In short, I ended up in using three types of models. Is this Ok? The view models are kept in the Presentation layer, domain models are kept in the business layer and data models are kept in the repository layer.

Please let me know your thoughts.

Bullington answered 20/11, 2012 at 9:0 Comment(0)
M
4

That sounds fine and indeed has several benefits.

  1. You can recreate your database models from scratch without affecting the domain models, except how they are mapped of course. Some would argue that these two could be merged into one but it deeply depends on your setup.

  2. Separate view models will allow you more freedom to change and create new viewmodels to suit your views. It also helps preventing late loading proxies etc.

Many people would also have a Dto set of objects. These come in useful as a set of objects for caching and also if you have more than one UI, say a windows service as well.

Automapper is very popular to ease the pain of having so many models to map.

Mccallion answered 20/11, 2012 at 9:6 Comment(4)
Thank you. That makes perfect sense. BTW, If I am having a facade layer between my presentation layer and business layer, should I add another set of DTOs or can the view model act as the DTO to pass data between presentation layer and business layer? (though this requires me to add my view models to the business layer)Bullington
you wouldn't use your viewmodels as DTOs. If you need Dtos, create them, but only if you need them.Mccallion
Since I am having a facade layer, I thought I should make sure that all calls will go via facade layer only. in that case I think I will end up creating lots of DTOs as well. But, again - is that ok?Bullington
that's fine, not unusual to have the number of dtos greater than number of domain models but less than the number of your view modelsMccallion
C
3

It is good when you have differences between models on every level. If all you do with this models is put data from one layer model to another layer model without any transformation and processing, you can remove redundant models. In common cases domain model becomes redundant.

You should create separated viewmodel and database model due to necessity to have a possibility to recreate database model from database without changing of views.

Cirsoid answered 20/11, 2012 at 9:7 Comment(3)
+1, that's the right philosophy. only do it when you need it.Surround
Thanks. But I might need business models as well to pass data from Presentation layer to the business layer and later to map to the data model. Am I missing something here?Bullington
As far as i understand, no, you are not.Cirsoid

© 2022 - 2024 — McMap. All rights reserved.