Skip to content

ApplicationLayer

Mehmet Özkaya edited this page Apr 14, 2019 · 6 revisions

Development of Domain Logic with implementation. Interfaces drives business requirements and implementations in this layer. Application layer defines that user required actions in app services classes as below way;

public interface IProductAppService
{
    Task<IEnumerable<ProductDto>> GetProductList();
    Task<ProductDto> GetProductById(int productId);
    Task<IEnumerable<ProductDto>> GetProductByName(string productName);
    Task<IEnumerable<ProductDto>> GetProductByCategory(int categoryId);
    Task<ProductDto> Create(ProductDto entityDto);
    Task Update(ProductDto entityDto);
    Task Delete(ProductDto entityDto);
}

Also implementation located same places in order to choose different implementation at runtime when DI bootstrapped.

public class ProductAppService : IProductAppService
{
    private readonly IProductRepository _productRepository;
    private readonly IAppLogger<ProductAppService> _logger;

    public ProductAppService(IProductRepository productRepository, IAppLogger<ProductAppService> logger)
    {
        _productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task<IEnumerable<ProductDto>> GetProductList()
    {
        var productList = await _productRepository.GetProductListAsync();
        var mapped = ObjectMapper.Mapper.Map<IEnumerable<ProductDto>>(productList);
        return mapped;
    }
}

In this layer we can add validation , authorization, logging, exception handling etc. -- cross cutting activities should be handled in here.

Application Layer Components

Beyond this developments, also there are main components should be handled in Application layer;

  • Authorization Management
  • Validation Management
  • Session Management
  • Notification Management
  • Exception Management

So in this layer we have these cross-cutting activities which provide to manage your application. You will authorize your application in this layer, you should apply your validations in here, you should manage application session and publish notifications from application layer with exception management.

Then the implementation of application layer would be something like below code block;

public async Task AssignIssueToUser(AssignIssueToUserInput input)
{
	// authorization
	AuthorizationService.CheckPermission("TaskAssignmentPermission");

	// validation
	_validationService.Validate(input);

	// domain
	var user = await _userRepository.GetByIdAsync(input.UserId);
	var issue = await _issueRepository.GetByIdAsync(input.IssueId);

	issue.AssignTo(user, _issueAssignmentPolicy);
	await _issueRepository.UpdateAsync(issue);


	// notification
	if (SessionService.UserId != user.Id)
	{
		_userEmailer.IssueAssigned(user, issue);
	}
	
	// logging
	_logger.LogInformation($"Assigned issue {issue} to user {user}");            
}

As you can see that all components of application layer represents in a particular "Assign Issue to User" Use Case method.

Clone this wiki locally