You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Result<int, string>.Ok(99)); // useful if creating the value is expensive
201
200
```
202
201
202
+
### Result Errors
203
+
203
204
Since error messages are frequently represented as keyed string collections, the `ResultErrors` type is provided to simplify Result creation. The flexible constructor allows errors to be initialized with a single string, a collection of strings, or a key-value pair.
204
205
205
206
```csharp
@@ -315,7 +316,7 @@ Since Danom introduces types that are most commonly found in your model and busi
315
316
316
317
### Fluent Validation Integration
317
318
318
-
Danom is integrated with [Fluent Validation](https://fluentvalidation.net/) to provide a seamless way to validate your models and return a `Result` or `ResultOption` with the validation errors.
319
+
[Fluent Validation](https://fluentvalidation.net/) is an excellent library for building validation rules for your models. A first-class integration is available via [Danom.Validation](src/Danom.Validation/README.md) to provide a seamless way to validate your models and return a `Result` or `ResultOption` with the validation errors.
319
320
320
321
A quick example:
321
322
@@ -353,9 +354,7 @@ Documentation can be found [here](src/Danom.Validation/README.md).
353
354
354
355
### ASP.NET Core MVC Integration
355
356
356
-
Danom is integrated with [ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-5.0) to provide a set of utilities to help integrate the Danom library with common tasks in ASP.NET Core MVC applications.
357
-
358
-
Documentation can be found [here](src/Danom.Mvc/README.md).
357
+
Danom is integrated with ASP.NET Core via [Danom.Mvc](src/Danom.Mvc/README.md). This library provides a set of utilities to help integrate the core types with common tasks in ASP.NET Core MVC applications.
Danom.Mvc is a library that provides a set of rendering utilities to help integrate the [Danom](../../README.md) library with common tasks in ASP.NET Core MVC applications.
5
+
Danom.Mvc is a library that provides a set of utilities to help integrate the [Danom](../../README.md) library with common tasks in ASP.NET Core MVC applications.
6
6
7
7
## Getting Started
8
8
@@ -17,7 +17,20 @@ Or using the dotnet CLI
17
17
dotnet add package Danom.Mvc
18
18
```
19
19
20
-
## Working With `Option`
20
+
## Controller
21
+
22
+
The `DanomController` class extends the base controller class to provide a set of methods to help work with `Result`, `Option`, and `ResultOption` types in ASP.NET Core MVC applications.
23
+
24
+
25
+
### Option
26
+
27
+
The `ViewOption` method is used to render a view based on the presence of an `Option` value.
28
+
29
+
If the `Option` is `Some`, the view is rendered with the value. If the `Option` is `None`, the `noneAction` is invoked. By default, the `noneAction` returns a `NotFound` result.
30
+
31
+
A custom view name can be provided to render a view with a different name than the action.
32
+
33
+
Some examples demonstrating the use of `ViewOption` are shown below:
21
34
22
35
```csharp
23
36
usingDanom.Mvc;
@@ -26,29 +39,32 @@ using Microsoft.AspNetCore.Mvc;
The `ViewResult` method is used to render a view based on the presence of a `Result` value.
64
+
65
+
By default the `ViewResult` method will render the view with the value if the `Result` is `Ok`. If the `Result` is `Error`, the `errorAction` is invoked.
66
+
67
+
A custom view name can be provided to render a view with a different name than the action.
52
68
53
69
```csharp
54
70
usingDanom.Mvc;
@@ -57,58 +73,96 @@ using Microsoft.AspNetCore.Mvc;
Built into Danom is the `ResultErrors` type, which is particularly well suited for reporting model errors in ASP.NET Core MVC applications. The `ViewResultErrors` method, provided by the `DanomController` class, is a proxy for the `View` method that will inject the `ResultErrors` value into the model state.
91
+
92
+
When using `ResultErrors` as the error type, the `ViewResultErrors` will default the `errorAction` to inject the `ResultErrors` value into the model state.
While not explicitly part of the `Danom.Mvc` library, there are some patterns that make rendering the `Option` type easier in Razor views. Two methods from the base library are especially valuable: `TryGet` and `ToString`.
125
+
126
+
The `TryGet` method is used to extract the value from an `Option` type. If the `Option` is `Some`, the value is assigned to the `out` parameter and the method returns `true`. If the `Option` is `None`, the method returns `false`.
127
+
128
+
The custom `ToString` method is used to convert the `Option` value to a string. If the `Option` is `Some`, the value is converted to a string. If the `Option` is `None`, the method returns the default value. The method optionally accepts a second parameter for the format string.
129
+
130
+
Consider the following type:
131
+
132
+
```csharp
133
+
publicrecordPerson(
134
+
stringName,
135
+
Option<DateOnly> Birthdate,
136
+
Option<string> Email);
137
+
```
138
+
139
+
The `TryGet` and `ToString` methods can be used in a Razor view to help render the optional properties.
140
+
141
+
```cshtml
142
+
@model Person
143
+
144
+
<h1>@Model.Name</h1>
145
+
<h2>Email: <i>@Model.Email.ToString("-")</i></h2>
146
+
147
+
@if (Model.Birthdate.TryGet(out var birthdate))
148
+
{
149
+
var now = DateTime.Now;
150
+
var a = (now.Year * 100 + now.Month) * 100 + now.Day;
151
+
var b = (birthdate.Year * 100 + birthdate.Month) * 100 + birthdate.Day;
152
+
var age = (a - b) / 10000;
153
+
154
+
<p>You are born on @birthdate, and are @age years old.</p>
155
+
}
156
+
else
157
+
{
158
+
<p>You are an ageless wonder!</p>
113
159
}
114
160
```
161
+
162
+
## Find a bug?
163
+
164
+
There's an [issue](https://github.com/pimbrouwers/Danom/issues) for that.
165
+
166
+
## License
167
+
168
+
Built with ♥ by [Pim Brouwers](https://github.com/pimbrouwers) in Toronto, ON. Licensed under [Apache License 2.0](https://github.com/pimbrouwers/Danom/blob/master/LICENSE).
0 commit comments