TG.WXCRM.V4/BLL/DbContextRepository.cs

240 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using WX.CRM.Common;
using WX.CRM.Model.Entity;
namespace WX.CRM.BLL
{
public class DbContextRepository<T> : IRepository<T> where T : class
{
public virtual T Get(Expression<Func<T, bool>> where)
{
using (var db = new crmContext())
{
return db.Set<T>().FirstOrDefault(where);
}
}
public virtual int Add(T entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
using (var db = new crmContext())
{
try
{
db.Set<T>().Add(entity);
return db.SaveChanges();
}
catch (DbEntityValidationException exception)
{
var errorMessages =
exception.EntityValidationErrors
.SelectMany(validationResult => validationResult.ValidationErrors)
.Select(m => m.ErrorMessage);
var fullErrorMessage = string.Join(", ", errorMessages);
//记录日志
//Log.Error(fullErrorMessage);
var exceptionMessage = string.Concat(exception.Message, " 验证异常消息是:", fullErrorMessage);
LogHelper.Error(exceptionMessage);
throw new DbEntityValidationException(exceptionMessage, exception.EntityValidationErrors);
}
catch (Exception ex)
{
throw ex;
}
}
}
public virtual void AddList(List<T> list)
{
if (list == null)
throw new ArgumentNullException("entity");
using (var db = new crmContext())
{
var _dbSet = db.Set<T>();
foreach (var item in list)
{
_dbSet.Add(item);
}
db.SaveChanges();
}
}
public virtual bool Update(T obj)
{
using (var db = new crmContext())
{
var entry = db.Entry(obj);
db.Set<T>().Attach(obj);
entry.State = EntityState.Modified;
return db.SaveChanges() > 0;
}
}
public virtual void Delete(T obj)
{
using (var db = new crmContext())
{
var entry = db.Entry(obj);
entry.State = EntityState.Deleted;
db.Set<T>().Remove(obj);
db.SaveChanges();
}
}
public virtual bool Delete(Expression<Func<T, bool>> where)
{
using (var db = new crmContext())
{
var result = db.Set<T>().RemoveRange(db.Set<T>().Where(where)).Count() > 0;
db.SaveChanges();
return result;
}
}
public virtual IEnumerable<T> GetList()
{
return GetList(null);
}
public virtual IEnumerable<T> GetList(Expression<Func<T, bool>> where)
{
using (var db = new crmContext())
{
Debug(db);
if (where != null)
{
return db.Set<T>().Where(where).ToList();
}
return db.Set<T>().ToList();
}
}
public virtual IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where,
Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending)
{
using (var db = new crmContext())
{
Debug(db);
if (orderBy == null)
{
return db.Set<T>().Where(where).ToList();
}
else
{
if (sortOrder == SortOrder.Descending)
return db.Set<T>().Where(where).OrderByDescending(orderBy).ToList();
else
return db.Set<T>().Where(where).OrderBy(orderBy).ToList();
}
}
}
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy,
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
{
return GetList(null, orderBy,
pageindex, pagesize, out totalRecords, sortOrder);
}
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
Pager pg, SortOrder sortOrder = SortOrder.Descending)
{
int totalRecords;
var list = GetList(where, orderBy,
pg.page, pg.rows, out totalRecords, sortOrder);
pg.totalRows = totalRecords;
return list;
}
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
{
using (var db = new crmContext())
{
Debug(db);
if (pageindex < 1)
throw new ArgumentException("pageindex");
if (pagesize < 1)
throw new ArgumentException("pagesize");
int skip = (pageindex - 1) * pagesize;
var _query = db.Set<T>().AsQueryable();
if (null != where)
_query = _query.Where(where);
totalRecords = _query.Count();
if (sortOrder == SortOrder.Descending)
{
_query = _query.OrderByDescending(orderBy);
}
else
_query = _query.OrderBy(orderBy);
return _query.Skip(skip).Take(pagesize).ToList();
}
}
public bool Exists(Expression<Func<T, bool>> where)
{
using (var db = new crmContext())
{
return db.Set<T>().Any(where);
}
}
public void Dispose()
{
//if (db != null)
// db.Dispose();
}
bool isDebug = true;
private void Debug(crmContext db)
{
//if (isDebug)
// db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
//if (isDebug)
// db.Database.Log = s => LogHelper.Debug(s);
}
}
public interface IRepository<T> : IDisposable where T : class
{
T Get(Expression<Func<T, bool>> where);
IEnumerable<T> GetList();
IEnumerable<T> GetList(Expression<Func<T, bool>> where);
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending);
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy, int pageindex, int pagesize, out int totalRecords
, SortOrder sortOrder = SortOrder.Descending);
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending);
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
Pager pg, SortOrder sortOrder = SortOrder.Descending);
int Add(T entity);
void AddList(List<T> list);
bool Update(T obj);
void Delete(T obj);
bool Delete(Expression<Func<T, bool>> where);
bool Exists(Expression<Func<T, bool>> where);
}
}