Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectModelBinder<TSelf, TValue>

Namespace: AdCodicem.ValueObjects.AspNetCore.ModelBinding
Assembly: AdCodicem.ValueObjects.AspNetCore.dll

Binds a value object from the raw text of a route segment, query string value, header or form field.

public sealed class ValueObjectModelBinder<TSelf, TValue> : IModelBinder where TSelf : struct, IValueObject<TSelf, TValue>

Type Parameters​

TSelf

Value object type.

TValue

Underlying value type.

Inheritance​

object ← ValueObjectModelBinder<TSelf, TValue>

Implements​

IModelBinder

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

The binder is closed over the concrete value object type, so binding costs one TryParse and nothing else: no reflection, no TypeDescriptor lookup, and no exception on the rejection path. The violated rule travels with the model error so that can put its stable code in the response body.

Text that is empty or white space says no more than no text, as it does to MVC's own binder for every simple type but . An optional value object binds to null. One that cannot be null is refused as MVC refuses blank text for an , with the framework's "The value is invalid" message and , rather than reaching the action as the default instance no rule has checked.

A refusal carries the English message of the rule, unless the application configured its messages through AddValueObjects(messages => …): the message the resource holds under the key of the rule's code then takes its place, quoting the display name of the member as {0} and the arguments the type's rules supply from {1}. Blank text keeps the framework's message, which the application localises through MVC's own message provider.

Constructors​

ValueObjectModelBinder()​

Initializes a binder that answers a refusal with the English message of the rule.

public ValueObjectModelBinder()

Methods​

BindModelAsync(ModelBindingContext)​

Attempts to bind a model.

public Task BindModelAsync(ModelBindingContext bindingContext)

Parameters​

bindingContext ModelBindingContext

The .

Returns​

Task

A which will complete when the model binding process completes.

If model binding was successful, the should have set to true.

A model binder that completes successfully should set to a value returned from .