标签:
/* * author:iGo * for-free * last-update:2015年7月7日 * auth:mit * hope it will be useful to you * */ using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.Objects; using System.Data.Objects.DataClasses; using System.Linq; using System.Linq.Expressions; using System.Net.Configuration; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Remoting.Contexts; using System.Text; using DBContext; namespace DBContext { /// <summary> /// static data repo,you donot need to reference entity framework /// </summary> public class SimpleDataRepo { public static void Add<T>(T model) where T : IEntityWithKey { if (model == null) return; using (var context = DataContext.Context) { context.AddObject(typeof(T).Name, model); context.SaveChanges(); } } public static void Edit<T>(T model) where T : class { if (model == null) return; using (var context = DataContext.Context) { context.AddObject(model.GetType().Name, model); context.ObjectStateManager.ChangeObjectState(model, EntityState.Modified); context.SaveChanges(); } } public static void Delete<T>(T model) where T : IEntityWithKey { if (model == null) return; using (var context = DataContext.Context) { context.Attach(model); context.DeleteObject(model); context.SaveChanges(); } } [NotUsed("not used due to linq function unsupported!")] private static int GetPropertyIdValueFromModel<T>(T model) { return (int)model.GetType().GetProperty("id").GetValue(model, null); } public static IList<T> GetAll<T>(Expression<Func<T, bool>> condition = null) { using (var context = DataContext.Context) { var query = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>; return query != null ? query.ToList() : null; } } /// <summary> /// Get first model that satisfies the specified condition,GetById can be implied here,ie:a=>a.Id=5 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition"></param> /// <returns></returns> public static T QueryFirst<T>(Expression<Func<T, bool>> condition) { using (var context = DataContext.Context) { var query = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>; if (query == null) return default(T); return query.FirstOrDefault(condition); } } public static IList<T> QuerySort<T, TS>(Expression<Func<T, bool>> condition, Expression<Func<T, TS>> sortExpression = null, bool isDecending = false) { using (var context = DataContext.Context) { var query = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>; if (query == null) return null; query = query.Where(condition).Where(condition); if (sortExpression != null) query = isDecending ? query.OrderByDescending(sortExpression) : query.OrderBy(sortExpression); return query.ToList(); } } } }
2.context factory:
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Text; namespace DBContext { public partial class DataContext { public static GpsDataContext Context { get { return new GpsDataContext(); } } } }
写一个system.data.entity的simpledatarepo,实现crudq这些功能,不需要引入entityframework,直接可以使用,用到objectset
标签:
原文地址:http://www.cnblogs.com/hualiu0/p/4627726.html