0

In the past, when I've used MVC, my model objects were just dumb data containers. Everywhere I read says that in MVVM, models should contain business logic as well as being a data container. Now that my model objects also contain business logic, how do I use them as data containers? For instance: my models as an accessor (an injected dependency). In the past, my accessors would construct a model and pass that as their result:

class foo_accessor { 
    foo_model Get(int id);
    ...
}

This creates a cyclical dependency though, as my accessor has to create an model object with another accessor. I'm considering creating a structure with my properties and instead passing that around:

class foo_model {
    struct container {
          int x;
    }
    
    private container _cont;
    private foo_accessor _accessor;

    public X { get => return _cont.x; set => _cont.x = value; }

    public GetModel(int id) {
          _cont = _accessor.Get(id);
    }
}

class foo_accessor { 
    ref container Get(int id);
    ...
}

But this seems like duplication and more importantly, violates that out models is actually doing the modeling. It's really just becoming a controller.

  • 1
    "Model" was a catch-all term for business logic / domain model even in MVC and MVP. They were never just data containers. In MVVM, you have that kind of model, and you also have the view model ("the model of the view", a.k.a. presentation model - which is an abstract representation of the view - a way to isolate presentation logic and make it testable). Within the view model (to which the view is data-bound) you might have properties that are simple data containers or methods that return data containers. – Filip Milovanović Nov 02 '22 at 18:22
  • 1
    So, in line with this thinking, you don't ask some object to "get the model", but you already have a reference to the model (which again, is some component from the business layer, or more broadly from an inner application layer), and you either *ask the model* to return some data that you'll take and display, or you pass a view-related callback (or a full-blown object) *to the model*, so that some logic within the domain layer can call it at an appropriate point in time. – Filip Milovanović Nov 02 '22 at 18:27

0 Answers0