using System;
namespace Wayne.Lib.AsyncManager
{
///
/// An Async Operation stores the necessary data for one asynchronous call. It has a
/// type parameter that represents what type of identifier the operation has, i.e. the type of
/// sequence number, token etc.
///
public abstract class AsyncOperation
{
#region Fields
#endregion
#region Events
///
/// Internal event that is fired when the operation is completed. This will enable the AsyncManager to remove
/// the operation from the list of outstanding operations.
///
internal event EventHandler OnOperationCompleted;
#endregion
#region Construction
///
/// Protected constructor of the AsyncOperation
///
/// The owner that will stand as sender when calling the delegate at completion.
/// Owner, that will stand as sender in the completion callback invocation.
/// User token to be supplied in the callback invocation.
/// User defined data.
internal protected AsyncOperation(object owner, TOperationId id, object userToken, object data):this(owner, id, userToken, data, ServiceContainerFactory.Create())
{
}
///
/// Protected constructor of the AsyncOperation
///
/// The owner that will stand as sender when calling the delegate at completion.
/// Owner, that will stand as sender in the completion callback invocation.
/// User token to be supplied in the callback invocation.
/// User defined data.
///
internal protected AsyncOperation(object owner, TOperationId id, object userToken, object data, IServiceLocator serviceLocator)
{
ISystemTime systemTime = serviceLocator.GetServiceOrDefault(() => new WayneSystemTime());
CreationDateTime = systemTime.Now;
this.Owner = owner;
this.Id = id;
this.UserToken = userToken;
this.Data = data;
}
#endregion
#region Properties
///
/// The owner of the operation. It will be set as the sender when calling the delegate at completion
///
public object Owner { get; private set; }
///
/// The identification for the operation.
///
public TOperationId Id { get; private set; }
///
/// The user token that identifies the operation.
///
public object UserToken { get; private set; }
///
/// Creation date time that enables a cleaning of the outstanding operation list.
///
internal DateTime CreationDateTime { get; set; }
///
/// User-defined data property that can be used to store application data with the operation.
///
public object Data { get; set; }
internal abstract Type ResultEventArgsType
{
get;
}
#endregion
#region Methods
///
/// Internal method to be called by descendant classes when the operation is completed and should be removed from the outstanding operations.
///
internal protected void Complete()
{
if (OnOperationCompleted != null)
OnOperationCompleted(this, EventArgs.Empty);
}
///
/// Removes the operation from the Async Manager lists, but does not call the completion delegate.
///
public void Cancel()
{
Complete();
}
#endregion
}
}