MVVM ViewModel referencing DTO classes or any other class, is it correct?

JuanPabloSillerJuanPabloSiller USMember ✭✭
edited July 2016 in General

Hi,

I'm working in a Xamarin.Forms application, I'm using MVVM light. Each view has the bindingContext linked to the ViewModel.

Now in my ViewModel I have a List that it is binded to a ListView in the View.

Now, the DTOClass is in another file in another folder of the project. So, basically I'm not sure if this is valid or should I just add properties directly to the ViewModel or add my class inside my ViewModel class. Specially for the INotifyPropertyChange behavior, I'm not sure if the objects of the DTOClass could be notified.

So basically, is it correct to reference other classes directly from the ViewModel and if so how can I work with them using the INotifyPropertyChange, if not how can you have the same behavior directly inside my view model.

Here is an example of my current code, it works but I'm not sure if it is valid MVVM wise and if it will affect the INotifyPropertyChange for the elements inside the list.

public class EmployeeViewModel : ViewModelBase
    {
        List<Employee> employees = new List<Employee> ();
        public List<Employee> Employees { get { return employees; } }

    ...
    constructor and logic to add employees
    ...
}

So basically, should I make my List Raise a property change or somehow make the elements inside the list PropertyChanged? Or both? Is it possible even if the elements class is not inside the view model? Hope I making myself clear I just want to understand the MVVM pattern correctly.

Thanks
Regards

Best Answer

Answers

  • PubuduGayanPubuduGayan NZMember ✭✭✭

    Hi @JuanPabloSiller

    I strongly recommend do not use DTO objects in your modelview. Second yes you can use as a data binder. Third you need Observable Collection

    private ObservableCollection< Employee> _locations = new ObservableCollection< Employee >();
    public ObservableCollection< Employee > Locations
    {
    get { return _locations; }
    set { SetProperty(ref _locations, value); }
    }

    Notice I am using prism so the prism frame work know who change and to whom call. Its simple as mention above

  • JuanPabloSillerJuanPabloSiller USMember ✭✭

    Hi @PubuduGayan

    Thank you for the information. What really confuses me is then how would I implement my Employee class inside the EmployeeViewModel class in order to populate the list or what do you mean by not using DTOs? Where does the employee class need to exist?

    Thanks

  • PubuduGayanPubuduGayan NZMember ✭✭✭

    @JuanPabloSiller I mean do not use DTO classes as an entity in ViewModel classes.

    What I will do if I want to use the same class (Employee) will create a separate employee as model in mobile project PCL. Is it make any sense

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    I would go with EmployeeViewModel being a wrapper around the Employee class, with simple pass through properties.

    Then I would add a EmployeesViewModel that contains an ObservableCollection. This will notify whenever you add an employee to it.

    Be aware though that if you add hundreds of entries to the collection one by one the collection changed event will fire every time and cause the list to update - this may take your UI slow. In this case it's worth creating the collection first then assigning it to a property/raising a property changed

  • JuanPabloSillerJuanPabloSiller USMember ✭✭

    Hi @JimBennett

    By being a wrapper you mean literally creating the Employee class inside the EmployeeViewModel class? Basically what I'm having trouble understanding is how to have items inside the observable collection being notified too as well, so for instance, a new item is added to the collection, the list is notified, but what about if an item in the collection has changed and if this item represents a class, does that class need to be a view model itself or just a regular class with a INotifyPropertyChange?

    I really want to go in the right track so that's why so many questions, if it was possible would you mind putting a simple example representing your explanation.

    Thank you,
    Regards

  • JuanPabloSillerJuanPabloSiller USMember ✭✭

    Thank you for the explanation.

Sign In or Register to comment.