I am implementing Repository and Unit of Work in ASP.NET Core-6 Web API. i HAVE THIS CODE:
public interface IGenericRepository<T> where T : class
{
T GetById(string id);
IEnumerable<T> GetAll();
IEnumerable<T> Find(Expression<Func<T, bool>> expression);
void Add(T entity);
Task InsertAsync(T entity);
void AddRange(IEnumerable<T> entities);
void Delete(T entity);
void DeleteRange(IEnumerable<T> entities);
void Update(T entity);
}
AND THE IMPLEMENTATION:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly ApplicationDbContext _dbContext;
private readonly DbSet<T> _dbSet;
public GenericRepository(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public void Add(T entity)
{
_dbContext.Set<T>().Add(entity);
}
public async Task InsertAsync(T entity)
{
await _dbSet.AddAsync(entity);
}
public void AddRange(IEnumerable<T> entities)
{
_dbContext.Set<T>().AddRange(entities);
}
public IEnumerable<T> Find(Expression<Func<T, bool>> expression)
{
return _dbContext.Set<T>().Where(expression);
}
public IEnumerable<T> GetAll()
{
return _dbContext.Set<T>().ToList();
}
public T GetById(string id)
{
return _dbContext.Set<T>().Find(id);
}
public void Delete(T entity)
{
_dbContext.Set<T>().Remove(entity);
}
public void DeleteRange(IEnumerable<T> entities)
{
_dbContext.Set<T>().RemoveRange(entities);
}
public void Update(T entity)
{
_dbContext.Set<T>().Update(entity);
}
}
Also I have the Repository:
Interface:
public interface IBabRepository : IGenericRepository<BabTransactionDetail>
{
}
Implementation:
public class BabRepository : GenericRepository<BabTransactionDetail>, IBabRepository
{
private readonly ApplicationDbContext _dbContext;
private readonly DbSet<BabTransactionDetail> _babTransactions;
public BabRepository(ApplicationDbContext dbContext) : base(dbContext)
{
_dbContext = dbContext;
_babTransactions = _dbContext.Set<BabTransactionDetail>();
}
}
Unit of Work:
public interface IUnitOfWork : IDisposable
{
IBabRepository BabTransactionDetails { get; }
Task Save();
}
public class UnitOfWork : IUnitOfWork
{
private IBabRepository _babTransactions;
private readonly ApplicationDbContext _dbContext;
public UnitOfWork(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public IBankUserRepository BankUsers => _bankUsers ??= new BankUserRepository(_dbContext);
public IBabRepository BabTransactionDetails => _babTransactions ??= new BabRepository(_dbContext);
public async Task Save()
{
await _dbContext.SaveChangesAsync();
}
public void Dispose()
{
_dbContext.Dispose();
GC.SuppressFinalize(this);
}
}
Finally, the service:
public interface IBabService
{
Task<BaseResponse> MyAccount(InputRequest request);
}
public async Task<BaseResponse> MyAccount(InputRequest payload)
{
var response = new BaseResponse();
try
{
var babData = new BabTransactionDetail
{
UserName = payload.user_name,
UserEmail = payload.user_email,
UserPhone = payload.user_phone,
Amount = payload.amount,
};
// await _unitOfWork.BabTransactionDetails.InsertAsync(babData);
// await _unitOfWork.Save();
_dbContext.BabTransactionDetails.Add(babData);
await _dbContext.SaveChangesAsync();
}
When I submitted, I got this error:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Application
StackTrace:
at ApplicationRepositories.Concrete.GenericRepository`1.<InsertAsync>d__4.MoveNext()
and points at InsertAsync(T entity)
I checked the models and entries, there is data.
Where am I getting it wrong, and how do I resolve this?
Thanks