Before we start, we need to define some interfaces in the Catalog.Domain project. Let's proceed by setting a generic interface to determine the unit of work of our repository:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Catalog.Domain.Repositories
{
public interface IUnitOfWork : IDisposable
{
Task<int> SaveChangesAsync(CancellationToken cancellationToken =
default(CancellationToken));
Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken =
default(CancellationToken));
}
}
IUnitOfWork defines two methods: SaveChangesAsync and SaveEntitiesAsync. These two methods are used to save changes in our collection to the database effectively. These methods are both asynchronous: they return a Task type and they accept a CancellationToken type as a parameter. The CancellationToken parameter provides a way to stop pending asynchronous operations.
Let's proceed by defining an IRepository interface at the same folder level as the IUnitOfWork interface defined previously:
namespace Catalog.Domain.Repositories
{
public interface IRepository
{
IUnitOfWork UnitOfWork { get; }
}
}
As you can see, IRepository does not implicitly use the IUnitOfWork interface. Additionally, it exposes the UnitOfWork instance as a property of the class. This approach guarantees that all the consumers of the IRepository interface must explicitly update the database by calling the SaveChangesAsync or SaveEntitiesAsync methods.
The final step is to define the IItemRepository interface, as follows:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Catalog.Domain.Entities;
namespace Catalog.Domain.Repositories
{
public interface IItemRepository : IRepository
{
Task<IEnumerable<Item>> GetAsync();
Task<Item> GetAsync(Guid id);
Item Add(Item item);
Item Update(Item item);
}
}
The interface extends the IRepository class and refers to the Item entity that was defined previously. IItemRepository defines reading and writing operations over our data source. You may notice that the Add, Update. This is because they only act on the collection stored in the memory of the application, and the effective saving operation is performed by the unit of work.