Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

mvvm - What exactly are "WPF services"?

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"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...