Last active
January 2, 2016 17:59
-
-
Save msarchet/8339999 to your computer and use it in GitHub Desktop.
Generic Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Services.Core.HttpActionResult | |
{ | |
using System.Web.Http; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
public abstract class BaseActionResult : IHttpActionResult | |
{ | |
protected readonly HttpRequestMessage request; | |
protected readonly Task task; | |
protected BaseActionResult(HttpRequestMessage request, Task task) | |
{ | |
this.request = request; | |
this.task = task; | |
} | |
public abstract Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Services.Core.HttpActionResult | |
{ | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
public abstract class BaseActionResult<T> : BaseActionResult | |
{ | |
protected readonly Task<T> task; | |
protected BaseActionResult(HttpRequestMessage request, Task<T> task) | |
: base(request, task) | |
{ | |
this.task = task; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Services.Core.HttpActionResult | |
{ | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
/// <summary> | |
/// The patch result. | |
/// </summary> | |
public class PatchResult : BaseActionResult | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="PatchResult"/> class. | |
/// </summary> | |
/// <param name="request"> | |
/// The request. | |
/// </param> | |
/// <param name="task"> | |
/// The task. | |
/// </param> | |
public PatchResult(HttpRequestMessage request, Task task) | |
: base(request, task) | |
{ | |
} | |
/// <summary> | |
/// The execute async. | |
/// </summary> | |
/// <param name="cancellationToken"> | |
/// The cancellation token. | |
/// </param> | |
/// <returns> | |
/// The <see cref="Task"/>. | |
/// </returns> | |
public override async Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken) | |
{ | |
await this.task; | |
return request.CreateResponse(HttpStatusCode.NoContent); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Services.Core.HttpActionResult | |
{ | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
/// <summary> | |
/// The patch result. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// Result of the Patch | |
/// </typeparam> | |
public class PatchResult<T> : BaseActionResult<T> | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="PatchResult{T}"/> class. | |
/// </summary> | |
/// <param name="request"> | |
/// The request. | |
/// </param> | |
/// <param name="task"> | |
/// The task. | |
/// </param> | |
public PatchResult(HttpRequestMessage request, Task<T> task) | |
: base(request, task) | |
{ | |
} | |
/// <summary> | |
/// The execute async. | |
/// </summary> | |
/// <param name="cancellationToken"> | |
/// The cancellation token. | |
/// </param> | |
/// <returns> | |
/// The <see cref="Task"/>. | |
/// </returns> | |
public override async Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken) | |
{ | |
var result = await task; | |
return !result.Equals(default(T)) | |
? request.CreateResponse(HttpStatusCode.OK, result) | |
: request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment