123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Runtime.Serialization;
- using iiie.Logs.DBO;
- using iiie.WebApiUtils.DBO;
-
- namespace iiie.WebApiUtils.BusinessManager
- {
-
-
-
- public abstract class SqlServerManager<TDbObject, TDboGet, TDboAdd, TDboEdit, TEntities, TThis>
- : SqlServerBasicManager<TDbObject, TEntities, TThis>
- where TDbObject : class
- where TEntities : DbContext, new()
- where TThis : SqlServerManager<TDbObject, TDboGet, TDboAdd, TDboEdit, TEntities, TThis>, new()
- {
-
-
-
-
-
-
- public abstract TDboGet DbToDboGet(TDbObject obj);
-
-
-
-
-
-
-
- public abstract OpResult<bool> DboAddToDb(TDboAdd obj, TDbObject add);
-
-
-
-
-
-
-
- public abstract OpResult<bool> DboEditToDb(TDboEdit obj, TDbObject edit);
-
-
-
-
-
- public virtual Expression<Func<TDbObject, bool>> GetGlobalSelectPredicate()
- {
- return x => true;
- }
-
-
-
-
-
- public static Expression<Func<TDbObject, bool>> GetGlobalSelectPredicateStatic()
- {
- var obj = new TThis();
- return obj.GetGlobalSelectPredicate();
- }
-
-
-
-
-
-
- public static TDboGet DbToDboGetStatic(TDbObject res)
- {
- var obj = new TThis();
- return obj.DbToDboGet(res);
- }
-
-
-
-
-
-
-
- public static OpResult<bool> DboAddToDbStatic(TDboAdd res, TDbObject add)
- {
- var obj = new TThis();
- return obj.DboAddToDb(res, add);
- }
-
-
-
-
-
-
-
- public static OpResult<bool> DboEditToDbStatic(TDboEdit res, TDbObject edit)
- {
- var obj = new TThis();
- return obj.DboEditToDb(res, edit);
- }
-
-
-
-
-
-
-
- public static OpResult<TDbObject> GetSingleDbObject(DbSet<TDbObject> table, Expression<Func<TDbObject, bool>> predicate)
- {
- var o = table.Where(GetGlobalSelectPredicateStatic()).FirstOrDefault(predicate);
- if (o == null)
- {
- return OpResult<TDbObject>.Error(ResultStatus.NotFound, typeof(TThis).Name + ": Value not found", "");
- }
- return OpResult<TDbObject>.Ok(o);
- }
-
-
-
-
-
-
- public static OpResult<TDboGet> GetSingle(Expression<Func<TDbObject, bool>> predicate)
- {
- return Execute((db, table) =>
- {
- var obj = GetSingleDbObject(table, predicate);
- if (!obj)
- return obj.To<TDboGet>();
- return OpResult<TDboGet>.Ok(DbToDboGetStatic(obj.Data));
- });
- }
-
-
-
-
-
-
- public static OpResult<TDboGet> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
- {
- return GetSingle(GetExpression(keys));
- }
-
-
-
-
-
-
- public static OpResult<TDboGet> GetSingleById(long id)
- {
- return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
- }
-
-
-
-
-
-
-
-
-
- public static OpResult<DboGetMultiple<TDboGet>> GetMultiple<TKey>(Expression<Func<TDbObject, bool>> predicate,
- Expression<Func<TDbObject, TKey>> orderBy, int page = 0, int perPage = Int32.MaxValue)
- {
- return Execute((db, table) =>
- {
- var globalPredicate = GetGlobalSelectPredicateStatic();
- var count = table.Where(globalPredicate).Count(predicate);
- var results = table.OrderBy(orderBy).Where(globalPredicate).Where(predicate)
- .Skip(page * perPage).Take(perPage).Select(DbToDboGetStatic).ToList();
- var result = new DboGetMultiple<TDboGet>
- {
- Count = count,
- Data = results
- };
- return OpResult<DboGetMultiple<TDboGet>>.Ok(result);
- });
- }
-
-
-
-
-
-
-
-
-
- public static OpResult<DboGetMultiple<TDboGet>> GetMultipleByKeys<TKey>(Expression<Func<TDbObject, TKey>> orderBy,
- int page = 0, int perPage = Int32.MaxValue, params KeyValuePair<string, object>[] keys)
- {
- return GetMultiple(GetExpression(keys), orderBy, page, perPage);
- }
-
-
-
-
-
-
-
- public static OpResult<T> Add<T>(TDboAdd obj, Expression<Func<TDbObject, T>> returnFunc)
- {
- return Execute((db, table) =>
- {
- var add = table.Create();
- var item = DboAddToDbStatic(obj, add);
- if (!item)
- return item.To<T>();
- table.Add(add);
- db.SaveChanges();
- var res = returnFunc.Compile().DynamicInvoke(add);
- return OpResult<T>.Ok((T)res);
- });
- }
-
-
-
-
-
-
- public static OpResult<bool> Add(TDboAdd obj)
- {
- return Add(obj, x => true);
- }
-
-
-
-
-
-
- public static OpResult<TDboGet> AddDbo(TDboAdd obj)
- {
- return Add(obj, x => DbToDboGetStatic(x));
- }
-
-
-
-
-
-
- public static OpResult<long> AddId(TDboAdd obj)
- {
- var param = Expression.Parameter(typeof(TDbObject), "x");
- var exp = Expression.Property(param, "id");
- var lambda = Expression.Lambda<Func<TDbObject, long>>(exp, param);
- return Add(obj, lambda);
- }
-
-
-
-
-
-
-
- public static OpResult<bool> Edit(TDboEdit obj, Expression<Func<TDbObject, bool>> predicate)
- {
- return Execute((db, table) =>
- {
- var edit = GetSingleDbObject(table, predicate);
- if (!edit)
- return edit.To<bool>();
- var res = DboEditToDbStatic(obj, edit.Data);
- if (!res)
- return res.To<bool>();
- db.Entry(edit.Data).CurrentValues.SetValues(edit.Data);
- db.SaveChanges();
- return OpResult<bool>.Ok(true);
- });
- }
-
-
-
-
-
-
-
- public static OpResult<bool> Edit(TDboEdit obj, params KeyValuePair<string, object>[] keys)
- {
- return Edit(obj, GetExpression(keys));
- }
-
-
-
-
-
-
-
- public static OpResult<bool> EditById(TDboEdit obj, long id)
- {
- return Edit(obj, new KeyValuePair<string, object>("id", id));
- }
-
-
-
-
-
-
- public static OpResult<bool> Delete(Expression<Func<TDbObject, bool>> predicate)
- {
- return Execute((db, table) =>
- {
- var del = GetSingleDbObject(table, predicate);
- if (!del)
- return del.To<bool>();
- table.Remove(del.Data);
- db.SaveChanges();
- return OpResult<bool>.Ok(true);
- });
- }
-
-
-
-
-
-
- public static OpResult<bool> Delete(params KeyValuePair<string, object>[] keys)
- {
- return Delete(GetExpression(keys));
- }
-
-
-
-
-
-
- public static OpResult<bool> DeleteById(long id)
- {
- return Delete(new KeyValuePair<string, object>("id", id));
- }
- }
- }
|