heltonbiker
heltonbiker

Reputation: 27605

How should a ViewModel interact with a Repository?

I am learning to build an application using MVVM, and I have the following situation:

Now my question is: should the Repository BE the collection to be databound to the view, or should it only be the "datasource" to a Property implementing INPC? For example, from the following possibilities, is one of them correct and the other wrong, or both wrong...?

// example where the list is replaced in order to be changed;

// ViewModel class (part of it)
public class ThisViewModel : ViewModelBase {
    public List<Examination> ExaminationList {
        get { return _examinationList; }
        set { _examinationList = value;
              NotifyOfPropertyChange("ExaminationList"); }
    }
}


var repo = new ExaminationRepository();
ThisViewModel.ExaminationList = repo.getAll().where(ex => ex.Value > 20).ToList();

Second option

// Example where the very property IS a repository

// ViewModel class (part of it)
public class ThisViewModel : ViewModelBase {

    // the List is actually a repository in disguise.
    IEnumerable _examinationList = new ExaminationRepository();

    public List<Examination> ExaminationList {
        get { return _examinationList; }
        set { _examinationList = value;
              // This should be "NotifyOfCollectionChange", I guess...
              NotifyOfPropertyChange("ExaminationList"); }
    }
}

Most probably I am quite confused/mistaken here, but my application is rather small and simple architecture-wise, and I really don't plan to use most frameworks and advanced concepts I have seen associated with this kind of problem (ORM, IoC, DI), instead I am mostly concerned to "how to properly handle a mutable, reppository-based collection in a WPF/MVVM Databinding environment".

EDIT: some context about my application: The application is one that performs clinical examinations. It has a list of Patients, where each Patient has its Examinations. There is a Patient repository and an Examinations repository. When I select a Patient in the PatientList, the ExaminationsList of that patient displays the matching Examinations from the Examinations repo. User actions on the Patients and Examinations are CRUD, or most specifically BREAD (Browse, Read, Edit, Add and Delete).

Thanks for any advice!

Upvotes: 1

Views: 2211

Answers (1)

Sheridan
Sheridan

Reputation: 69987

In my opinion, for best separation in large applications, you should always use separate collections to fill from the database and then to display in the UI. This does mean that you'll have to iterate through the collection twice, but this is often required in WPF anyway, as we like to add in extra display properties for the UI, such as IsSelected or IsFocused (if we are filling view model objects).

The main reason for this really though is to allow us to interface our various layers in the application so that we can test each layer independently. If you have no requirement to do this, then maybe it's not so important.

I've always wondered why Microsoft implement the INotifyPropertyChanged and INotifyPropertyChanging interfaces in their auto generated classes (from Linq2SQL for instance) when using these classes in the UI directly breaks the separation between layers that they themselves are always telling developers to adhere to.

UPDATE >>>

I would say that for a small application, it really doesn't matter how and where you do these things. Sometimes when writing a really small application, I don't even bother using MVVM. Creating layers and/or implementing an MVVM folder structure all add additional development time and extra code and I feel that they are only useful if you;

a) will be testing the individual layers of your application separately, or

b) might be likely at some stage in the future to need to 'swap out' one or more of the layers, eg. to move from a SQL database to an Oracle database for instance, or to move from a WPF UI to a web-based UI.

Upvotes: 2

Related Questions