The Model in MVVM architecture compared to MVC

Brent

I am trying to learn the MVVM pattern, and in particular what to do when a view represents a database table, but the view has a few elements representing a single database field. As a simple example:

Suppose I have a database field with a DateTime type (with a class having a property for each database field), but in the view, i have a datepicker for the date component and a TimeSpan for the time component.

In an MVC pattern, I would always have a POCO model which contained a nullable DateTime property for the datepicker and a nullable TimeSpan property for the time. I would map the database class across to the model.

In a MVVM pattern I can wrap the class representing the database fields, and have logic on the set accessors which keeps the wrapped class DateTime property in sync (and as the 2 fields are nullable, private DateTime and TimeSpan fields in the viewmodel, only setting the database class Datetime property if both date and time are not null)

Alternatively, I can have the viewmodel wrap a model much more like the MVC model - ie a POCO model with the date and time components representing seperate properties. The viewmodel then becomes a much simpler wrapper which implements INotifyPropertyChanged, along with a save ICommand which adds the date and timespan only as it creates or updates an instance of the database class.

Obviously both approaches "work", but what would be considered better practice and/or most adherent to MVVM architecture. Thank you.

Brent

I ended up mapping and splitting the date from the database model to a mapped model, as it was better for validation messages to the user (the model implemented IDataErrorInfo). as I had a DateTime which needed to be before now, I could split the validation in the model into:

If date after today's date -> return string "Can't use a date later than today"

else if date present but time missing -> return string "Please enter the time"

else if DateTime after now ->return string "Can't be later than the current date and time"

the second approach would involve sharing simple, related validation logic between 2 classes which seemed silly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the difference between MVI compared to MVC and MVVM

From Dev

The Model in MVVM architecture compared to MVC

From Dev

ASPNET MVC WebAPI architecture

From Dev

Combining MVVM and MVC in a picture

From Dev

What are the responsibilities of a model in MVVM?

From Dev

MVC Architecture on Grails

From Dev

architecture multilayered vs mvc

From Dev

MVVM Model with Entity Framework

From Dev

MVVM, ViewModel, Model & MessageBoxes

From Dev

MVVM – Correct implementation of the Model

From Dev

Mvvm model ViewModel

From Dev

MVC with Service architecture

From Dev

MVC architecture DTO/Model mapping/conversion

From Dev

What is the different between Model/Business Layer/Data Access and Repositories in the MVC architecture?

From Dev

MVVM architecture: one model - several view models + place for data access

From Dev

View and manipulate the model of a MVC web app through a MVVM client

From Dev

MVVM architecture in android Using Volley

From Dev

In a MVC architecture, can the `View` access the model?

From Dev

Nested model/view architecture

From Dev

Architecture ASP MVC

From Dev

architecture multilayered vs mvc

From Dev

MVC Architecture - Adding fields with custom logic to the Model

From Dev

WPF MVVM Change Model

From Dev

MVVM RaisePropertyChanged in ViewModel or Model?

From Dev

MVC model binder: Which <input> attribute is compared with input parameter of action method?

From Dev

MVVM model binding beginner

From Dev

Mvvm model validation with INotifyDataErrorInfo

From Dev

How to move logic to show UITableViewRowAction from delegate method to View Model (MVVM Architecture)

From Dev

PreferencesFragment and MVVM architecture

Related Related

HotTag

Archive