|
| 1 | +# Winton.DomainModelling.AspNetCore |
| 2 | + |
| 3 | +[](https://ci.appveyor.com/project/wintoncode/winton-domainmodelling-aspnetcore/branch/master) |
| 4 | +[](https://travis-ci.org/wintoncode/Winton.DomainModelling.AspNetCore) |
| 5 | +[](https://www.nuget.org/packages/Winton.DomainModelling.AspNetCore) |
| 6 | +[](https://www.nuget.org/packages/Winton.DomainModelling.AspNetCore) |
| 7 | + |
| 8 | +Conventions useful for creating an ASP.NET Core based REST API on top of a domain model. |
| 9 | + |
| 10 | +## Exception Filters |
| 11 | + |
| 12 | +### DomainExceptionFilter |
| 13 | + |
| 14 | +An [Exception Filter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filters.iexceptionfilter) for converting [supported Exceptions](https://github.com/wintoncode/Winton.DomainModelling.Abstractions#exceptions) to [IActionResult](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.iactionresult)s, automatically setting the type, status code, and message, as appropriate. The following conversions are performed by default: |
| 15 | + |
| 16 | +* Base [DomainException](https://github.com/wintoncode/Winton.DomainModelling.Abstractions#domainexception) to [BadRequest](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.badrequestobjectresult) (HTTP 400) |
| 17 | +* [UnauthorizedException](https://github.com/wintoncode/Winton.DomainModelling.Abstractions#unauthorizedexception) to [Unauthorized](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.unauthorizedresult) (HTTP 401) |
| 18 | +* [EntityNotFoundException](https://github.com/wintoncode/Winton.DomainModelling.Abstractions#entitynotfoundexception) to [NotFound](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.notfoundobjectresult) (HTTP 404) |
| 19 | + |
| 20 | +#### Usage |
| 21 | + |
| 22 | +The `DomainExceptionFilter` should be added to the collection of filters on the [MvcOptions](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions) configuration for your application. For example, if MVC Core is added to your service collection using a custom configurer |
| 23 | + |
| 24 | +```csharp |
| 25 | +services.AddMvcCore(options => options.ConfigureMvc()) |
| 26 | +``` |
| 27 | + |
| 28 | +then simply add the `DomainExceptionFilter` to the collection of filters |
| 29 | + |
| 30 | +```csharp |
| 31 | +internal static class MvcConfigurer |
| 32 | +{ |
| 33 | + public static void ConfigureMvc(this MvcOptions options) |
| 34 | + { |
| 35 | + ... |
| 36 | + options.Filters.Add(new DomainExceptionFilter()); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +#### Extensibility |
| 42 | + |
| 43 | +Since `DomainException` is extensible for any domain-specific error, `DomainExceptionFilter` can be extended to support custom Exception to Result mappings. Simply pass a `Func<DomainException, ErrorResponse, IActionResult>` to the constructor, such as |
| 44 | + |
| 45 | +```csharp |
| 46 | +new DomainExceptionFilter((exception, response) => exception is TeapotException ? new TeapotResult() : null) // 418 |
| 47 | +``` |
| 48 | + |
| 49 | +Note that all custom mappings are handled **after** `EntityNotFoundException`s and `UnauthorizedException`s. |
0 commit comments