Class Mapper<TRequest, TResponse, TEntity>
use this base class to define domain entity mappers for your endpoints.
HINT: entity mappers are used as singletons for performance reasons. do not maintain state in the mappers.
Inherited Members
Namespace: FastEndpoints
Assembly: FastEndpoints.dll
Syntax
public abstract class Mapper<TRequest, TResponse, TEntity> : IRequestMapper<TRequest, TEntity>, IRequestMapper, IResponseMapper<TResponse, TEntity>, IResponseMapper, IMapper, IServiceResolverBase where TRequest : notnull where TResponse : notnull
Type Parameters
| Name | Description |
|---|---|
| TRequest | the type of request dto |
| TResponse | the type of response dto |
| TEntity | the type of domain entity to map to/from |
Methods
CreateScope()
if you'd like to resolve scoped or transient services from the MS DI container, obtain a service scope from this method and dispose the scope when the work is complete.
<pre><code class="lang-csharp">using var scope = CreateScope();
var scopedService = scope.Resolve<MyService>();</code></pre>
Declaration
public IServiceScope CreateScope()
Returns
| Type | Description |
|---|---|
| IServiceScope |
FromEntity(TEntity)
implement this method and place the logic for mapping a domain entity to a response dto
Declaration
public virtual TResponse FromEntity(TEntity e)
Parameters
| Type | Name | Description |
|---|---|---|
| TEntity | e | the domain entity to map from |
Returns
| Type | Description |
|---|---|
| TResponse |
FromEntityAsync(TEntity, CancellationToken)
implement this method and place the logic for mapping a domain entity to a response dto
Declaration
public virtual Task<TResponse> FromEntityAsync(TEntity e, CancellationToken ct)
Parameters
| Type | Name | Description |
|---|---|---|
| TEntity | e | the domain entity to map from |
| CancellationToken | ct | a cancellation token |
Returns
| Type | Description |
|---|---|
| Task<TResponse> |
Resolve(Type)
resolve an instance for the given type from the dependency injection container. will throw if unresolvable.
Declaration
public object Resolve(Type typeOfService)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | typeOfService | the type of the service to resolve |
Returns
| Type | Description |
|---|---|
| object |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if requested service cannot be resolved |
Resolve(Type, string)
resolve an instance for the given type from the dependency injection container. will throw if unresolvable.
Declaration
public object Resolve(Type typeOfService, string keyName)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | typeOfService | the type of the service to resolve |
| string | keyName | the key name for resolving keyed service |
Returns
| Type | Description |
|---|---|
| object |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if requested service cannot be resolved |
Resolve<TService>()
resolve an instance for the given type from the dependency injection container. will throw if unresolvable.
Declaration
public TService Resolve<TService>() where TService : class
Returns
| Type | Description |
|---|---|
| TService |
Type Parameters
| Name | Description |
|---|---|
| TService | the type of the service to resolve |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if requested service cannot be resolved |
Resolve<TService>(string)
resolve an instance for the given type from the dependency injection container. will throw if unresolvable.
Declaration
public TService Resolve<TService>(string keyName) where TService : class
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyName | the key name for resolving keyed service |
Returns
| Type | Description |
|---|---|
| TService |
Type Parameters
| Name | Description |
|---|---|
| TService | the type of the service to resolve |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if requested service cannot be resolved |
ToEntity(TRequest)
implement this method and place the logic for mapping the request dto to the desired domain entity
Declaration
public virtual TEntity ToEntity(TRequest r)
Parameters
| Type | Name | Description |
|---|---|---|
| TRequest | r | the request dto |
Returns
| Type | Description |
|---|---|
| TEntity |
ToEntityAsync(TRequest, CancellationToken)
implement this method and place the logic for mapping the request dto to the desired domain entity
Declaration
public virtual Task<TEntity> ToEntityAsync(TRequest r, CancellationToken ct)
Parameters
| Type | Name | Description |
|---|---|---|
| TRequest | r | the request dto to map from |
| CancellationToken | ct | a cancellation token |
Returns
| Type | Description |
|---|---|
| Task<TEntity> |
TryResolve(Type)
try to resolve an instance for the given type from the dependency injection container. will return null if unresolvable.
Declaration
public object? TryResolve(Type typeOfService)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | typeOfService | the type of the service to resolve |
Returns
| Type | Description |
|---|---|
| object |
TryResolve(Type, string)
try to resolve an instance for the given type from the dependency injection container. will return null if unresolvable.
Declaration
public object? TryResolve(Type typeOfService, string keyName)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | typeOfService | the type of the service to resolve |
| string | keyName | the key name for resolving keyed service |
Returns
| Type | Description |
|---|---|
| object |
TryResolve<TService>()
try to resolve an instance for the given type from the dependency injection container. will return null if unresolvable.
Declaration
public TService? TryResolve<TService>() where TService : class
Returns
| Type | Description |
|---|---|
| TService |
Type Parameters
| Name | Description |
|---|---|
| TService | the type of the service to resolve |
TryResolve<TService>(string)
try to resolve an instance for the given type from the dependency injection container. will return null if unresolvable.
Declaration
public TService? TryResolve<TService>(string keyName) where TService : class
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyName | the key name for resolving keyed service |
Returns
| Type | Description |
|---|---|
| TService |
Type Parameters
| Name | Description |
|---|---|
| TService | the type of the service to resolve |
UpdateEntity(TRequest, TEntity)
implement this method and place the logic for mapping the updated request dto to the desired domain entity
Declaration
public virtual TEntity UpdateEntity(TRequest r, TEntity e)
Parameters
| Type | Name | Description |
|---|---|---|
| TRequest | r | the request dto to update from |
| TEntity | e | the domain entity to update |
Returns
| Type | Description |
|---|---|
| TEntity |
UpdateEntityAsync(TRequest, TEntity, CancellationToken)
implement this method and place the logic for mapping the updated request dto to the desired domain entity
Declaration
public virtual Task<TEntity> UpdateEntityAsync(TRequest r, TEntity e, CancellationToken ct)
Parameters
| Type | Name | Description |
|---|---|---|
| TRequest | r | the request dto to update from |
| TEntity | e | the domain entity to update |
| CancellationToken | ct | a cancellation token |
Returns
| Type | Description |
|---|---|
| Task<TEntity> |