-
Notifications
You must be signed in to change notification settings - Fork 408
Description
Proposal:
In order to homogenize deferrals, I suggest to create a new, distinct pair of interface IClaimDeferral and abstract class Deferral. (The names I use here are merely suggestions.)
Apply these to every class providing a GetDeferral() method instead of retaining the literally hundreds of strayed, distinct implementations for the same kind of functionality.
Summary
I propose to create a new interface IClaimDeferral and have it being implemented by all classes providing a GetDeferral() method. The return value should return an object deriving from an abstract base class Deferral:
interface IClaimDeferral
{
Deferral GetDeferral();
}
abstract class Deferral : IDisposable
{
public virtual void Complete()
{
...
}
public void Dispose() => Complete();
}Rationale
It's currently quite tedious and error-prone to encompass almost each and every awaiting method implementation with:
var deferral = e.GetDeferral();
...
deferral.Complete();I anticipate significant ease of implementation by replacing the current plethora of divergent GetDeferral() implementations and return values by using a vanilla, single, homogenous implementation that's utilizing the IDisposable pattern.
The most significant simplification I can think of right away would be to use a using declaration to have the deferral's Complete() method called automatically:
void My_Event(object sender, SomeEventArgs e)
{
using e.GetDeferral();
...
}
// having SomeEventArgs extend Deferral, like:
public class SomeEventArgs : Deferral
{
...
}Second Thought
I'm not sure, but is it truly necessary to bother programmers with deferrals anyway?
When the UWP runtime triggers an event, instead of using event arguments providing a GetDeferral() method, why doesn't the runtime itself simply claim a Deferral and close it afterwards?
var d = new Deferral();
await _myEvent?.Invoke(this, ea); // pseudo code, solely for illustration purposes
d.Close();