Silverlight EAP vs APM to WCF Services - silverlight

I am building a Silverlight application that uses Web Services.
I have an ServiceAgent calls that is used to hide the proxy implementation from my ViewModels.
My question is which Asynchronous programming model is best used for Silverlight applications? That is, should I use Event base Async pattern (EAP) OR Async Programming Model (APM)?
Please let me know which has worked best for you in the past.

The rule of thumb is to implement the EAP when it is being used by UI code and the APM for back-end or library services.
I guess in your case, having events raised on the UI thread would be useful, so I would choose the EAP.
You might also want to consider using the new TAP: Task-based Asynchronous Pattern. This would make it easier to port to C#5 async when it is released later this year.
UPDATE:
I should have recommended using TaskFactory.FromAsync since you are wrapping an existing APM.

Related

Lightswitch evaluation and questions

There seems to have a buzz around the Lightswitch application framework.
Reading some posts/forums/articles and listening to a video provdided some answers, but raised some others.
Extensibility?
Is there several hooks to override or extend features?
Modularity? Can MEF/Prism be integrated? Is there a concept of dynamic modules that can be loaded at runtime?
Localizable? Any special features about localization in different languages?
Versionnable? Any concept of version so simultaneous version can co-exist?
Abstraction? The connection to data source can be abstracted and replaced?
Testability? Are the different components unit testable in an automated way? Easy or difficult to achieve?
Cross-cutting / NFR ? Any hooks so we can plug NFR code? (logging, caching, audit, secuity )
Is this a solid framework to build some PAAS application over?
Let me give you my 2 cents based on an eval I just did:
Extensibility
You can of course add custom controls and apparently there will be a published extensability API.
Modularity
No. Lightswitch is not a code generator, it abstracts out implementation of the UI.
Localizable
Did not see anything, but I imagine this will occur in stages.
Versionnable?
You could TFS the Lightswitch project, but if you mean versioning from an execution standpoint ... it's as simple as deploying a new version of the app to the server. The client will check for updates on run and install as necessary from what I understand.
Abstraction
Yes, if you abstract it at the WCF RIA Services layer which is very easy to do.
Testability
Business rules can be implemented at the RIA Services layer so testing there should be a snap, but if you rely heavily on rules driven through the UI you will have to use another UI-based testing tool.
Cross-cutting / NFR
If you focus on getting the RIA Services tier of the app to be the central location for business rules and let the UI just focus on user experience you won't run into much overlap. I think Lightswitch is a good client for many solutions, but the core power of it is using it as a client for RIA Services which itself is not dependent on Lightswitch by any stretch.
I hope I at least provided some useful insight.
Extensibility? When the built-in LS controls don't allow you to do what you need, you can use just about any Silverlight control (including 3rd party controls), or create your own custom Silverlight controls. There is a little more work to do than using LS controls, because the LS controls have some extra "smarts" built into them, so you can just drop them on a screen, bind, & use them. There are also extensibility points built into the framework that allows you to create your own "extensions" (shells, controls, themes etc).
Modularity? LS already uses MEF under the covers, so it's quite possible that it may be able to be leveraged further in the future. LS does generate code, but for itself, not for external consumption. There is an excellent article, however, by Microsoft's Eric Erhardt
http://blogs.msdn.com/b/lightswitch/archive/2011/04/08/how-do-i-display-a-chart-built-on-aggregated-data-eric-erhardt.aspx
that shows how to reuse the data model that LS constructs (in Eric's case, he uses it to create a RIA service that aggregates data for presentation in a chart control).
Localizable? I don't believe there are currently any built-in localisation features.
Versionable? For a web-based app, if the data schema didn't change, I don't see why you couldn't publish separate versions, but desktop (OOB) apps are click-once apps & are therefore governed by that technology.
Abstraction? If you were to create a RIA service for your data source, you could abstract away the implementation details.
Testability? Unfortunately, V1 was not written with "testability" in mind. If you extract out your business logic into Silverlight class libraries (for use on the client, or full .NET class libraries for use on the server), you can unit test that logic, but the code that you write in an LS application itself is not really unit-testable at this point in time.

WCF RIA Services - switching to Local Store

I have a Silverlight application that uses WCF RIA Services. I'd like to be able to switch the datasource to a local store (for example, SQLite) for demo or UI testing purposes.
At which layer should I refactor my code to allow to do this? Is there a way to do this without repeating functionality already present in WCF RIA Services classes (DomainClient, Entity, etc.), or should I create the switch much higher in the abstraction and bypass all of these classes altogether?
Thanks.
The DomainClient is the hook the framework provided for implementing this kind of switch. However, I don't think I've heard of anybody taking that approach. Instead, the split is often made at the Service layer of applications designed using the MVVM pattern (discussed in this video http://channel9.msdn.com/Series/Silverlight-Firestarter/Silverlight-Firestarter-2010-Session-4-MVVM-Why-and-How-Tips-and-Patterns-using-MVVM-and-Service-Pat).
Also, you might find this thread interesting as it discusses some existing patterns that have been used to persist data to the Silverlight isolated store.
http://forums.silverlight.net/forums/p/219768/524983.aspx#524983

silverlight / wpf two way communication with server side app

I have business logic in one application, let's call it "server". I want to "observe" what's going on in this application using a silverlight or WPF app.
So I've figured, I will use WCF services that expose current state to my clients, and I will poll for updates on a regular basis.
It's working well, but I need some kind of two way communication, because I need to notify my clients about changes.
Is there any other way than polling?
For Silverlight, there is a PollingDuplex mode, which still results in polling, but has a somewhat easier API to work with than dealing with the polling yourself.
See this article on MSDN : http://msdn.microsoft.com/en-us/library/cc645027(VS.95).aspx
For WPF, I believe there are more efficient ways to do this, but I'm not as familiar with the full WCF stack.
http://msdn.microsoft.com/en-us/library/ms731064.aspx

WPF MVVM - Correct way to consume a web service asynchronously

I have implemented MVVM in my WPF application. I was wondering what is the correct way to consume a Web Service from WPF MVVM app.
So far, I have created a DataAccess interface and a class that implements this interface. This would serve as a facade / proxy to the web service. The ViewModel gets a reference to this class as the constructor parameter so it can make any calls to get/set data, in this case it will be a Web service call. I would appreciate if you can guide me with some hints and / or direct me to some online resource.
I know about 3 different ways of writting asynchronous code for MVVM:
the classical using callbacks
using coroutines:
project Caliburn
Using C# 2.0 iterators to simplify writing asynchronous code
many more..
using Rx "Reactive Extensions" (this the newest):
The Joy of Rx: Building an Asynchronous API with Rx and IQueryable
Using Reactive Extensions in Silverlight
We aware that except the "classical" solution all the others use advanced techniques.

Options for Async Silverlight-to-WPF communication?

What's the fastest communication model for a Silverlight component communicating with a WPF component? That is, at the very least I'd like to consume an event from a Silverlight component in a WPF component.
I understand you can use WCF to build a bridge
I understand you can use Javascript to bridge from WPF -> Silverlight (and I have that working)
I understand you can use COM to go the other way (Silveright -> WPF)
However I'm looking for a tighter communication model (not using COM) - perhaps like EventAggregator (Silverlight component pubs, WPF component subs)...but I've only seen eventAggregation in WPF or Silverlight but not both at the same time.
Any ideas for creating such an eventAgg / eventBus, without using WCF, without using COM?
Open to any approaches / ideas; might be something I haven't considered.
Thanks.
Interesting challenge - could you provide a little more detail on why you need to do this?
My first question is: Are you envisioning these two apps running on the same machine, at the same time? Both are important.
If the answer is yes (same machine, both running) then you should be able to implement a solution using WCF, with the service self-hosted in the WPF app, the Silverlight client referencing it, and a cross-domain policy setup appropriately.
If the answer is yes, but not at the same time, then you'll need a third party, like a Message Bus, perhaps implemented as a Windows Service. NServiceBus comes to mind.
In any case, WCF really is the best solution for messaging in Silverlight. If you were going Silverlight app-to Silverlight app there is a mechanism for that that doesn't require WCF.
A message bus would give you the Event Aggregator functionality you're thinking of - a player who is always there to accept subscriptions and publications.
Another option (if running at the same time) is to elevate your Silverlight app to full-trust and use Sockets. See this link for more info (he provides an example that may be helpful).

Resources