Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Action Filters for converting IDomainResult to IActionResult #73

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions samples/WebApi/Controllers/GlobalFilterResponsesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Tasks;

using DomainResults.Common;
using DomainResults.Examples.Domain;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DomainResults.Examples.WebApi.Controllers;

[ApiController]
[Route("[controller]")]
public class GlobalFilterResponsesController : ControllerBase
{
private readonly DomainSuccessService _service = new();

[HttpGet("[action]")]
public IDomainResult<int> Get200OkWithNumber()
=> _service.GetSuccessWithNumericValue();

[HttpGet("[action]")]
public Task<IDomainResult<int>> Get200OkWithNumberTask()
=> _service.GetSuccessWithNumericValueTask();

[HttpGet("[action]")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public Task<IDomainResult> Get204NoContentTask()
=> _service.GetSuccessTask();
}
5 changes: 4 additions & 1 deletion samples/WebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DomainResults.Examples.WebApi.Configuration;
using DomainResults.Mvc.IResult;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -19,7 +21,8 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<IActionFilter, ResultFilter>();
services.AddControllers(options => options.Filters.AddService<IActionFilter>());
services.AddAndConfigureSwagger();
}

Expand Down
37 changes: 37 additions & 0 deletions src/Mvc/IResult/ResultFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;

using DomainResults.Common;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace DomainResults.Mvc.IResult;

public class ResultFilter : IActionFilter, IEndpointFilter

Check failure on line 11 in src/Mvc/IResult/ResultFilter.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IEndpointFilter' could not be found (are you missing a using directive or an assembly reference?)
{
/// <summary>
/// Called before the action executes, after model binding is complete.
/// </summary>
/// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext" />.</param>
public void OnActionExecuting(ActionExecutingContext context) {}

/// <summary>
/// Called after the action executes, before the action result.
/// </summary>
/// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext" />.</param>
public void OnActionExecuted(ActionExecutedContext context)
{
var actionResult = (context.Result as ObjectResult)?.Value;
if (actionResult is IDomainResult result)
context.Result = result.ToActionResult();
// else (actionResult is IDomainResult<> resultGeneric)
// context.Result = resultGeneric.ToActionResult();
}

public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)

Check failure on line 32 in src/Mvc/IResult/ResultFilter.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'EndpointFilterInvocationContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 32 in src/Mvc/IResult/ResultFilter.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'EndpointFilterDelegate' could not be found (are you missing a using directive or an assembly reference?)
{
// See context here
return await next(context);
}
}
Loading