示例#1
0
        public virtual bool DeleteBatch(Expression <Func <T, bool> > predicate, BatchEntityEventHandler <T> batchDeletingHandler = null, BatchEntityEventHandler <T> batchDeletedHandler = null)
        {
            var query = repository.AsQueryable <T>();

            if (predicate != null)
            {
                query = query.Where(predicate);
            }
            var objs = query.AsEnumerable();

            objs = this.HandleBatchEvents(events, es => es.BatchDeletingHandler, DataOperation.Delete, objs);
            objs = this.HandleBatchEvent(batchDeletingHandler, DataOperation.Delete, objs);

            long totals = 0;

            foreach (var obj in objs)
            {
                var ret = repository.Remove(GetKeyQuery(obj));
                totals += ret.DocumentsAffected;
            }

            if (totals > 0)
            {
                this.HandleBatchEvent(batchDeletedHandler, DataOperation.Delete, objs);
                this.HandleBatchEvents(events, es => es.BatchDeletedHandler, DataOperation.Delete, objs);
            }

            return(totals > 0);
        }
示例#2
0
        public virtual bool DeleteBatch(Expression <Func <T, bool> > predicate, BatchEntityEventHandler <T> batchDeletingHandler = null, BatchEntityEventHandler <T> batchDeletedHandler = null)
        {
            bool hasHandle = events != null && events.Any(e => e.CanHandle()) || batchDeletingHandler != null || batchDeletedHandler != null;
            int  totals    = 0;

            if (hasHandle)
            {
                var objs = db.GetTable <U>().Where(predicate).AsEnumerable();
                objs = this.HandleBatchEvents(events, es => es.BatchDeletingHandler, DataOperation.Delete, objs);
                objs = this.HandleBatchEvent(batchDeletingHandler, DataOperation.Delete, objs);

                foreach (var obj in objs)
                {
                    U entity = new U();
                    entity  = mapper.Map <T, U>(obj, () => entity);
                    totals += db.Delete <U>(entity);
                }
                if (totals > 0)
                {
                    this.HandleBatchEvent(batchDeletedHandler, DataOperation.Delete, objs);
                    this.HandleBatchEvents(events, es => es.BatchDeletedHandler, DataOperation.Delete, objs);
                }
            }
            else
            {
                totals = db.GetTable <U>().Delete(predicate);
            }

            return(totals > 0);
        }
示例#3
0
        public virtual bool DeleteBatch(Expression <Func <T, bool> > predicate, BatchEntityEventHandler <T> batchDeletingHandler = null, BatchEntityEventHandler <T> batchDeletedHandler = null)
        {
            var            tbl   = db.Set <U>();
            IQueryable <T> query = tbl;

            if (predicate != null)
            {
                query = query.Where(predicate);
            }
            var objs = query.AsEnumerable();

            objs = this.HandleBatchEvents(events, es => es.BatchDeletingHandler, DataOperation.Delete, objs);
            objs = this.HandleBatchEvent(batchDeletingHandler, DataOperation.Delete, objs);

            foreach (var entity in objs)
            {
                tbl.Remove(entity as U);
            }

            int ret = db.SaveChanges();

            if (ret > 0)
            {
                this.HandleBatchEvent(batchDeletedHandler, DataOperation.Delete, objs);
                this.HandleBatchEvents(events, es => es.BatchDeletedHandler, DataOperation.Delete, objs);
            }

            return(ret > 0);
        }
示例#4
0
        public virtual bool InsertBatch(IEnumerable <T> objs, BatchEntityEventHandler <T> batchInsertingHandler = null, BatchEntityEventHandler <T> batchInsertedHandler = null)
        {
            objs = this.HandleBatchEvents(events, es => es.BatchInsertingHandler, DataOperation.Insert, objs);
            objs = this.HandleBatchEvent(batchInsertingHandler, DataOperation.Insert, objs);

            var ret = repository.InsertBatch(objs);

            if (ret != null)
            {
                this.HandleBatchEvent(batchInsertedHandler, DataOperation.Insert, objs);
                this.HandleBatchEvents(events, es => es.BatchInsertedHandler, DataOperation.Insert, objs);
            }

            return(ret != null);
        }
示例#5
0
        public virtual bool InsertBatch(IEnumerable <T> objs, BatchEntityEventHandler <T> batchInsertingHandler = null, BatchEntityEventHandler <T> batchInsertedHandler = null)
        {
            objs = this.HandleBatchEvents(events, es => es.BatchInsertingHandler, DataOperation.Insert, objs);
            objs = this.HandleBatchEvent(batchInsertingHandler, DataOperation.Insert, objs);

            int totals = 0;

            foreach (var obj in objs)
            {
                totals += db.Insert(obj);
            }

            if (totals > 0)
            {
                this.HandleBatchEvent(batchInsertedHandler, DataOperation.Insert, objs);
                this.HandleBatchEvents(events, es => es.BatchInsertedHandler, DataOperation.Insert, objs);
            }

            return(totals > 0);
        }
