0

Assume I have a canvas that has rendered shapes. I can drag-move around that canvas as if I was in a zoomed in image to move all shapes around. Between the Model and ViewModel I have a ServiceLayer (or Controller, however you'd like to call that ) that generates shapes based on generic object-logic and applies the necessary styles, which it then feeds to the ViewModel. Although not completely traditional MVVM, it's the solution I came up with thinking about how to change the CanvasBehaviour at runtime without switching ViewModels. Now if I want to switch the underlying generic service (perhaps because another tool was selected) I need to save what the current center position is.

It seems like a nitpicky question, but I feel like since I cannot answer this fully and with determination, I might have misunderstood something about the architecture. Here's what I'm thinking:

  1. It should not be saved in the Model. The model is for all the underlying shapes etc. and has nothing to do with how they're displayed.
  2. It should not be in the ServiceObject, since it can change dynamically at runtime based on tools and it "feels" wrong to read out the previous center and feed it to a new ServiceObject before replacing it.
  3. It might belong in the ViewModel. the ViewModel prepares the data it receives into the necessary binded collections. I can keep a reference of the center here, and modify the incoming shapes etc. to translate them around this center point. However that means that the "move" tool now needs a reference towards this ViewModel, to tell it how it changed or similarily, but the tools are supposed to be contained in the ServiceObject entirely.
  4. It does definitely not belong in the view, I think.

1 Answer 1

0

You have a choice.

The idea of separating the View and ViewModel is to help you separate the presentation and 'business logic' responsibilities.

But things like "The button should be red when the user clicks it" or "Show shapes zoomed in based on the selected center point" don't fall clearly into one of the two camps.

You can argue that these things are pure presentation logic and the View should contain everything, or you can say 'I have business rules which depend on the center point! It should go in the ViewModel'

If you are using WPF you also have some practical considerations. It might just be easier or more performant to choose one location over the other.

1
  • Ah, so I also need to figure out how I handle the interaction logic - if they are all based on the shape being selected, then I can put the whole zoom logic in the View since the shapes have their respective calling functions, but if I instead use the canvas absolute mouse position and figure out the shape that was clicked on that way, I'd need to have business logic for that, which means it should be in the ViewModel!
    – Joe
    Apr 29, 2019 at 9:34

Your Answer

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

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