CustomReferenceFromRouteBinder<TReference,TStore> Class
HAKOM.Persistence.Http.Net.Api.Serialization
Section titled “HAKOM.Persistence.Http.Net.Api.Serialization”Base class for model binders that bind route/path parameters to complex types implementing IReference, i.e. objects that refer to items stored in a repository.
public abstract class CustomReferenceFromRouteBinder<TReference,TStore> : HAKOM.Serialization.Http.Net.Api.CustomModelFromRouteBinder<TReference> where TReference : HAKOM.Persistence.Core.Net.Api.References.IReferenceType parameters
Section titled “Type parameters”TReference
The specific implementation type the binder can handle.
TStore
The store used to look up the reference.
Inheritance System.Object 🡒 HAKOM.Serialization.Http.Net.Api.CustomModelFromRouteBinder<TReference> 🡒 CustomReferenceFromRouteBinder<TReference,TStore>
Remarks
Section titled “Remarks”Instead of having every controller action parse string parameters from path parameters that should resolve to an item stored in the database, a custom model binder can be used to do this in one central place.
Deriving from this abstract class means less boiler-plate code has to be written in the specific binder. This also ensures that error handling, etc. is done in a consistent way.
Methods
Section titled “Methods”CustomReferenceFromRouteBinder<TReference,TStore>.BindModel(ModelBindingContext, CancellationToken) Method
Section titled “CustomReferenceFromRouteBinder<TReference,TStore>.BindModel(ModelBindingContext, CancellationToken) Method”Virtual implementation of the actual binding.
protected override System.Threading.Tasks.Task BindModel(Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext bindingContext, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));Parameters
Section titled “Parameters”bindingContext Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
The context in which the model is being bound, i.e. the current request and information about it and its parameters.
cancellationToken System.Threading.CancellationToken
A token that may be used by the method to cancel the ongoing operation. This should be used to prevent unneccessary work when the underlying request has been aborted for some reason.
Returns
Section titled “Returns”System.Threading.Tasks.Task
A task that completes when binding has completed. This does not indicate success. Instead, the Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.Result property of bindingContext will be set accordingly.
Remarks
Section titled “Remarks”This method takes care of calling the other abstract methods and producing appropriate (and consistent) error handling accross implementations.
Implementations should probably not override this.
CustomReferenceFromRouteBinder<TReference,TStore>.GetRequiredStore(ModelBindingContext, CancellationToken) Method
Section titled “CustomReferenceFromRouteBinder<TReference,TStore>.GetRequiredStore(ModelBindingContext, CancellationToken) Method”Override this and return an instance of TStore in the result.
protected abstract System.Threading.Tasks.Task<HAKOM.ErrorHandling.Core.Net.Api.Result<TStore>> GetRequiredStore(Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext bindingContext, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));Parameters
Section titled “Parameters”bindingContext Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
The context in which the model is being bound, i.e. the current request and information about it and its parameters.
cancellationToken System.Threading.CancellationToken
A token that may be used by the method to cancel the ongoing operation. This should be used to prevent unneccessary work when the underlying request has been aborted for some reason.
Returns
Section titled “Returns”System.Threading.Tasks.Task<HAKOM.ErrorHandling.Core.Net.Api.Result<TStore>>
A task that completes with an instance of Result<TValue> which indicates either successful initialization of the store, or a failure that occurred when initialization was attempted.
Remarks
Section titled “Remarks”When the implementation returns successfully, the store will be passed to either TryCreateReferenceFromId(string, TStore, ModelBindingContext, CancellationToken) or TryCreateReferenceFromName(string, TStore, ModelBindingContext, CancellationToken) depending on the format of the path value (i.e. depending if it is prefixed with “id:”).
When the implementation returns with a Failure<TValue>, the request will be terminated with an appropriate error message.
CustomReferenceFromRouteBinder<TReference,TStore>.TryCreateReferenceFromId(string, TStore, ModelBindingContext, CancellationToken) Method
Section titled “CustomReferenceFromRouteBinder<TReference,TStore>.TryCreateReferenceFromId(string, TStore, ModelBindingContext, CancellationToken) Method”Override this and return the instance of TReference that should be bound to the parameter value.
protected abstract System.Threading.Tasks.Task<HAKOM.ErrorHandling.Core.Net.Api.Result<TReference>> TryCreateReferenceFromId(string id, TStore store, Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext bindingContext, System.Threading.CancellationToken cancellationToken);Parameters
Section titled “Parameters”The identifier of the item within the store.
store TStore
The store in which the item resides.
bindingContext Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
The context in which the model is being bound, i.e. the current request and information about it and its parameters.
cancellationToken System.Threading.CancellationToken
A token that may be used by the method to cancel the ongoing operation. This should be used to prevent unneccessary work when the underlying request has been aborted for some reason.
Returns
Section titled “Returns”System.Threading.Tasks.Task<HAKOM.ErrorHandling.Core.Net.Api.Result<TReference>>
A task that completes with an instance of Result<TValue> which indicates either successful creation of the reference, or a failure that occurred when creation was attempted.
Remarks
Section titled “Remarks”It is up to the implementation to decide whether or not to return an already resolved reference, allowing the implementation to defer error handling to the controller action, for example if the store does not contain an item with that identifier. This is to reduce overhead in actions that do not actually require the item to be loaded.
CustomReferenceFromRouteBinder<TReference,TStore>.TryCreateReferenceFromName(string, TStore, ModelBindingContext, CancellationToken) Method
Section titled “CustomReferenceFromRouteBinder<TReference,TStore>.TryCreateReferenceFromName(string, TStore, ModelBindingContext, CancellationToken) Method”Override this and return the instance of TReference that should be bound to the parameter value.
protected abstract System.Threading.Tasks.Task<HAKOM.ErrorHandling.Core.Net.Api.Result<TReference>> TryCreateReferenceFromName(string name, TStore store, Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext bindingContext, System.Threading.CancellationToken cancellationToken);Parameters
Section titled “Parameters”name System.String
The name the item is identified by within the store.
store TStore
The store in which the item resides.
bindingContext Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
The context in which the model is being bound, i.e. the current request and information about it and its parameters.
cancellationToken System.Threading.CancellationToken
A token that may be used by the method to cancel the ongoing operation. This should be used to prevent unneccessary work when the underlying request has been aborted for some reason.
Returns
Section titled “Returns”System.Threading.Tasks.Task<HAKOM.ErrorHandling.Core.Net.Api.Result<TReference>>
A task that completes with an instance of Result<TValue> which indicates either successful creation of the reference, or a failure that occurred when creation was attempted.
Remarks
Section titled “Remarks”It is up to the implementation to decide whether or not to return an already resolved reference, allowing the implementation to defer error handling to the controller action, for example if the store does not contain an item with that name. This is to reduce overhead in actions that do not actually require the item to be loaded.