MVVM Question when involving lists

Hello,

Question isn't realistically related to Xamarin Forms, but I am developing in Xamarin Forms. My question is more related to MVVM design principals, and there is something I can't quite understand properly - so wondering if I could get an answer from someone with more experience.

Take the following example where I am maintaining a list of Teams;

Is it best practice to do the following;

  • Create a model for Team, implementing the properties of Team
  • Create a ViewModel for Team, implementing Application logic for Team (e.g API access, datastore logic, and manipulation of fields)
  • Create a ViewModel for Teams - containing an ObservableCollection of TeamViewModel

Is this something to be avoided? E.g nesting ViewModels? Or is this "best practice". I can see the value to doing this but i don't know if its bad practice/or any negatives.

Or should I;

  • Create a model for Team
  • Create a ViewModel for Teams, containing an ObservableCollection of Team Model?

And yes, the result will be a View, which will have a BindingContext to one of the ObservableCollections.

Thanks for the help - I understand this might be basic to most people - and maybe i'm reading too much in to it.

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @CamMurray ,

    I suggest you to create individual projects for your Models/Services and so on.
    This way they will be de-coupled from each other and good for maintenance as well.

    Another thing you must take care of is using the right IOC and follow the same MVVM style as you would typically follow for projects in WPF or silverlight. There are couple of open-source frameworks and libraries out there which ease your task on this.

    My personal favorite is PRISM library which has tons of features and support wide variety of IOC containers including AutoFac and Unity (again unity is my favorite). Using this library takes most of the pain of implementing the bases of MVVM and you can definitely concentrate on other important areas of project.

    Hope I do not sound in rush rush here, may be you can google a bit about PRISM or just browse their home on below URL:

    Hope it helps.

    -- N Baua

  • CharwakaCharwaka INMember ✭✭✭

    Hi @CamMurray

    For Forms its Best to go with FreshMvvM , other than PRISM or MvvMCross Which simply bloats the app.
    FreshMvvM is very light weight and only fills gaps existing in Xamarin Forms and very easy to use.

    https://michaelridland.com/xamarin/freshmvvm-quick-start-guide/

    Create all viewmodels in PCL where your all Observablecollection and Properties goes, Write Services for Call, and database interaction, use constructor injection to resolve services and use them in Viewmodel to get data and fill your viewmodel .

    Further Reference

    https://forums.xamarin.com/discussion/60855/freshmvvm-vs-mvvm-light

  • BrianLagunasBrianLagunas USInsider ✭✭✭

    @Charwaka can you support your claim of how Prism bloats your app? Can you define "bloat" and how you feel Prism adds the "bloat"? What do you define as "light weight"? Is it solely based on assembly size? I am very interested in your perspective.

  • CharwakaCharwaka INMember ✭✭✭
    edited November 11

    @Charwaka said:
    Hi @CamMurray

    For Forms its Best to go with FreshMvvM , other than MvvMCross Which simply bloats the app.
    FreshMvvM is very light weight and only fills gaps existing in Xamarin Forms and very easy to use.

    https://michaelridland.com/xamarin/freshmvvm-quick-start-guide/

    Create all viewmodels in PCL where your all Observablecollection and Properties goes, Write Services for Call, and database interaction, use constructor injection to resolve services and use them in Viewmodel to get data and fill your viewmodel .

    Further Reference

    https://forums.xamarin.com/discussion/60855/freshmvvm-vs-mvvm-light

    Just edited answer but in case of MvvMcross due to large APK size

Sign In or Register to comment.