码迷,mamicode.com
首页 > 其他好文 > 详细

写一个system.data.entity的simpledatarepo,实现crudq这些功能,不需要引入entityframework,直接可以使用,用到objectset

时间:2015-07-07 19:10:00      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

note:you can delete reference of entityframework when using this classes.it`s just a simple repohelper.the code below can also include a getpagedlist method when paging.
have fun,it`s simple,just create edmx file from database.all one sentence.but when using iqueryable or transaction,you need to use context factory and develop your own methods.
i am using it now.very simple.just like a static modelhelper orm.
1.simpledaterepo:
/*
 * 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!