Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:45:02+00:00 2026-06-05T22:45:02+00:00

TL;DR: What is a good and testable way to implement the dependency between the

  • 0

TL;DR:
What is a good and testable way to implement the dependency between the ViewModels and the WCF services in a MVVM client?

Please read the rest of the question for more details about the problems I encountered while trying to do this:


I am working on a silverlight client that connects to a wcf service, and I want to write unit tests for the client.
So I’m looking for a good solution for using the wcf clients in my ViewModels and testing that interaction. I have found two solutions until now:

Solution 1: This is actually how I have implemented it until now:

public class ViewModelExample
{
    public ViewModelExample(IServiceClient client)
    {    
            client.DoWorkCompleted += .. 
            client.DoWorkAsync();
    }
}

//This is how the interface looks like
public interface IServiceClient
{
    event EventHandler<AsyncCompletedEventArgs> DoWorkCompleted;
    void DoWorkAsync();
}

//I was able to put the interface on the generated clients because they are partial classes, like this:
public partial class GeneratedServiceClient : IServiceClient
{

}

The good part: it’s relatively easy to mock

The bad part: my service client lives as long as my ViewModel, and when I have concurrent requests I don’t know which answer belongs to which request.

Solution 2: Inspired by this answer
WCF Service Client Lifetime.

public class ViewModelExample
{
    public ViewModelExample(IServiceFactory factory)
    {
        var client = factory.CreateClient();
        client.DoWorkCompleted += ...
        client.DoWorkAsync();
    }
}

The good part: each request is on a different client, so no more problems with matching requests with answers.

The bad part: it’s more difficult to test. I would have to write mocks for both the factory and the wcf client every time. This is not something I would like to do, since I alreay have 200 tests… 🙁

So my question is, how do you guys do it? How do your ViewModels talk to the wcf services, where do you inject the dependency, and how do you test that interaction?
I feel that I’m missing something..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:45:07+00:00Added an answer on June 5, 2026 at 10:45 pm

    Try having a Func<IServiceClient> injected into your VM instead of the a client instance; you’ll have a ‘language-level factory’ injected instead of building a class for this. In the factory method you can instantiate your client however you want (each access could create a new instance for that for example).

    The downside is that you’ll still have to touch your tests for the most part, but I assume it will be less work:

    public ViewModelExample(Func<IServiceClient> factoryMethod)
    {
        var client = factoryMethod();
        client.DoWorkCompleted += ...
        client.DoWorkAsync();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, just a quick question: I would like to bind a table to
whats a good way to test the settings (especially keys) in web.config? I think
Good Morning How to create a new plist in iphone? it is possible please
Good day, I've read a few other stack overflow postings and other tutorials, but
I'm trying to think of a good way to clean up my controllers to
Good morning, I have mysql queries where I would like to calculate percentage of
Good day. I need to teach Windows CryptoAPI to encrypt the message with private
Good evening guys. I'm currently trying to get started on development of a project
Good Day, Can someone confirm what was said at the bottom of this post
Good day, I have a class that implements the LoaderCallbacks, and hence have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.