Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObservableCollection and Repository pattern in MVVM - How to relate them, and a case for the "Update" method

My Desktop WPF application has a repository (of type Person) which resides in the Model layer, and is called by a ViewModel which has a PersonList property which is databound to a DataGrid.

When I open my Application, the list is shown by default. During initialization, the following code applies:

public class PersonScreenViewModel : ViewModelBase
{
    PersonRepository _person_repository;

    public ObservableCollection<Person> PersonList { get; set; }

    public PersonScreenViewModel() {

        _repositorio_pacientes = new RepositorioPacientes();

        PersonList = new ObservableCollection<Person>(_person_repository.GetAll());
    }

    // ... ///
}

My doubts are based on some difficulty to understand how to implement "Save / Update" method(s).

Every text about the Repository pattern tells that "the repository should behave like a collection of [ entities | domain objects ]". So, the most logical thing would be to databind the repository itself to the DataGrid. That could be done if I created a class which inherit from ObservableCollection and implements repository logic. Currently, what I do is take a copy of the repository items, using _repo.GetAll(), and work in that copy, having to commit back after my changes are done.

Specifically, my workflow involves selecting a row in the DataGrid, changing some properties of the selected object, and commiting these changes, expecting that the same object is persisted with the new property values.

Usually, if you take an object in a collection and modify it, you don't need to "update" anything, since you already changed the object itself.

The question is: "How can I data bind my view to the repository itself? Should the repo inherit from ObservableCollection? How is it supposed to be done? Is some part of my reasoning flawed?"

It is important to say that my application is not data-oriented, the Person class is very basic (POCO marked as [Serializable], with a few properties), and my repository uses plain XML serialization and the filesystem structure.

Thanks for reading!

like image 853
heltonbiker Avatar asked Oct 03 '22 16:10

heltonbiker


1 Answers

"Every text about the Repository pattern tells that "the repository should behave like a collection of [ entities | domain objects ".

What you originally have is sort of correct. First of all ObservableCollection works perfectly with the View because every add/update/delete will automatically propagate back to the user interface. If you implemented it with repository, then you would have to go call the database reload the control bound to your repository

"How can I data bind my view to the repository itself? Should the repo inherit from ObservableCollection? How is it supposed to be done? Is some part of my reasoning flawed?"

  • You don't want to data bind the Repository to the DataGrid itself, because you lose caching and every time the user loads the user control you always have a trip back to your database.
  • You don't want to inherit a repository to an ObservableCollection, ObservableCollection has more overhead vs simple IEnumerable given you already database returned you a collection already.
  • You always don't want ObservableCollection as your collection for your data because it is expensive. A simple enumerable of data is enough depending on your scenario (Read-only data).
like image 97
123 456 789 0 Avatar answered Oct 13 '22 11:10

123 456 789 0