4

I'm new to MVVM, I get the general idea, but there's one thing that I've been having a tough time finding an answer for. Inevitably an application will have a save button in the UI. The UI talks to the View Model, the view model updates the model objects, and then the model saves to whatever persistence medium (DB, File System, Web Service...).

My question is how does this last step happen? Does the View Model get an instance of a DAL object?

public MyVm(IMyModel obj, IDal<IMyModel> dal){ ... }

Or does IMyModel have CRUD methods as part of its interface.

Is it the third option, which is me getting this completely wrong?

1 Answer 1

2

This is a simple explanation with code sample of how it is done in C#, persistence medium is the context object. View model is used mainly to update some properties of the model object, then during "SaveOrUpdate" operation persistence medium should check wether the model has been modified somewhere else and wether the model state is valid (foreign key constraints, value constraints and so on). If there are none - the storage gets updated

4
  • Thank you. I'll be using a interface instead of direct;y passing the db context object as I will be using multiple persistence methods, based on setup and environment, but this give me what I need. I'll pass my persistence interface to my view models and update data that way This was what I was leaning towards, but wasn't completely certain if this was right.
    – Jesse
    Commented May 26, 2017 at 15:34
  • @Jesse wouldn't it be a good idea to separate models from logic? Your models are simple POCO-objects and separate layer of services/repositories is responsible for CRUD operations? Commented May 26, 2017 at 15:51
  • They are separate. I was just saying that the CRUD objects will implement an interface, because based on configuration implementation will differ, but the interface will remain the same.
    – Jesse
    Commented May 26, 2017 at 18:41
  • @papadoble151 No, separating logic from business models is a terrible idea; its the anemic domain model antipattern.
    – Andy
    Commented Jul 10, 2018 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.