visit
public interface IUnitOfWork
{
void Commit();
void Rollback();
}
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _dbContext;
public UnitOfWork(DbContext dbContext)
{
_dbContext = dbContext;
}
public void Commit()
{
_dbContext.SaveChanges();
}
public void Rollback()
{
// Implement rollback logic
}
}
public interface IRepository<T> where T : class
{
void Add(T entity);
void Remove(T entity);
void Update(T entity);
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbSet<T> _dbSet;
private readonly IUnitOfWork _unitOfWork;
public Repository(DbSet<T> dbSet, IUnitOfWork unitOfWork)
{
_dbSet = dbSet;
_unitOfWork = unitOfWork;
}
public void Add(T entity)
{
_dbSet.Add(entity);
}
public void Remove(T entity)
{
_dbSet.Remove(entity);
}
public void Update(T entity)
{
_dbSet.Update(entity);
}
public void SaveChanges()
{
_unitOfWork.Commit();
}
}
public interface IUnitOfWork
{
void Commit();
void Rollback();
IRepository<Customer> Customers { get; }
IRepository<Account> Accounts { get; }
IRepository<Transaction> Transactions { get; }
}
public class UnitOfWork : IUnitOfWork
{
private readonly BankingDbContext _dbContext;
private IRepository<Customer> _customers;
private IRepository<Account> _accounts;
private IRepository<Transaction> _transactions;
public UnitOfWork(BankingDbContext dbContext)
{
_dbContext = dbContext;
}
public IRepository<Customer> Customers
{
get
{
if (_customers == null)
{
_customers = new Repository<Customer>(_dbContext.Customers, this);
}
return _customers;
}
}
public IRepository<Account> Accounts
{
get
{
if (_accounts == null)
{
_accounts = new Repository<Account>(_dbContext.Accounts, this);
}
return _accounts;
}
}
public IRepository<Transaction> Transactions
{
get
{
if (_transactions == null)
{
_transactions = new Repository<Transaction>(_dbContext.Transactions, this);
}
return _transactions;
}
}
public void Commit()
{
_dbContext.SaveChanges();
}
public void Rollback()
{
// Implement rollback logic
}
}
public interface IRepository<T> where T : class
{
void Add(T entity);
void Remove(T entity);
void Update(T entity);
IEnumerable<T> GetAll();
T GetById(int id);
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbSet<T> _dbSet;
private readonly IUnitOfWork _unitOfWork;
public Repository(DbSet<T> dbSet, IUnitOfWork unitOfWork)
{
_dbSet = dbSet;
_unitOfWork = unitOfWork;
}
public void Add(T entity)
{
_dbSet.Add(entity);
}
public void Remove(T entity)
{
_dbSet.Remove(entity);
}
public void Update(T entity)
{
_dbSet.Update(entity);
}
public IEnumerable<T> GetAll()
{
return _dbSet.ToList();
}
public T GetById(int id)
{
return _dbSet.Find(id);
}
public void SaveChanges()
{
_unitOfWork.Commit();
}
}
var unitOfWork = new UnitOfWork(new BankingDbContext());
var customer = new Customer
{
FirstName = "John",
LastName = "Doe",
Email = "[email protected]"
};
unitOfWork.Customers.Add(customer);
unitOfWork.Customers.SaveChanges();
Similarly, we can use the Unit of Work and Repository to perform other database operations, such as transferring money from one account to another, retrieving account details, etc.
Also published