示例#6
0
        public virtual bool InsertBatch(IEnumerable <T> objs, BatchEntityEventHandler <T> batchInsertingHandler = null, BatchEntityEventHandler <T> batchInsertedHandler = null)
        {
            objs = this.HandleBatchEvents(events, es => es.BatchInsertingHandler, DataOperation.Insert, objs);
            objs = this.HandleBatchEvent(batchInsertingHandler, DataOperation.Insert, objs);

            var tbl = db.Set <T>();

            foreach (var obj in objs)
            {
                tbl.Add(obj);
            }

            int totals = db.SaveChanges();

            if (totals > 0)
            {
                this.HandleBatchEvent(batchInsertedHandler, DataOperation.Insert, objs);
                this.HandleBatchEvents(events, es => es.BatchInsertedHandler, DataOperation.Insert, objs);
            }

            return(totals > 0);
        }
示例#7
0
        public virtual bool InsertBatch(IEnumerable <T> objs, BatchEntityEventHandler <T> batchInsertingHandler = null, BatchEntityEventHandler <T> batchInsertedHandler = null)
        {
            objs = this.HandleBatchEvents(events, es => es.BatchInsertingHandler, DataOperation.Insert, objs);
            objs = this.HandleBatchEvent(batchInsertingHandler, DataOperation.Insert, objs);

            int totals = 0;

            foreach (var obj in objs)
            {
                U entity = new U();
                entity = mapper.Map <T, U>(obj, () => entity);
                var ret = repository.Insert <U>(entity);
                totals += (int)ret.DocumentsAffected;
            }

            if (totals > 0)
            {
                this.HandleBatchEvent(batchInsertedHandler, DataOperation.Insert, objs);
                this.HandleBatchEvents(events, es => es.BatchInsertedHandler, DataOperation.Insert, objs);
            }

            return(totals > 0);
        }
示例#8
0
        public virtual bool UpdateBatch(Expression <Func <T, bool> > predicate, Expression <Func <T, T> > setter, BatchEntityEventHandler <T> batchUpdatingHandler = null, BatchEntityEventHandler <T> batchUpdatedHandler = null)
        {
            var query = repository.AsQueryable <T>();

            if (predicate != null)
            {
                query = query.Where(predicate);
            }
            var objs = query.AsEnumerable();

            objs = this.HandleBatchEvents(events, es => es.BatchUpdatingHandler, DataOperation.Update, objs);
            objs = this.HandleBatchEvent(batchUpdatingHandler, DataOperation.Update, objs);

            long totals = 0;
            var  func   = setter.Compile();

            foreach (var obj in objs)
            {
                var nobj = func(obj);
                var ret  = repository.Save(nobj);
                totals += ret.DocumentsAffected;
            }

            if (totals > 0)
            {
                this.HandleBatchEvent(batchUpdatedHandler, DataOperation.Update, objs);
                this.HandleBatchEvents(events, es => es.BatchUpdatedHandler, DataOperation.Update, objs);
            }

            return(totals > 0);
        }
示例#9
0
        public static IEnumerable <T> HandleBatchEvent <T>(this IRepository <T> repository, BatchEntityEventHandler <T> handler, DataOperation action, IEnumerable <T> objs)
            where T : class
        {
            if (handler == null)
            {
                return(objs);
            }
            var args = new EntitiesEventArgs <T>()
            {
                Entities = objs, Action = action, Count = objs.Count(), Repository = repository
            };

            handler(args);
            return(args.Entities);
        }
示例#10
0
        public virtual bool UpdateBatch(Expression <Func <T, bool> > predicate, Expression <Func <T, T> > setter, BatchEntityEventHandler <T> batchUpdatingHandler = null, BatchEntityEventHandler <T> batchUpdatedHandler = null)
        {
            IQueryable <T> query = db.Set <U>();

            if (predicate != null)
            {
                query = query.Where(predicate);
            }
            var objs = query.AsEnumerable();

            objs = this.HandleBatchEvents(events, es => es.BatchUpdatingHandler, DataOperation.Update, objs);
            objs = this.HandleBatchEvent(batchUpdatingHandler, DataOperation.Update, objs);

            var func = setter.Compile();

            foreach (var entity in objs)
            {
                func(entity);
            }

            int ret = db.SaveChanges();

            if (ret > 0)
            {
                this.HandleBatchEvent(batchUpdatedHandler, DataOperation.Update, objs);
                this.HandleBatchEvents(events, es => es.BatchUpdatedHandler, DataOperation.Update, objs);
            }

            return(ret > 0);
        }
示例#11
0
        public virtual bool UpdateBatch(Expression <Func <T, bool> > predicate, Expression <Func <T, T> > setter, BatchEntityEventHandler <T> batchUpdatingHandler = null, BatchEntityEventHandler <T> batchUpdatedHandler = null)
        {
            bool hasHandle = events != null && events.Any(e => e.CanHandle()) || batchUpdatingHandler != null || batchUpdatedHandler != null;
            int  totals    = 0;

            if (hasHandle)
            {
                var objs = db.GetTable <T>().Where(predicate).AsEnumerable();
                objs = this.HandleBatchEvents(events, es => es.BatchUpdatingHandler, DataOperation.Update, objs);
                objs = this.HandleBatchEvent(batchUpdatingHandler, DataOperation.Update, objs);

                var func = setter.Compile();
                foreach (var obj in objs)
                {
                    var nobj = func(obj);
                    totals += db.Update <T>(obj);
                }
                if (totals > 0)
                {
                    this.HandleBatchEvent(batchUpdatedHandler, DataOperation.Update, objs);
                    this.HandleBatchEvents(events, es => es.BatchUpdatedHandler, DataOperation.Update, objs);
                }
            }
            else
            {
                totals = db.GetTable <T>().Update(predicate, setter);
            }

            return(totals > 0);
        }