Shared : Application
The Shared.Application project provides the application layer abstractions and implementations for a CQRS-based architecture. It includes a lightweight in-process mediator with pipeline behaviors, domain event publishing, repository and service contracts, a Unit of Work pattern, and dependency injection helpers for automated registration.
Abstractions : Repositories
- ICommandRepository : A marker interface for repositories that handle write operations (create, update, delete).
- IQueryRepository : A marker interface for repositories that handle read operations (queries).
Abstractions : Services
- ICommandService : A marker interface for services that orchestrate command-side business logic.
- IQueryService : A marker interface for services that orchestrate query-side business logic.
Abstractions : Unit of Work
The IUnitOfWork interface defines a single SaveChangesAsync method that coordinates the persistence of changes across one or more repositories within a transactional boundary.
Data Providers
DataProvider static class provides centralized access to common data generation utilities :
- DateTimes.GetUtcNow : Returns the current UTC date and time.
- Guids.Create : Generates a new version 7 GUID (time-ordered, sortable).
Dependency Injection
ApplicationDependencyInjection static class provides helper methods to register all application layer services into the DI container :
- AddMediator : Registers the
IMediatorimplementation and the logging pipeline behavior. - AddCommandServices / AddQueryServices : Registers all concrete types implementing
ICommandServiceorIQueryServiceby scanning a given assembly. - AddCommandHandlers / AddQueryHandlers : Registers command and query handlers as
IRequestHandlerservices using generic type registration. - AddCommandHandlerProxies / AddQueryHandlerProxies : Registers handler proxies as
IRequestHandlerservices, allowing cross-cutting concerns to be applied via decorators. - AddDomainEventHandlers : Registers all
IDomainEventHandler<T>implementations found in the assembly. - AddDomainEventPublisher : Registers the
IDomainEventPublisherimplementation as a scoped service.
Domain Events
- IDomainEventPublisher : An interface defining a
PublishAsyncmethod that dispatches a domain event to all registered handlers. - IDomainEventHandler<TDomainEvent> : An interface for handling a specific domain event type, with a
HandleAsyncmethod. - DomainEventPublisher : A sealed implementation of
IDomainEventPublisherthat resolves all handlers for a given event type from the service provider and invokes them sequentially.
Extensions : Problem Details
The ToErrorListExtension class provides an extension method that converts a ProblemDetailsDto into a list of Error objects, mapping each error string to a validation error type.
Mediator
- IRequest<TResponse> : A marker interface for all requests that produce a response of type
TResponse. - IRequestHandler<TRequest, TResponse> : An interface defining a
HandleAsyncmethod for processing a specific request type. - ICommand<TResponse> : A marker interface for commands, extending
IRequest<Result<TResponse>>. - IQuery<TResponse> : A marker interface for queries, extending
IRequest<Result<TResponse>>. - ICommandHandler<TCommand, TResponse> : A handler interface for commands, extending
IRequestHandler. - IQueryHandler<TQuery, TResponse> : A handler interface for queries, extending
IRequestHandler. - ICommandHandlerProxy<TCommand, TResponse> : A proxy interface for command handlers, enabling decorator-based cross-cutting concerns.
- IQueryHandlerProxy<TQuery, TResponse> : A proxy interface for query handlers, enabling decorator-based cross-cutting concerns.
- IPipelineBehavior<TRequest, TResponse> : An interface for cross-cutting behaviors that wrap request handling, such as logging or validation.
- RequestHandlerDelegate<TResponse> : A delegate representing the next action in the pipeline chain.
- IMediator : An interface defining a
SendAsyncmethod to dispatch requests through the pipeline. - Mediator : A sealed implementation of
IMediatorthat builds and caches pipeline metadata using expression trees, resolves the handler and behaviors from the service provider, and executes them in a nested delegate chain.
Pipeline Behaviors
The LoggingPipelineBehavior<TRequest, TResponse> class is a sealed pipeline behavior that logs the request name and content before handling, measures execution time with a stopwatch, and logs the response with elapsed milliseconds after handling.