ADO.NET Entity Framework多庫架構初探(一)
阿新 • • 發佈:2019-02-20
底層架構如上
Model
1.Model.UC Model.Stock Model.PSS,是對應的三個庫,edmx檔案的建立就不說了
2.Model.Comm是和前臺互動的josn類 內容比較簡單
public class JsonList<T> where T : class { public long count { get; set; } public IList<T> root { get; set; } } public class JsonResult { public bool result { get; set; } public string message { get; set; } }
BLL
1.BLL.Base 邏輯管理的基類,實現一些常用的方法
ManageBase.cs
public abstract class JsonManager<T> where T : class { public abstract ObjectContext DB { get; } #region getlist過載 public virtual JsonList<T> getlist(int s, int l) { return getlist(s, l, null, null); } public virtual JsonList<T> getlist(int s, int l, string sorts) { return getlist(s, l, null, sorts); } public virtual JsonList<T> getlist(int s, int l, Expression<Func<T, bool>> where) { return getlist(s, l, where,null); } #endregion public virtual JsonList<T> getlist(int s, int l, Expression<Func<T, bool>> where, string sorts) { JsonList<T> ret = new JsonList<T>(); //var query = DB.CreateObjectSet<T>().AsQueryable(); var query = DB.CreateObjectSet<T>().AsEnumerable(); //query.Expression = DynamicExpression Sort[] sort; #region 解析sort if (string.IsNullOrEmpty(sorts)) sort = new Sort[] { new Sort() { property = "id", direction = "DESC" } }; else { sort = JsonConvert.DeserializeObject<Sort[]>(sorts); } #endregion foreach (Sort st in sort) { //query = query.OrderBy(st.property + " " + st.direction); if(st.direction.ToLower()=="asc") query = query.OrderBy(a => typeof(T).GetProperty(st.property)); else query = query.OrderByDescending(a => typeof(T).GetProperty(st.property)); } if (where != null) query = query.Where(where.Compile()); ret.count = query.LongCount(); query = query.Skip(s).Take(l); ret.root = query.ToList(); return ret; } public virtual JsonResult Save(T o) { JsonResult ret = new JsonResult() { result = true }; try { ObjectSet<T> oset = DB.CreateObjectSet<T>(); oset.AddObject(o); oset.Context.SaveChanges(); ret.message = o.GetType().GetProperty("id").GetValue(o, null).ToString(); } catch (Exception e) { ret.result = false; ret.message = e.Message; } return ret; } public virtual JsonResult Delte(IList<int> ids) { JsonResult ret = new JsonResult() { result = true }; try { ObjectSet<T> o = DB.CreateObjectSet<T>(); foreach(int i in ids) { o.DeleteObject(o.Where("id==@0", i).Single()); } o.Context.SaveChanges(); } catch (Exception e) { ret.result = false; ret.message = e.Message; } return ret; } public virtual T GetObjeteByID(int id) { T ret = null; try { ret = DB.CreateObjectSet<T>().Where("id==@0", id).Single(); } catch { } return ret; }
PredicateBuilder.cs
public static class PredicateBuilder { public static Expression<Func<T, bool>> True<T>() { return f => true; } public static Expression<Func<T, bool>> False<T>() { return f => false; } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters); } }
2 BLL.PSS
public abstract class ManagerBase<T> : xsuda.BLL.Base.JsonManager<T> where T : class
{
public override ObjectContext DB { get { return new PSSEntities(new PSSConn().conn); } }
public JsonList<T> GetObjectList(int start, int limit, int accountid, string sort)
{
var query = PredicateBuilder.True<T>();
query = query.And(DynamicExpression.ParseLambda<T,bool>("accountid == @0",accountid));
//query = query.And<T>(o => (int)typeof(T).GetProperties().Where(x=> x.Name == "accountid").First().GetValue(o,null) == accountid);
return base.getlist(start, limit, query, sort);
}
}
至此所有PSS下的管理類繼承ManagerBase即可,如下
public class ManageProduct:ManagerBase<tbProduct>
{
}
BLL結構結束
應用層可直接
new ManageProduct().GetObjeteByID(1)
如果有其他方法,可以根據適用情況新增到各管理類中
product獨有方法新增到 ManageProduct
PSS中各個類公用方法新增到 ManagerBase<T>
各個庫公用方法新增到JsonManager<T>