Would it be nice if we have a CRUD that works for all tables for EntityFramework without writing repetitive codes? Thanks to Generics, we can finally achieve the tasks.
using SimplePOS.Data;
using SimplePOS.Model;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace SimplePOS.Helper
{
public class CRUDHelper
{
public static CRUDHelper Instance => new Lazy<CRUDHelper>().Value;
public DataContext DbContext { get; set; } = null;
// CREATE
public async Task<ReturnResult> AddAsync<T>(T item, Expression<Func<object, bool>> WhereEx, string messageIfExists) where T : class
{
ReturnResult result = new ReturnResult();
if (DbContext is null) return result;
try
{
var b = DbContext.Set<T>().Where(WhereEx);
if (!b.Any())
{
await DbContext.Set<T>().AddAsync(item).ContinueWith(async (a) =>
{
await DbContext.SaveChangesAsync();
});
result.Result = true;
}
else
{
result.Result = false;
result.Message = messageIfExists;
}
}
catch (Exception ex)
{
result.Result = false;
result.Message = ex.Message;
result.Ex = ex;
}
return result;
}
// READ with specified parameters
public ReturnResult Read<T>(Expression<Func<object, bool>> WhereEx) where T : class
{
ReturnResult result = new ReturnResult();
if (DbContext is null) return result;
try
{
var b = DbContext.Set<T>().Where(WhereEx);
if (b.Any())
{
result.Result = b.ToList();
}
else
{
result.Result = false;
}
}
catch (Exception ex)
{
result.Result = false;
result.Message = ex.Message;
result.Ex = ex;
}
return result;
}
// READ all
public ReturnResult ReadAll<T>() where T : class
{
ReturnResult result = new ReturnResult();
if (DbContext is null) return result;
try
{
var b = DbContext.Set<T>().ToList<T>();
if (b.Any())
{
result.Result = b;
}
else
{
result.Result = false;
}
}
catch (Exception ex)
{
result.Result = false;
result.Message = ex.Message;
result.Ex = ex;
}
return result;
}
// UPDATE
public async Task<ReturnResult> UpdateAsync<T>(T item, Expression<Func<object, bool>> WhereEx, string messageIfExists) where T : class
{
ReturnResult result = new ReturnResult();
if (DbContext is null) return result;
try
{
var b = DbContext.Set<T>().Where(WhereEx);
if (!b.Any())
{
DbContext.Set<T>().Update(item);
await DbContext.SaveChangesAsync();
result.Result = true;
}
else
{
result.Result = false;
result.Message = messageIfExists;
}
}
catch (Exception ex)
{
result.Result = false;
result.Message = ex.Message;
result.Ex = ex;
}
return result;
}
// DELETE
public async Task<ReturnResult> DeleteAsync<T>(T item) where T : class
{
ReturnResult result = new ReturnResult();
if (DbContext is null) return result;
try
{
var b = DbContext.Set<T>().Remove(item);
await DbContext.SaveChangesAsync();
result.Result = true;
}
catch (Exception ex)
{
result.Result = false;
result.Message = ex.Message;
result.Ex = ex;
}
return result;
}
}
}
The code is pretty straight forward. One thing to note is to make sure your set the DbContext first
i.e.
CRUDHelper.Instance.DbContext = <your_db_context>;
Comments
Post a Comment