using LSALogger;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace LSADataAccess
{
public class DataAccessorHelper
{
private readonly static string formatString = typeof(DataAccessorHelper).FullName;
#region Select Method
/// <summary>
/// Select Single
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <returns></returns>
public T SelectSingle<T>(Func<T, bool> selector = null) where T : class
{
T result = null;
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
if (selector == null)
{
result = context.Set<T>().FirstOrDefault();
}
else
{
result = context.Set<T>().FirstOrDefault(selector);
}
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
/// <summary>
/// Select Single Join Table
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <param name="includePath"></param>
/// <returns></returns>
public T SelectSingle<T>(Func<T, bool> selector, string includePath) where T : class
{
T result = null;
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
result = context.CreateObjectSet<T>()
.Include(includePath)
.Where(selector)
.FirstOrDefault();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
/// <summary>
/// Select Single Join Tables
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <param name="includePaths"></param>
/// <returns></returns>
public T SelectSingle<T>(Func<T, bool> selector, string[] includePaths) where T : class
{
T result = null;
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
ObjectQuery<T> query = context.CreateObjectSet<T>().Where("1==1");
if (includePaths != null && includePaths.Count() > 0)
{
foreach (string path in includePaths)
{
query = query.Include(path);
}
}
result = query.FirstOrDefault(selector);
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
/// <summary>
/// Select All
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IEnumerable<T> Select<T>() where T : class
{
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
return context.CreateObjectSet<T>().ToList();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return null;
}
}
/// <summary>
/// Select Collection
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <returns></returns>
public IEnumerable<T> Select<T>(Func<T, bool> selector) where T : class
{
IEnumerable<T> result = null;
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
result = context.Set<T>().Where(selector).ToList();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
/// <summary>
/// Select Collection Join Table
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <param name="include"></param>
/// <returns></returns>
public IEnumerable<T> Select<T>(Func<T, bool> selector, string include) where T : class
{
IEnumerable<T> result = null;
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
ObjectQuery<T> query = context.CreateObjectSet<T>().Where("1==1");
var data = context.CreateObjectSet<T>()
.Include(include)
.Where(selector);
result = data.ToList();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
/// <summary>
/// Select Collection Join Tables
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <param name="includes"></param>
/// <returns></returns>
public IEnumerable<T> Select<T>(Func<T, bool> selector, string[] includes) where T : class
{
IEnumerable<T> result = null;
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
ObjectQuery<T> query = context.CreateObjectSet<T>().Where("1==1");
if (includes != null && includes.Count() > 0)
{
foreach (string path in includes)
{
query = query.Include(path);
}
}
result = query.Where(selector).ToList();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
#endregion
#region Insert Method
/// <summary>
/// Insert Single
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public int Insert<T>(T entity) where T : class
{
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
context.CreateObjectSet<T>().AddObject(entity);
return context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
#region other way
//using (DataAccessorEntities context = new DataAccessorEntities())
//{
// context.Set<T>().Add(entity);
// return context.SaveChanges();
//}
#endregion
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
/// <summary>
/// Insert Collection
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entities"></param>
/// <returns></returns>
public int Insert<T>(IEnumerable<T> entities) where T : class
{
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
context.Set<T>().AddRange(entities);
return context.SaveChanges();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
#endregion
#region Update Method
/// <summary>
/// Update Single
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public int Update<T>(T entity) where T : class
{
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
ObjectSet<T> objectSet = context.CreateObjectSet<T>();
objectSet.Attach(entity);
context.ObjectStateManager.ChangeObjectState(entity, System.Data.Entity.EntityState.Modified);
return context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
#region other way
//using (DataAccessorEntities context = new DataAccessorEntities())
//{
// context.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified;
// return context.SaveChanges();
//}
#endregion
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
/// <summary>
/// Update Collection
/// See better way: https://github.com/loresoft/EntityFramework.Extended
/// NuGet: https://www.nuget.org/packages/EntityFramework.Extended
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entities"></param>
/// <returns></returns>
public int Update<T>(IEnumerable<T> entities) where T : class
{
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
foreach (var item in entities)
{
context.Entry<T>(item).State = EntityState.Modified;
}
return context.SaveChanges();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
#endregion
#region Delete Method
public int Delete<T>(T entity) where T : class
{
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
context.Entry<T>(entity).State = EntityState.Deleted;
return context.SaveChanges();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
/// <summary>
/// Delete Collection
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entities"></param>
/// <returns></returns>
public int Delete<T>(IEnumerable<T> entities) where T : class
{
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
DbSet<T> dbSet = context.Set<T>();
foreach (var item in entities)
{
dbSet.Attach(item);
}
dbSet.RemoveRange(entities);
return context.SaveChanges();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
public int Delete<T>(Func<T, bool> selector) where T : class
{
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
IEnumerable<T> deleteData = context.Set<T>().Where(selector);
foreach (var item in deleteData)
{
context.Set<T>().Attach(item);
}
context.Set<T>().RemoveRange(deleteData);
return context.SaveChanges();
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
#endregion
#region Other Method
/// <summary>
/// Whether the specified object exists
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <returns>-1:Exception, 1: Existed, 0: Not Exist</returns>
public int IsExists<T>(Func<T, bool> selector) where T : class
{
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
return context.Set<T>().Where(selector).Count() > 0 ? 1 : 0;
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
return -1;
}
}
/// <summary>
/// Whether the specified object exists
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns>-1:Exception, 1: Existed, 0: Not Exist</returns>
public int IsExists<T>(T entity) where T : class
{
try
{
using (DataAccessorObjectEntities context = new DataAccessorObjectEntities())
{
object value;
var entityKeyValues = new List<KeyValuePair<string, object>>();
EntitySet entitySet = context.CreateObjectSet<T>().EntitySet;
foreach (var member in entitySet.ElementType.KeyMembers)
{
var info = entity.GetType().GetProperty(member.Name);
var tempValue = info.GetValue(entity, null);
var pair = new KeyValuePair<string, object>(member.Name, tempValue);
entityKeyValues.Add(pair);
}
var key = new EntityKey(entitySet.EntityContainer.Name + "." + entitySet.Name, entityKeyValues);
if (context.TryGetObjectByKey(key, out value))
{
return value != null ? 1 : 0;
}
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return -1;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="selector"></param>
/// <param name="maxfunc"></param>
/// <returns></returns>
public object Max<T>(Func<T, bool> selector, Func<T, object> maxfunc) where T : class
{
object result = null;
try
{
using (DataAccessorEntities context = new DataAccessorEntities())
{
result = context.Set<T>().Where(selector).Max(maxfunc);
}
}
catch (Exception ex)
{
PrintExceptionLog(ex);
}
return result;
}
#endregion
#region Private Method
/// <summary>
/// Print Exception Log Info
/// </summary>
/// <param name="ex"></param>
private void PrintExceptionLog(Exception ex)
{
StackTrace trace = new StackTrace();
MethodBase method = trace.GetFrame(1).GetMethod();
LogHelper.Instance.Error(string.Format("{0}::{1} - Exception:", formatString, method), ex);
}
#endregion
}
}