- Notifications
You must be signed in to change notification settings - Fork112
A result abstraction that can be mapped to HTTP response codes if needed.
License
ardalis/Result
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A result abstraction that can be mapped to HTTP response codes if needed.
Docs are located in the /docs folder and available online atresult.ardalis.com Please add issues for new docs requests andpull requests for docs issues are welcome!
- Getting Started With Ardalis.Result
- Transforming Results With the Map Method
- Avoid Using Exceptions to Determine API Status Codes and Responses
- Fluent Validation in MediatR with Results
Many methods on service need to return some kind of value. For instance, they may be looking up some data and returning a set of results or a single object. They might be creating something, persisting it, and then returning it. Typically, such methods are implemented like this:
publicCustomerGetCustomer(intcustomerId){// more logicreturncustomer;}publicCustomerCreateCustomer(stringfirstName,stringlastName){// more logicreturncustomer;}
This works great as long as we're only concerned with the happy path. But what happens if there are multiple failure modes, not all of which make sense to be handled by exceptions?
- What happens if customerId is not found?
- What happens if required
lastName
is not provided? - What happens if the current user doesn't have permission to create new customers?
The standard way to address these concerns is with exceptions. Maybe you throw a different exception for each different failure mode, and the calling code is then required to have multiple catch blocks designed for each type of failure. This makes life painful for the consumer, and results in a lot of exceptions for things that aren't necessarilyexceptional. Like this:
[HttpGet]publicasyncTask<ActionResult<CustomerDTO>>GetCustomer(intcustomerId){try{varcustomer=_repository.GetById(customerId);varcustomerDTO=CustomerDTO.MapFrom(customer);returnOk(customerDTO);}catch(NullReferenceExceptionex){returnNotFound();}catch(Exceptionex){returnnewStatusCodeResult(StatusCodes.Status500InternalServerError);}}
Another approach is to return aTuple
of the expected result along with other things, like a status code and additional failure mode metadata. While tuples can be great for individual, flexible responses, they're not as good for having a single, standard, reusable approach to a problem.
The Result pattern provides a standard, reusable way to return both success as well as multiple kinds of non-success responses from .NET services in a way that can easily be mapped to API response types. Although theArdalis.Result package has no dependencies on ASP.NET Core and can be used from any .NET Core application, theArdalis.Result.AspNetCore companion package includes resources to enhance the use of this pattern within ASP.NET Core web API applications.
Thesample folder includes some examples of how to use the project. Here are a couple of simple uses.
Imagine the snippet below is defined in a domain service that retrieves WeatherForecasts. When compared to the approach described above, this approach uses a result to handle common failure scenarios like missing data denoted as NotFound and or input validation errors denoted as Invalid. If execution is successful, the result will contain the random data generated by the final return statement.
publicResult<IEnumerable<WeatherForecast>>GetForecast(ForecastRequestDtomodel){if(model.PostalCode=="NotFound")returnResult<IEnumerable<WeatherForecast>>.NotFound();// validate modelif(model.PostalCode.Length>10){returnResult<IEnumerable<WeatherForecast>>.Invalid(newList<ValidationError>{newValidationError{Identifier=nameof(model.PostalCode),ErrorMessage="PostalCode cannot exceed 10 characters."}});}varrng=newRandom();returnnewResult<IEnumerable<WeatherForecast>>(Enumerable.Range(1,5).Select(index=>newWeatherForecast{Date=DateTime.Now.AddDays(index),TemperatureC=rng.Next(-20,55),Summary=Summaries[rng.Next(Summaries.Length)]}).ToArray());}
Continuing with the domain service example from the previous section, it's important to show that the domain service doesn't know aboutActionResult
or other MVC/etc types. But since it is using aResult<T>
abstraction, it can return results that are easily mapped to HTTP status codes. Note that the method above returns aResult<IEnumerable<WeatherForecast>
but in some cases, it might need to return anInvalid
result, or aNotFound
result. Otherwise, it returns aSuccess
result with the actual returned value (just like an API would return an HTTP 200 and the actual result of the API call).
You can apply the[TranslateResultToActionResult]
attribute to anAPI Endpoint (or controller action if you still use those things) and it will automatically translate theResult<T>
return type of the method to anActionResult<T>
appropriately based on the Result type.
[TranslateResultToActionResult][HttpPost("Create")]publicResult<IEnumerable<WeatherForecast>>CreateForecast([FromBody]ForecastRequestDtomodel){return_weatherService.GetForecast(model);}
Alternatively, you can use theToActionResult
helper method within an endpoint to achieve the same thing:
[HttpPost("/Forecast/New")]publicoverrideActionResult<IEnumerable<WeatherForecast>>Handle(ForecastRequestDtorequest){returnthis.ToActionResult(_weatherService.GetForecast(request));// alternately// return _weatherService.GetForecast(request).ToActionResult(this);}
Similar to how theToActionResult
extension method translatesArdalis.Results
toActionResults
, theToMinimalApiResult
translates results to the newMicrosoft.AspNetCore.Http.Results
IResult
types in .NET 6+. The following code snippet demonstrates how one might use the domain service that returns aResult<IEnumerable<WeatherForecast>>
and convert to aMicrosoft.AspNetCore.Http.Results
instance.
app.MapPost("/Forecast/New",(ForecastRequestDtorequest,WeatherServiceweatherService)=>{returnweatherService.GetForecast(request).ToMinimalApiResult();}).WithName("NewWeatherForecast");
The full Minimal API sample can be found in thesample folder.
A common use case is to map between domain entities to API response types usually represented as DTOs. You can map a result containing a domain entity to a Result containing a DTO by using theMap
method. The following example calls the method_weatherService.GetSingleForecast
which returns aResult<WeatherForecast>
which is then converted to aResult<WeatherForecastSummaryDto>
by the call toMap
. Then, the result is converted to anActionResult<WeatherForecastSummaryDto>
using theToActionResult
helper method.
[HttpPost("Summary")]publicActionResult<WeatherForecastSummaryDto>CreateSummaryForecast([FromBody]ForecastRequestDtomodel){return_weatherService.GetSingleForecast(model).Map(wf=>newWeatherForecastSummaryDto(wf.Date,wf.Summary)).ToActionResult(this);}
By default, Asp Net Core and API Explorer know nothing about[TranslateResultToActionResult]
and what it is doing. To reflect[TranslateResultToActionResult]
behavior in metadata generated by API Explorer (which is then used by tools like Swashbuckle, NSwag etc.), you can useResultConvention
:
services.AddControllers(mvcOptions=>mvcOptions.AddDefaultResultConvention());
This will add[ProducesResponseType]
for every knownResultStatus
to every endpoint marked with[TranslateResultToActionResult]
.To customize ResultConvention behavior, one may use theAddResultConvention
method:
services.AddControllers(mvcOptions=>mvcOptions.AddResultConvention(resultStatusMap=>resultStatusMap.AddDefaultMap()));
This code is functionally equivalent to the previous example.
From here you can modify the ResultStatus to HttpStatusCode mapping
services.AddControllers(mvcOptions=>mvcOptions.AddResultConvention(resultStatusMap=>resultStatusMap.AddDefaultMap().For(ResultStatus.Ok,HttpStatusCode.OK, resultStatusOptions=>resultStatusOptions.For("POST",HttpStatusCode.Created).For("DELETE",HttpStatusCode.NoContent)).For(ResultStatus.Error,HttpStatusCode.InternalServerError)));
ResultConvention
will add[ProducesResponseType]
for every result status configured inResultStatusMap
.AddDefaultMap()
maps every known ResultType, so if you want to exclude certain ResultType from being listed (e.g. your app doesn't have authentication and authorization, and you don't want 401 and 403 to be listed asSupportedResponseType
) you can do this:
services.AddControllers(mvcOptions=>mvcOptions.AddResultConvention(resultStatusMap=>resultStatusMap.AddDefaultMap().For(ResultStatus.Ok,HttpStatusCode.OK, resultStatusOptions=>resultStatusOptions.For("POST",HttpStatusCode.Created).For("DELETE",HttpStatusCode.NoContent)).Remove(ResultStatus.Forbidden).Remove(ResultStatus.Unauthorized)));
Alternatively, you can specify which (failure) result statuses are expected from a certain endpoint:
[TranslateResultToActionResult()][ExpectedFailures(ResultStatus.NotFound,ResultStatus.Invalid)][HttpDelete("Remove/{id}")]publicResultRemovePerson(intid){// Method logic}
!!!
If you list a certain Result status inExpectedFailures
, it must be configured inResultConvention
on startup.
Another configurable feature is what part of the Result object is returned in case of specific failure:
services.AddControllers(mvcOptions=>mvcOptions.AddResultConvention(resultStatusMap=>resultStatusMap.AddDefaultMap().For(ResultStatus.Error,HttpStatusCode.BadRequest, resultStatusOptions=>resultStatusOptions.With((ctrlr,result)=>string.Join("\r\n",result.ValidationErrors)))));
We can use Ardalis.Result.FluentValidation on a service with FluentValidation like that:
publicasyncTask<Result<BlogCategory>>UpdateAsync(BlogCategoryblogCategory){if(Guid.Empty==blogCategory.BlogCategoryId)returnResult<BlogCategory>.NotFound();varvalidator=newBlogCategoryValidator();varvalidation=awaitvalidator.ValidateAsync(blogCategory);if(!validation.IsValid){returnResult<BlogCategory>.Invalid(validation.AsErrors());}varitemToUpdate=(awaitGetByIdAsync(blogCategory.BlogCategoryId)).Value;if(itemToUpdate==null){returnResult<BlogCategory>.NotFound();}itemToUpdate.Update(blogCategory.Name,blogCategory.ParentId);returnResult<BlogCategory>.Success(await_blogCategoryRepository.UpdateAsync(itemToUpdate));}
If you're building an ASP.NET Core Web API you can simply install theArdalis.Result.AspNetCore package to get started. Then, apply the[TranslateResultToActionResult]
attribute to any actions or controllers that you want to automatically translate from Result types to ActionResult types.
About
A result abstraction that can be mapped to HTTP response codes if needed.