This question has been around my head for the past weeks or months, and I really don't know what the best solution is.
Using the MVVM patter, we use View Models to display data in a view. For example, if I want to show the product information to the user, I will create some propositions inside the view model and fill them. Then, through the bindings, the view will be able to receive data from these properties. Something like that:
<StackPanel> <TextBlock Text="Prodcut Name:" FontWeight="Bold" /> <TextBlock Text="{Binding Path=ProductName}" /> <TextBlock Text="Price:" FontWeight="Bold"/> <TextBlock Text="{Binding Path=Price}"/> <TextBlock Text="Added Date:" FontWeight="Bold" /> <TextBlock Text="{Binding Path=Date}"/> </StackPanel>
In the view model, I will get the data that I want to display. I will receive this data as a DTO product that will have the necessary properties in the view.
this.productDTO = getData();
So my question is: can we bind directy from the view model to dto? View Model:
private ProductDTO product; public string ProductName { get { return this.product.Name; } set { this.product.Name = value; } } public string Price { get { return this.product.Price; } set { this.product.Price = value; } }
I have an idea that exposing the DTO is not very good. but if it saves me from having to map all the properties from the DTO to the view model.
source share