The service class implementation

Once the mapper layer is completed, we can proceed by implementing the service layer. Let's start by defining the ItemService.cs file in the Services folder of the Catalog.Domain project. The following code describes the implementation of the constructor method and the reading operations:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Catalog.Domain.Mappers;
using Catalog.Domain.Repositories;
using Catalog.Domain.Requests.Item;
using Catalog.Domain.Responses;

namespace Catalog.Domain.Services
{
public class ItemService : IItemService
{
private readonly IItemRepository _itemRepository;
private readonly IItemMapper _itemMapper;

public ItemService(IItemRepository itemRepository,
IItemMapper itemMapper)
{
_itemRepository = itemRepository;
_itemMapper = itemMapper;
}

public async Task<IEnumerable<ItemResponse>> GetItemsAsync()
{
var result = await _itemRepository.GetAsync();

return result.Select(x => _itemMapper.Map(x));
}

public async Task<ItemResponse> GetItemAsync(GetItemRequest
request)
{
if (request?.Id == null) throw new ArgumentNullException();

var entity = await _itemRepository.GetAsync(request.Id);

return _itemMapper.Map(entity);
}
}
}

First of all, we can see that the class refers to both the IItemRepository and the IItemMapper interfaces, which are injected using the constructor injection technique. The snippet also describes the implementation of the GetItemsAsync and GetItemAsync functions. Both methods use the IItemRepository interface to retrieve data from the data source, and the IItemMapper interface to perform the mapping between the Item entity and ItemResponse. The same approach can be taken by the writing operations, which are implemented with the following:


namespace Catalog.Domain.Services
{
public class ItemService : IItemService
{
...

public async Task<ItemResponse> AddItemAsync(AddItemRequest
request)
{
var item = _itemMapper.Map(request);
var result = _itemRepository.Add(item);

await _itemRepository.UnitOfWork.SaveChangesAsync();

return _itemMapper.Map(result);
}

public async Task<ItemResponse> EditItemAsync(EditItemRequest
request)
{
var existingRecord = await
_itemRepository.GetAsync(request.Id);

if (existingRecord == null)
{
throw new ArgumentException($"Entity with {request.Id}
is not present");
}

var entity = _itemMapper.Map(request);
var result = _itemRepository.Update(entity);

await _itemRepository.UnitOfWork.SaveChangesAsync();

return _itemMapper.Map(result);
}
}
}

Also, in the case of the writing operations, they use the IItemMapper instance to map the request's type with the Item entity type and to retrieve the ItemResponse type. In addition, they perform the operations by calling the IItemRepository instance, and subsequently, they call the SaveChangesAsync method to apply those changes to the database. Once we have implemented the service layer, we can proceed by testing the class and verifying the implementation. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.20