Product Entity object is passed to the View. I want to add some properties that mainly concerns only the View like
public string MiddleImage
{
get
{
return "M" + this.ImageUrl;
}
}
And now I can use this property in the View
<img src="<%=Model.MiddleImage%>" />
My question is where I should add this property, in the Product Entity object itself or it’s better to create another class that inherits from Product?
public class ProductWrapper : Product
{
public string MiddleImage
{
get
{
return "M" + this.ImageUrl;
}
}
}
I would create a
ProductViewModelclass that holds the properties that only your view cares about which of course can include any newly formed UI properties such as your image url.Then save yourself some leg work and use AutoMapper in your controller to map those properties from the entity to the View Model.
Assign the view model’s custom properties after the map.
Your
Productentity maps to your persistence store, yourProductViewModelmaps to your User Interface – exactly the separation of concerns MVC is trying to achieve.