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.
I’m not aware of a tool that will do this for you, but there’s an easy way to wrap them in
Taskobjects. This is easier at least than manually definingAsyncmethods andeventcallbacks.The general concept is to run the method as a
Taskand 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):
then when you want to call a method and handle its return value:
It’s not quite as pretty as your suggested syntax, but it’s usable. The task continuation passed to
ContinueWithwill run on the UI thread, so it is safe to update the UI or any databound objects.Taskobjects 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
T4template to generate extension methods for you (e.g.,LoginTask(string username, string password, Action<Task<User>> continuation)).