1

Within Exrin, using the normal hierarchy of ViewModel having access to the IModel, The Model having access to the IService, and Operations commonly having access to the IModel yet being called from the ViewModel, what is the proper use of methods and interactions within each scope?

For example, I need to clear a table in my SQLite database on navigating to a page. I have an IRepositoryService that contains all my methods for interacting with our database. My Model has a ClearUserInputTables method that calls a few different methods within the IRepositoryService.

I could either override the OnAppearing method (may likely change it to a different point in the life cycle) within the ViewModel to call the Model.ClearUserInputTables method or I could create an Operation that has access to the Model to do the same thing. Which is favored in Exrin?

Maybe I need a better understanding of the purpose of Operations. I read up on Operation Separation (basically used for navigation), but am unsure if it should be used for other things such as this (calling Model methods from the ViewModel).

Adam
  • 16,089
  • 6
  • 66
  • 109
Timothy James
  • 1,062
  • 13
  • 25

1 Answers1

1

Operation's are designed to take code outside of the ViewModel, so it can

  1. Easily be Unit Tested (also due to return of IResult)
  2. Timeout Logic Added
  3. Pushes to background thread
  4. Catches exceptions
  5. Ensures only 1 operation runs at a time. (e.g. stops rapid click's of button firing off the operation multiple times before the last one has finished)

Operation's however are completely optional. You don't have to use them.

If you want to clear a table when you navigate to a page, I could recommend

OnNavigated

This is only called once when the page first loads.

OnBackNavigated

Is called when the back is navigated to, via the page in front of it being popped. But

OnAppearing

Is called every time you see the page, regardless of how you got there. However if that's what you want, then it can be used for clearing your database tables.

Because an Operation is essentially part of the ViewModel, you can think of them at the same level as an operation. Hence

Operation -> Model -> Service -> Repository (ClearDB)

You just pass the model through, via the Operation constructor.

As a side note, I realize that Operations can be a lot of overhead. They are optional. If you don't really need the benefits, listed at the start of this answer, then you can just call the Model directly from OnAppearing, without using an Operation.

Adam
  • 16,089
  • 6
  • 66
  • 109