Implementing the repository 

The following subsection focuses on building the concrete implementation of the IItemRepository interface. It is vital to note that the IItemRepository interface is located in the Catalog.Domain project and the implementation of ItemRepository is in the Catalog.Infrastructure project.

Once we have built the DbContext class, we can proceed by implementing the concrete ItemRepository class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Catalog.Domain.Entities;
using Catalog.Domain.Repositories;
using Microsoft.EntityFrameworkCore;

namespace Catalog.Infrastructure.Repositories
{
public class ItemRepository
: IItemRepository
{
private readonly CatalogContext _context;

public IUnitOfWork UnitOfWork => _context;

public ItemRepository(CatalogContext context)
{
_context = context ?? throw new
ArgumentNullException(nameof(context));
}

public async Task<IEnumerable<Item>> GetAsync()
{
return await _context
.Items
.AsNoTracking()
.ToListAsync();
}

public async Task<Item> GetAsync(Guid id)
{
var item = await _context.Items
.AsNoTracking()
.Where(x => x.Id == id)
.Include(x => x.Genre)
.Include(x => x.Artist).FirstOrDefaultAsync();

return item;
}

public Item Add(Item order)
{
return _context.Items
.Add(order).Entity;
}

public Item Update(Item item)
{
_context.Entry(item).State = EntityState.Modified;
return item;
}
}
}

The preceding code implements the CRUD operations previously defined in the IItemRepository interface. It also exposes CategoryContext using the IUnitOfWork interface. This kind of approach guarantees that the consumer of IItemRepository can modify and query our collections and also update the data source with the corresponding changes. Let's walk-through the methods implemented in the preceding code:

  • The GetAsync() method uses the context to retrieve the collection of Items entities. The method uses the AsNoTracking() method explicitly in order to prevent the tracking of entities. This extension method can be used every time you don't need to perform writing operations on entities, and it is meant for read-only data.
  • GetAsync(Guid id) overloads the previously mentioned method, and it uses AsNoTracking() for the same purpose described previously. This method also gets details of the related entities (Genre and Artist) by using the Include() extension method provided by EF Core. 
  • The Add(Item entity) method uses the context to add the entity passed as a parameter and it returns the added entity to the caller.
  • The Edit method updates the target entity from the context and sets EntityState.Modified. This approach guarantees that, once the entity is in a modified state, it will be updated at the saving step.

The preceding code doesn't implement the Delete method. This is because the delete process will be implemented later on in the book. We will essentially perform a soft deletion of our data.
..................Content has been hidden....................

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