UnitOfWork Layer creation
Add a folder in CMBusinessTyre project Named UnitOfWorkLayer
add 2 Interface and 2 class files.
IMyUnitOfWork
IGenericRepository
MyUnitOfWork
GenericRepository
public interface IMyUnitOfWork
{
int Commit();
}
-------------------------------------------------
public interface IGenericRepository<T>
{
IEnumerable<T> GetData();
T GetByID(object id);
T Insert(T entity);
void Delete(object id);
void Update(T entityToUpdate);
IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate);
}
-------------------------------------------------
public class MyUnitOfWork : IMyUnitOfWork, IDisposable
{
private GenericRepository<LoginUser> _loginUser;
private GenericRepository<LogRegister> _logRegister;
private GenericRepository<ContactInfo> _contactInfo;
private GenericRepository<ContactAddress> _addressInfo;
ContactManagerContext context = new ContactManagerContext();
public GenericRepository<LoginUser> LoginUser
{
get
{
if (this._loginUser == null)
this._loginUser = new GenericRepository<LoginUser>(context);
return _loginUser;
}
}
public GenericRepository<LogRegister> LogRegister
{
get
{
if (this._logRegister == null)
this._logRegister = new GenericRepository<LogRegister>(context);
return _logRegister;
}
}
public GenericRepository<ContactInfo> ContactInfo
{
get
{
if (this._contactInfo == null)
this._contactInfo = new GenericRepository<ContactInfo>(context);
return _contactInfo;
}
}
public GenericRepository<ContactAddress> ContactAddress
{
get
{
if (this._addressInfo == null)
this._addressInfo = new GenericRepository<ContactAddress>(context);
return _addressInfo;
}
}
public int Commit()
{
try
{
return context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
// Join the list to a single string.
var fullErrorMessage = string.Join("; ", errorMessages);
// Combine the original exception message with the new one.
var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
// Throw a new DbEntityValidationException with the improved exception message.
throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
}
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
------------------------------------------------------------------
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
internal ContactManagerContext context;
internal DbSet<T> dbSet;
public GenericRepository(ContactManagerContext context)
{
this.context = context;
this.dbSet = context.Set<T>();
}
public IEnumerable<T> GetData()
{
return dbSet.ToList();
}
public T GetByID(object id)
{
return dbSet.Find(id);
}
public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)
{
IEnumerable<T> query = dbSet.Where(predicate).AsEnumerable();
return query;
}
public T Insert(T entity)
{
return dbSet.Add(entity);
}
public void Delete(object id)
{
T entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
//public virtual T Delete(T entityToDelete)
//{
// if (context.Entry(entityToDelete).State == EntityState.Detached)
// {
// dbSet.Attach(entityToDelete);
// }
// dbSet.Remove(entityToDelete);
//}
public void Update(T entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
------------------------------------------------------------
Comments
Post a Comment