123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Linq.Expressions;
- using iiie.Logs.DataAccess;
- using iiie.Logs.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 TDbObject DboAddToDb(TDboAdd obj);
-
-
-
-
-
-
-
- public abstract TDbObject DboEditToDb(TDboEdit obj, TDbObject edit);
-
-
-
-
-
-
- public static TDboGet DbToDboGetStatic(TDbObject res)
- {
- var obj = new TThis();
- return obj.DbToDboGet(res);
- }
-
-
-
-
-
-
- public static TDbObject DboAddToDbStatic(TDboAdd res)
- {
- var obj = new TThis();
- return obj.DboAddToDb(res);
- }
-
-
-
-
-
-
-
- public static TDbObject 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.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<IEnumerable<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 results = table.OrderBy(orderBy).Where(predicate).Skip(page * perPage).Take(perPage).ToList();
- return OpResult<IEnumerable<TDboGet>>.Ok(results.Select(DbToDboGetStatic));
- });
- }
-
-
-
-
-
-
-
-
-
- public static OpResult<IEnumerable<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<bool> Add(TDboAdd obj)
- {
- return Execute((db, table) =>
- {
- table.Add(DboAddToDbStatic(obj));
- db.SaveChanges();
- return OpResult<bool>.Ok(true);
- });
- }
-
-
-
-
-
-
-
- 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);
- db.Entry(edit.Data).CurrentValues.SetValues(res);
- 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));
- }
- }
- }
|