views:

108

answers:

1

Hello,

I am working on an WPF application that uses a BusinessLogic layer (currently a single dll) in which I created my BL methods that will be called directly from the UI. Each BL manager is resolved with Unity (thinking on switching to MEF though...). BL classes implements a specific interface that have of course apropriate methods.

Now, what I want is to create (or rather to GENERATE) a new asynchronous-aspect-like assembly (or more...) that should have similar methods/operations defined as in my original assembly (the same parameters...) and also a callback delegate as a parameter. So basically I want async methods to be generated with some framework out there...

Besides the usual call to: User userBO = Resolve().Login("name", "pass"); I'd like to use something similar with: Resolve().Login("name", "pass", delegate(object, SomeArgs e) { User userBO = e.Args....};

Now, I want this assembly to be generated instead of writing new eventArgs and delegates for each method. I am aware that PostSharp could help in AOP task, but I coulnd't find anything regarding this code generation mechanism in a new dll for asynchronous methods.

Is there a way to achieve this using a third party tool or do I have to rewrite the whole async thing manually?

Any ideas are welcome. Thank you.

+2  A: 

I'm not aware of a tool that will do this for you, but there's an easy way to wrap them in Task objects. This is easier at least than manually defining Async methods and event callbacks.

The general concept is to run the method as a Task and then schedule a task continuation to the UI thread.

First, define a scheduler (you don't need to do this every time; it could be a global var or a window-level var):

TaskScheduler ui = TaskScheduler.FromCurrentSynchronizationContext();

then when you want to call a method and handle its return value:

var bll = Resolve();
Task.Factory.StartNew(_ => bll.Login("name", "pass"))
  .ContinueWith(task =>
  {
    // Note: accessing Result will raise any exceptions thrown by Login
    User userBO = task.Result;
    ...
  }, ui);

It's not quite as pretty as your suggested syntax, but it's usable. The task continuation passed to ContinueWith will run on the UI thread, so it is safe to update the UI or any databound objects.

Task objects also fully support other common asynchronous scenarios, in particular cancellation and progress reporting.

Since this approach doesn't actually add events to the class, it should be possible to write a T4 template to generate extension methods for you (e.g., LoginTask(string username, string password, Action<Task<User>> continuation)).

Stephen Cleary
Looks interesting - how does this behave when the task is blocking? Does the UI block?
Adam
Thanks guys for the info, I'll need to digg into T4 templates. So far, I only used them in EF. I was thinking on asynchronous interfaces because of the synthactical sugar it provides:IUser blUser = Resolve().Login(...) and then IUserAsynchronous = Resolve().LoginAsync(...., callback)...
Sese
I believe I'm going to write this tool myself..It would be nice if this tool would create async methods whenever I create a new BL method for example and build the project. just thoughts...So that the output async dll would contain the appropriate async method as well....
Sese
Personally, I prefer task continuations. But if you like the `IAsyncInvoke` style, note that the `Task` class does in fact implement `IAsyncInvoke`. So you can return it from your `BeginLogin` method and pull the result out in your `EndLogin` method.
Stephen Cleary