Skip to content

Commit ac267f3

Browse files
authored
Merge pull request #2 from wintoncode/readme
Added README
2 parents ae3798a + f6679ee commit ac267f3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Winton.DomainModelling.AspNetCore
2+
3+
[![Build status](https://ci.appveyor.com/api/projects/status/k94y5or6toq2un7d?svg=true)](https://ci.appveyor.com/project/wintoncode/winton-domainmodelling-aspnetcore/branch/master)
4+
[![Travis Build Status](https://travis-ci.org/wintoncode/Winton.DomainModelling.AspNetCore.svg?branch=master)](https://travis-ci.org/wintoncode/Winton.DomainModelling.AspNetCore)
5+
[![NuGet version](https://img.shields.io/nuget/v/Winton.DomainModelling.AspNetCore.svg)](https://www.nuget.org/packages/Winton.DomainModelling.AspNetCore)
6+
[![NuGet version](https://img.shields.io/nuget/vpre/Winton.DomainModelling.AspNetCore.svg)](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

Comments
 (0)