Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MVVM in large applications - sharing viewmodels, models, states etc

Tags:

c#

.net

mvvm

I was wondering if any of you guys know some good tutorial explaining MVVM for large applications. Every tutorial on MVVM i found are just basics explained (how to implement model, viewmodel and view), but I'm curious on techniques and patterns when it comes to passing data between application pages, where to init viewmodels, where to store common application data (shared between multiple viewmodels) etc.

I prefer pure c#/XAML solutions without any MVVM frameworks (MVVMLight etc).

like image 875
user969153 Avatar asked Sep 14 '12 10:09

user969153


2 Answers

techniques and patterns when it comes to passing data between application pages

If the data passing is event-driven (i.e: you want to notify when you receive data pushed from a server), use weak events. Prism's EventAggregator is one of many implementations.

If the data passing is constant, let the DataContext flow down the XAML tree and simply use binding.
That means if you have a "master" UserControl that uses other, more specific, UserControls, don't set their DataContext, they will inherit the master's and all is well.
If you want to separate functionality, use composition in the master's viewmodel.

where to init viewmodels

Purists will tell you to instantiate them directly in XAML. I almost never do that.
First of all, ViewModels usually need services, and they're passed as constructor parameters, or injected via MEF in the constructor... etc. So you can't have a parameterless constructor, or it's harder.

Now, we can do hard stuff, but what's the gain? It's perfectly fine to let the code-behind of the view retrieve the ViewModel, or get it injected in its constructor.

where to store common application data (shared between multiple viewmodels)

If the data is global: use static classes/properties,
in XAML you can access them via {x:Static my:StaticClass.StaticProperty}, if you want to reach a nested class/enum, use + instead of .

If the data is only shared between several ViewModels but not all, use inheritance/composition on the viewmodels, along with a controller pattern.

Other tips gathered from experience:

  • Only bind strictly what you need: don't bind to an entire object when you just need a string in there.

  • If you're binding the same thing again and again, don't hesitate to make custom markup by inheriting from Binding/MultiBinding. It's DRY 101, but most people just copy/paste Bindings for some reason.

Example:

public class VisibilityBinding : MultiBinding, IMultiValueConverter
{
   public VisibilityBinding()
   {
      var isSomething = new Binding("IsSomething");
      isSomething.ElementName = myUsrCtrl;
      this.Bindings.Add(isSomething);

      //Add more bindings

      this.Converter = this;
    }

    //Implement IMultiValueConverter to compute a System.Visibility from the bound values.
}

And in XAML:

 <TextBox Visibility={customMrkup:VisibilityBinding} />
  • Avoid at all cost any data duplication, all the way down to the DAL. It becomes complex really fast to synchronize fields that could just be deduced through an I(Multi)ValueConverter.
    Example: You have an audio file. It has a start time, an end time, and a duration. Get rid of any of these 3 properties, the third can be deduced.

If I think of any more tips I'll come back and add them.

Hope this helps!

like image 54
Louis Kottmann Avatar answered Nov 08 '22 22:11

Louis Kottmann


You might want to have a look at this book: Building Enterprise Applications with Windows Presentation Foundation and the Model View ViewModel Pattern (for example at Amazon.com).

It approaches MVVM in a more general context, not just a 5-minute code snippet, and even gives sugeestions at how you could organize a business solution.

We often used it as a reference at work. Personally speaking, I wish it would have gone a little bit deeper into details. But still, is a nice read to get into MVVM from a line-of-business application perspective.

Description text:

Create rich, flexible, and maintainable line-of-business applications with the MVVM design pattern

Simplify and improve business application development by applying the MVVM pattern to Windows Presentation Foundation (WPF) and Microsoft® Silverlight® 4. With this hands-on guide, you'll use MVVM with data binding, commands, and behaviors to create user interfaces loosely coupled to business logic. MVVM is ideal for .NET developers working with WPF and Silverlight—whether or not you have experience building enterprise applications.

Discover how to:

  • Dive deep into MVVM—and learn how it differs from other UI design patterns Build a simple Customer Relationship Management application you can adapt for your own projects
  • Implement MVVM to maintain separation between UI declarative syntax and presentation logic code
  • Create a Domain Model to define your application’s business context
  • Write dynamic code for the data access layer with the Microsoft Entity Framework and NHibernate
  • Enforce complex data-validation scenarios using Windows Workflow Foundation 4
  • Implement MVVM using frameworks and toolkits such as Microsoft Prism
like image 41
Jens H Avatar answered Nov 08 '22 21:11

Jens H