tags:

views:

293

answers:

1

How would you structure the code for calling a wcf service in silverlight application?

Using only-once instanciated wcf service-proxy (aka singleton) and using it across the whole SL app? If so, how did you solve the unsubscribing controls from ws-call-completed event?

or

creating the wcf service-proxy for each ws-call? Where do you close the proxy then?

+2  A: 

Here's the application structure I found workable:

  • Application is split into modules (Prism but can be anything) - module per vertical function.
  • Every module has its own set of service client classes (generated by slsvcutil)
  • For every service client partial class I have another generated partial class where for every service method I have a version that returns IObservable.

    E.g. if my service client has a method GetAllMyDataAsync() and event GetAllMyDataCompleted the generated method signature will be IObservable<MyDataDto[]> GetMyData() This method will deal with subscribing/unsubscribing to an event, authentication, error handling, and other infrastructure issues.

    This way web-service call becomes simply:

    new MyServiceClient().GetAllMyData().Subscribe(DoSomethingWithAllMyData)

    With this I can easily join data from multiple requests, e.g. (strictly for demonstration purposes, don't try this in real app):

    var des = (from d in new MyServiceClient().GetMyDataItem()
               from e in new MyServiceClient().GetDataItemEnrichment(d)  
               select new EnrichedData { Data = d, Enrichment = e});  
    des.Subscribe(DoSomethingWithEnrichedData);
    
  • Once application gets more complex (e.g. data is shared by multiple components, you add messaging that dynamically updates initially retrieved data, etc.) it's helpful to add another element in the stack - Model.

    Therefore if I have a service MyDataService I'd have a model class called MyDataServiceModel. It will be registered in the container as singleton and it will be injected into viewmodels that needs it. Thus viewmodels talk to this class when they need data (so rather than calling MyServiceClient.GetAllMyData it would call MyDataServiceModel.GetAllMyData.

    This way viewmodels are completely independent of WCF stack (easier to mock, easier to test) Additionally these model classes take care of:

    • data transformation from/to DTO
    • enriching and combining data (one model method may join data from more than one request)
    • handling issues like throttling (e.g. typical scenario, user selected something in a combobox, it caused a request to be sent to a server to retrieve a data for that selection, while that request is being exected user made another change, but for some reason responses came out of order), etc.
    • combining data pulled on initial load via WCF with data pushed by the service during the session
PL