2
votes

I am working on a Prism desktop application and would like to know the best way to deal with lookup / reference data lists when using a WCF backend. I think this question may cover a few areas and I would appreciate some guidance

For example, consider a lookup that contains Products(codes and descriptions) which would be used in a lot of different input screens in the system.

  1. Does the viewmodel call the WCF service directly to obtain the data to fill the control?
  2. Would you create a control that solely deals with Products with its own viewmodel etc and then use that in every place that needs a product lookup or would you re-implements say a combobox that repopulates the products ItemsSource in every single form view model that uses it?
  3. Would I create a brand new WCF service called something like LookupData service and use that to populate my lookup lists? - I am concerned I will end up with lots of lookups if I do this.
  4. What other approaches are there for going about this?
1

1 Answers

2
votes

I suggest creating your lookup object/component as a proxy object for WCF service. It can work in several ways, but most simple coming to my mind would be:

  1. Implement WCF service with methods to provide all Products entities and requested one (eg. basing on product code)
  2. Implement component that will use WCF client to get products, let's call it ProductsProvider
  3. Your view models will take dependency on ProductsProvider (eg. via constructor injection)

Key element in this model is ProductsProvider - it will work as kind of cache for Products objects. First, it will ask web service for all products (or some part of it, up to your liking) to start with. Then, whenever you need to lookup product, you ask provider - it's provider's responsibility to deal with how product should be looked up - maybe it's already in local list? Maybe it will need to call web service for update? Example:

public class ProductsProvider
{
    private IList<Product> products;
    private IProductsService serviceClient;

    public ProductsProvider(IProductsService serviceClient)
    {
        this.serviceClient = serviceClient;
        this.products = serviceClient.GetAllProducts();
    }

    public Product LookUpProduct(string code)
    {
        // 1: check if our local list contains product with given code
        // 2: if it does not, call this.serviceClient.LookUpProduct
        // 3: if service also doesn't know such product:
        //    throw, return null, report error
    }
}

Now, what this gives you is:

  • you only need to have one ProductsProvider instance
  • better flexibility with when and how your service is called
  • your view models won't have to deal with WCF at all

Edit:

As for your second question. Control may not be needed, but having view model for Product entity is definitely a good idea.