Example
For example, there is a customer repository and a customer repositoryinterface.
Â
namespace WebApplication16.Repository
{
interfaceICustomerRepo
{
//definition of your method
}
}
This is the customer repository interface. Declare your method here and implement that method in repository.
Â
namespace WebApplication16.Repository
{
publicclassCustomerRepo : ICustomerRepo
{
DbContext context;
public CustomerRepo(DbContext ctx)
{
this.context = ctx;
}
}
}
This is the customer repository that implements the method from ICustomerRepo.
These repository work on their dbcontext instance that is used to perform the operations onto the database using Entity Framework Core.
So, we use the Unit of Work pattern to group the all common (CRUD) operations like create, retrieve, update, and delete and place it in the generic repository.
In IRepo, we can declare two method for add and remove as shown below.
Â
Step 1: - Create generic repository interface
Â
namespace WebApplication16.Repository
{
interfaceIRepowhere T : class
{
void Add(T entity);
void Delete(T entity);
}
}
We have defined two method Add and Delete in IRepo and implement them in Repository named Repo as shown below.
Â
Step 2: - Create generic repository
Â
namespace WebApplication16.Repository
{
publicclassRepo : IRepowhere T : class
{
protectedreadonly DbSet _context;
public Repo(DbContext context)
{
_context = context.Set();
}
publicvoid Add(T entity)
{
_context.Add(entity);
}
publicvoid Delete(T entity)
{
_context.Remove(entity);
}
}
}
IRepo accept the generic type reference T of class type and implement that method in repository and works on context passed on to it.
The entity set is created using context.set() method which returns the specific dbset.
Now make use of these Interface repositories to link with customer repository as shown below.
Â
Step 3: - Create customer repository interface
Â
namespace WebApplication16.Repository
{
interfaceICustomerRepo : IRepo
{
//declare your methods
}
}
ICustomerRepo repository interface inherits the IRepo.
Â
Step 4: - Create customer repository
Â
namespace WebApplication16.Repository
{
publicclassCustomerRepo : Repo, ICustomerRepo
{
publicCustomerRepo(Context ctx) : base(ctx)
{
}
}
}
Now CustomerRepo class from Repo with their type reference Customer.
We have passed the context in the constructor which is derived from the baseconstructor.
Create the Unit of Work class to group all repositories.
Â
Step 5: - Create Unit of Work Interface
Â
namespace WebApplication16.Repository
{
interfaceIUoW : IDisposable
{
ICustomerRepo CustomerRepo {get;}
intCommit ();
}
}
We can create the IUoW interface which extends the IDisposable interface that provides a mechanism for releasing unmanaged resources.
Â
Step 6: - Create Unit of Work Class
Â
namespace WebApplication16.Repository
{
publicclassUow : IUoW
{
privatereadonly Context _context;
private IRepo customer;
public Uow(Context context)
{
this._context = context;
}
public ICustomerRepo CustomerRepo
{
get
{
returnnew CustomerRepo(_context);
}
}
publicint Commit()
{
return _context.SaveChanges();
}
publicvoid Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
Â
protectedvirtualvoid Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
}
}
}
Unit of Work class (Uow) extends the unit of work interface (IUoW). Which has a commit method and one property.
We have passed the context in the Uow constructor that invokes the commit method in which respective operations will be saved into the database.
Now create the controller which uses the Unit of Work interface and perform the operations on database.
Â
Step 7: - Create Constructor
Â
namespace WebApplication16.Controllers
{
[Route("api/[controller]")]
[ApiController]
publicclassCustomerController : ControllerBase
{
private IUoW _unitOfWork;
public CustomerController(IUoW unitOfWork)
{
_unitOfWork = unitOfWork;
}
[HttpGet]
public IEnumerable Get()
{
return _unitOfWork.CustomerRepo.GetAll();
}
[HttpGet("{id}")]
public Customer Get(int id)
{
return _unitOfWork.CustomerRepo.GetCustomerById(id);
}
[HttpPost]
{
var customer = new Customer
{
Id = 1,FirstName = "Raj",LastName = "Patel", Age = 22
} ;
_unitOfWork.CustomerRepo.Add(customer);
_unitOfWork.Commit();
return Ok();
}
}
We have created the customer controller that extends controller base.
In the customer controller constructor pass the Unit of Work interface instance. Now Define your method to get, add, and delete method from the repository.
In this way, we can create the simple Unit of Work pattern for our application which helps us to improve the database transaction time.