I'm very busy with the architecture of a new MVC application, but i'm getting very confused about how to manage different type of objects. The confusion is about the relation between entities, business objects and viewmodels. I 'm going to describe my confusion with an example:
I've set up my web application with different projects: MVC frontend, BLL, DAL, Common things etc.
Let's say i have a view with a List of bikes. I want to display the bike details like color, size, manufacturer. But in my database, the Bike and Manufacturer are two different tables, so in my Entity Framework context, these are also two different classes.
So i have these two entities Bike and Manufacturer. But in my business needs, i think they need to be a single object, which i can manipulate or use in business logic. Then there is my view, which needs a (View)Model. That should also be a combined ViewModel with properties from different tables.
How do i handle this? Do i need to get the Bike and Manufacturer object from my DAL, and create a business object from it in my BLL, and after doing some business logic, should i create a ViewModel from it in my controller? Or does my DAL need to return a combined business object? Or can i use the entity object as business classes? Or can i also use my business object as a ViewModel?
I hope that my problem is clear and that anyone can give me a good advise about which object are needed and how, where and when the different types of objects are created, and in which layer this classes should go...
Data Transfer Objects (DTOs) and View Models (VMs) are not the same concept! The main difference is that while VMs can encapsulate behaviour, DTOs do not. The purpose of a DTO is the transfer of data from one part of an application to another.
In ASP.NET MVC, ViewModel is a class that contains the fields which are represented in the strongly-typed view. It is used to pass data from controller to strongly-typed view.
The ViewModel is essential when you want a separation of concerns between your DomainModel (DataModel) and the rest of your code.
What ViewModel is. In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization.
The answer to your question is easy. There is NO relationship between your different layers of models. They are completely isolated, and do not refer to each other. Not at all. As such, there is nothing to be confused about.
You have code in different parts of your layer that maps between two layers UI->Business and Business->Data, but that should be the extent of any interaction between them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With