tags:

views:

862

answers:

2

Someone told me in an answer to a stackoverflow question that the "two big guns" for the MVVM pattern are 1) attached behaviors and 2) services. I assume he means "WPF services" a phrase which I found used in the following ways:

PresentationFoundation.dll defines the WPF controls types, animation and multimedia support, data binding support, and other WPF services.

Many of these WPF services ( de-coupled eventing, rich databinding, styling, resources, etc.) are software development best practices that converge in a single, declarative UI stack.

You will understand the motivation behind WPF, learn the syntax of XAML, examine the core programming model, and survey several WPF services.

None of the WPF books I have even mention "WPF services" as such, so is this just a word that means "WPF features" such as decoupled eventing, rich databinding, styling, etc. or is there something else behind the term "WPF Services"?

+1  A: 

He's not talking about WPF services, but application services. That is, abstracting some portion of functionality into an independent service that multiple VMs can consume.

HTH, Kent

Kent Boogaart
What does that mean exactly, are you talking about a Windows Service, Web Service? The wikipedia article on WPF gives examples of "WPF application services" as "user interface, 2D and 3D drawing, fixed and adaptive documents, advanced typography, vector graphics, raster graphics, animation, data binding, audio and video". What would it mean that then that e.g. "multiple ViewModels can consume data binding". That's always the case. What is behind this word "WPF services" and "application services" in this sense? Or do you mean in the classic sense such as "security services"?
Edward Tanguay
By "service" I just mean an abstraction over a piece of functionality. Perhaps the underlying functionality is a COM component, or database access or whatever. Doesn't matter - the VM just knows about an interface that it can use to interact with said functionality.
Kent Boogaart
+2  A: 

Martin Fowler has a description of what a service is in his Dependency Injection article. Put simply, a service is an object that provides functionality to be used by other objects. You'll find the term used heavily when discussing the patterns Inversion of Control and Service Locator.

To make this concrete with the topic at hand, let's think about how we'd display a message box in the MVVM pattern. Calling MessageBox.Show() would be bad, Ray. This ties the ViewModel tightly to the UI architecture, and makes the ViewModel difficult to test. Instead, one solution would be to use a service, which we'll call IDisplayMessage. This service is supplied to the ViewModel somehow (through one of the two patterns above), and this service is used to "display" a message. During normal operation, a concrete version of this service will call MessageBox.Show(), but during testing we can provide a different concrete version (a test double) that behaves differently (a noop often, or if we're ensuring the ViewModel displays the message, a version that records the call so we can assert that it occurred). Onyx (disclaimer: I'm the author) provides just such a service, and the infrastructure necessary for providing this service (and others) to your ViewModel.

Update: Since this response was made, I've written a blog post Services: Your ViewModel Deathstar, which covers the topic fairly well. This was part of a "series" of posts, and readers may also be interested in the first post Behavior - Your Trusty ViewModel Bazooka.

wekempf
Why not point to your articles?Attached Behaviours: http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!940.entryServices: http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!951.entry
Trainee4Life
At the time, the articles didn't exist, and I just didn't think to come back here and update the response.
wekempf