AkbarAmd.SharedKernel.Application
1.0.19
dotnet add package AkbarAmd.SharedKernel.Application --version 1.0.19
NuGet\Install-Package AkbarAmd.SharedKernel.Application -Version 1.0.19
<PackageReference Include="AkbarAmd.SharedKernel.Application" Version="1.0.19" />
<PackageVersion Include="AkbarAmd.SharedKernel.Application" Version="1.0.19" />
<PackageReference Include="AkbarAmd.SharedKernel.Application" />
paket add AkbarAmd.SharedKernel.Application --version 1.0.19
#r "nuget: AkbarAmd.SharedKernel.Application, 1.0.19"
#:package AkbarAmd.SharedKernel.Application@1.0.19
#addin nuget:?package=AkbarAmd.SharedKernel.Application&version=1.0.19
#tool nuget:?package=AkbarAmd.SharedKernel.Application&version=1.0.19
AkbarAmd.SharedKernel.Application
Clean Architecture Application Layer - CQRS, MediatR, Validation, and Application Services
Overview
The Application layer orchestrates business operations and coordinates between the Domain and Infrastructure layers. This package provides CQRS patterns, command/query handlers, DTOs, and service result models for building maintainable application services.
Purpose
This layer defines:
- CQRS contracts (Commands, Queries, Events)
- Handler base classes for MediatR integration
- DTOs for data transfer between layers
- Service results for standardized operation outcomes
- Mapping utilities for object transformation
Key Components
CQRS Contracts
ICommand: Base interface for commands (state-changing operations)ICommand<TResult>: Command that returns a resultIQuery<TResult>: Base interface for queries (read operations)IEvent: Integration event interface for cross-bounded context communication
Handler Base Classes
CommandHandler<TCommand>: Base class for command handlersCommandHandler<TCommand, TResult>: Base class for command handlers with return valuesQueryHandler<TQuery, TResult>: Base class for query handlersEventHandler<TEvent>: Base class for event handlersDomainEventHandler<TEvent>: Base class for domain event handlers
Service Results
ServiceResult: Result model for operations without return dataServiceResult<T>: Result model for operations with return dataValidationResult: Validation error result modelPaginatedResult<T>: Paginated query result model
DTOs
EntityDto<TKey>: Base DTO for entitiesAggregateDto<TKey>: Base DTO for aggregate rootsCreatableAggregateDto<TKey>: DTO with creation trackingModifiableAggregateDto<TKey>: DTO with modification trackingDeletableAggregateDto<TKey>: DTO with deletion tracking
Mapping
IMapper: Mapping interface abstractionServiceCollectionMapperExtensions: Extension methods for mapper registration
Features
- CQRS Pattern: Clean separation of commands and queries
- MediatR Integration: Decoupled communication via mediator pattern
- Handler Lifecycle: Built-in validation, logging, and error handling hooks
- Service Results: Standardized operation outcomes with error handling
- DTO Support: Base DTOs for common aggregate root patterns
- Mapping Abstraction: Framework-agnostic mapping interface
Installation
dotnet add package AkbarAmd.SharedKernel.Application
Usage Examples
Creating a Command Handler
public class CreateUserCommand : ICommand<Guid>
{
public string Email { get; set; }
public string Name { get; set; }
}
public class CreateUserCommandHandler : CommandHandler<CreateUserCommand, Guid>
{
private readonly IRepository<User, Guid> _userRepository;
public CreateUserCommandHandler(IRepository<User, Guid> userRepository)
{
_userRepository = userRepository;
}
protected override async Task<Guid> ProcessAsync(
CreateUserCommand request,
CancellationToken cancellationToken)
{
var user = new User(request.Email, request.Name);
await _userRepository.AddAsync(user, cancellationToken);
return user.Id;
}
}
Creating a Query Handler
public class GetUserQuery : IQuery<UserDto>
{
public Guid UserId { get; set; }
}
public class GetUserQueryHandler : QueryHandler<GetUserQuery, UserDto>
{
private readonly IReadOnlyRepository<User, Guid> _userRepository;
private readonly IMapper _mapper;
protected override async Task<UserDto> ProcessAsync(
GetUserQuery request,
CancellationToken cancellationToken)
{
var user = await _userRepository.GetByIdAsync(request.UserId, cancellationToken);
return _mapper.Map<UserDto>(user);
}
}
Using Service Results
public async Task<ServiceResult<UserDto>> GetUserAsync(Guid userId)
{
var user = await _userRepository.GetByIdAsync(userId);
if (user == null)
return ServiceResult<UserDto>.Failure("User not found", "USER_NOT_FOUND");
var dto = _mapper.Map<UserDto>(user);
return ServiceResult<UserDto>.Success(dto);
}
Creating Integration Events
public class UserCreatedEvent : IEvent
{
public Guid Id { get; } = Guid.NewGuid();
public DateTime OccurredOn { get; } = DateTime.UtcNow;
public string? CorrelationId { get; set; }
public string Source { get; } = "UserService";
public Guid UserId { get; }
public string Email { get; }
public UserCreatedEvent(Guid userId, string email)
{
UserId = userId;
Email = email;
}
}
Dependencies
- MediatR 12.5.0: Mediator pattern implementation
- FluentValidation 11.5.0: Validation framework
- AutoMapper 12.0.1: Object mapping
- Microsoft.Extensions.Logging.Abstractions 8.0.3: Logging abstractions
- Bonyan 1.5.6: Modularity framework
Architecture Principles
This layer follows Clean Architecture principles:
- Depends on Domain layer only
- No infrastructure dependencies (except abstractions)
- Application logic orchestration
- Framework integration (MediatR, AutoMapper)
License
MIT License - see LICENSE file for details.
Author
Akbar Ahmadi Saray - GitHub
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- AkbarAmd.SharedKernel.Domain (>= 1.0.19)
- AutoMapper (>= 12.0.1)
- Bonyan (>= 1.5.6)
- FluentValidation (>= 11.5.0)
- MediatR (>= 12.5.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 1.0.19:
- Updated to support nested includes from Domain layer
- Enhanced application layer compatibility with new IncludeChain functionality
Version 1.0.18:
- Updated ModifiableAggregateDto to use ModifiedAt/ModifiedBy instead of LastModifiedAt/LastModifiedBy
- Breaking change: ModifiableAggregateDto properties renamed from LastModifiedAt/LastModifiedBy to ModifiedAt/ModifiedBy