Service /Repository Pattern:
What Is Services\Repository Pattern:
public class StudentClassService : IRepository<STD_CLASS>, IDisposable
{
private readonly _DbContext _DbContext;
public StudentClassService(_DbContext dbContext)
{
_DbContext = dbContext;
}
public void Add(STD_CLASS entity)
{
_DbContext.STDCLASS.Add(entity);
_DbContext.SaveChanges();
}
public async Task<bool> AddAsync(STD_CLASS entity)
{
await _DbContext.STDCLASS.AddAsync(entity);
await _DbContext.SaveChangesAsync();
return true;
}
public async Task<bool> AddRangeAsync(IEnumerable<STD_CLASS> entity)
{
await _DbContext.STDCLASS.AddRangeAsync(entity);
await _DbContext.SaveChangesAsync();
return true;
}
public void Delete(STD_CLASS entity)
{
_DbContext.Entry(entity).State = EntityState.Modified;
_DbContext.SaveChanges();
}
public async Task<bool> DeleteAsync(STD_CLASS entity)
{
_DbContext.Entry(entity).State = EntityState.Modified;
await _DbContext.SaveChangesAsync();
return true;
}
public void DeleteByFlag(STD_CLASS entity)
{
throw new NotImplementedException();
}
public void DeleteById(int id)
{
throw new NotImplementedException();
}
public Task<bool> DeleteRange(STD_CLASS entity)
{
throw new NotImplementedException();
}
public void DeleteRange(IEnumerable<STD_CLASS> entity)
{
throw new NotImplementedException();
}
public void Dispose()
{
_DbContext.Dispose();
}
public STD_CLASS Get(int id)
{
return _DbContext.STDCLASS.Where(c => c.Id == id && c.IsDeleted == false).FirstOrDefault();
}
public STD_CLASS Get(string Name)
{
return _DbContext.STDCLASS.Where(c => c.ClassName.ToLower() == Name.ToLower() && c.IsDeleted == false).FirstOrDefault();
}
public IEnumerable<STD_CLASS> GetAll()
{
return _DbContext.STDCLASS.Where(c => c.IsDeleted==false).ToList();
}
public void Update(STD_CLASS entity)
{
_DbContext.Entry(entity).State = EntityState.Modified;
_DbContext.SaveChanges();
}
public async Task<bool> UpdateAsync(STD_CLASS entity)
{
_DbContext.Entry(entity).State = EntityState.Modified;
await _DbContext.SaveChangesAsync();
return true;
}
}