Skip to content

Instantly share code, notes, and snippets.

@ducmeit1
Created July 15, 2019 08:16
Show Gist options
  • Save ducmeit1/7147725783a32db9fb946368b2380c61 to your computer and use it in GitHub Desktop.
Save ducmeit1/7147725783a32db9fb946368b2380c61 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Customer.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
public class ApiControllerBase : ControllerBase
{
private readonly IMediator _mediator;
public ApiControllerBase(IMediator mediator)
{
_mediator = mediator ?? throw new ArgumentNullException();
}
protected async Task<TResult> QueryAsync<TResult>(IRequest<TResult> query)
{
return await _mediator.Send(query);
}
protected ActionResult<T> Single<T>(T data)
{
if (data == null) return NotFound();
return Ok(data);
}
protected async Task<TResult> CommandAsync<TResult>(IRequest<TResult> command)
{
return await _mediator.Send(command);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment