Beispiel #1
0
        /// <summary>
        /// Deactivate all models that have been "resolved", i.e. date range adjusted, or inactivated.
        /// Add new records for "resolved" rows. Add the new record that hit all the conflicts to begin with.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <param name="useWriteDb"></param>
        /// <param name="isUpdateOnly"></param>
        /// <returns></returns>
        public static IEnumerable <T> SaveConflictResolution <T>(IEnumerable <T> models, bool useWriteDb = false, bool isUpdateOnly = false) where T : BaseDateRangeModel
        {
            var modelsList = models.ToList();

            // Can't premptive check here because some BO's have extended matching criteria that would get ignored.
            // Do it in BO layer. See CopayRuleBO.GetConflicts for example.
            //var hasPendingConflicts = GetConflicts<T>(modelsList).Any();
            //if (hasPendingConflicts)
            //{
            //    throw new Exception("Cannot save this resolution because some conflicts remain.");
            //}

            using (var db = new RxSenseDb(useWriteDb))
            {
                foreach (var model in modelsList)
                {
                    if (!isUpdateOnly && model.IDateRangeId == 0) //add new model only if isUpdateOnly is false.
                    {
                        db.Set <T>().Add(model);
                        continue;
                    }
                    var dbModel = db.Set <T>().Find(model.IDateRangeId);
                    if (dbModel != null &&                          //found a dbModel
                        (dbModel.StartDate != model.StartDate ||    //and there is some edit to this dbModel
                         dbModel.EndDate != model.EndDate ||
                         dbModel.IsActive != model.IsActive))
                    {
                        //deactivate, regardless of if this is a deactivation or a date adjustment
                        //inactives stay inactive and keep deactivated date as is
                        dbModel.IsActive        = false;
                        dbModel.DeactivatedDate = dbModel.DeactivatedDate ?? DateTime.UtcNow;
                        if (model.IsActive &&                        //not a deactivate; maybe a "reactivate", still yields new record
                            (dbModel.StartDate != model.StartDate || //or is an effDateRange adjustment
                             dbModel.EndDate != model.EndDate))
                        {
                            db.Set <T>().Add(model);
                        }
                    }
                }

                db.SaveChanges();
            }
            //Set DeactivatedDate and ModifiedDate for return
            foreach (var mdl in modelsList.Where(x => x.IsActive == false))
            {
                mdl.DeactivatedDate = mdl.DeactivatedDate ?? DateTime.UtcNow;
                mdl.ModifiedDate    = DateTime.UtcNow;
            }
            return(modelsList);
        }
Beispiel #2
0
        public static bool Contains <T>(Func <T, string> property, string value) where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                var items = db.Set <T>().Select(property);

                return(items.Contains(value, StringComparer.OrdinalIgnoreCase));
            }
        }
Beispiel #3
0
        public static List <T> GetAll <T>() where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                var results = db.Set <T>().ToList();

                return(results);
            }
        }
Beispiel #4
0
        public static async Task <T> GetAsync <T>(int id) where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                var item = await db.Set <T>().FindAsync(id);

                return(item);
            }
        }
Beispiel #5
0
        public static T Get <T>(int id) where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                var item = db.Set <T>().Find(id);

                return(item);
            }
        }
Beispiel #6
0
 public static int Count <T>(
     Expression <Func <T, bool> > predicate,
     bool useWriteDb = false)
     where T : BaseModel
 {
     using (var db = new RxSenseDb(useWriteDb))
     {
         var dbSet = db.Set <T>().AsQueryable();
         return(dbSet.Where(predicate).Count());
     }
 }
Beispiel #7
0
        public static async Task <T> AddOrUpdateAsync <T>(T model, bool useWritedb = true) where T : BaseModel
        {
            using (var db = new RxSenseDb(useWritedb))
            {
                db.Set <T>().AddOrUpdate(model);

                await db.SaveChangesAsync();

                return(model);
            }
        }
Beispiel #8
0
        public static async Task <T> AddAsync <T>(
            T model) where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                db.Set <T>().Add(model);
                await db.SaveChangesAsync();

                return(model);
            }
        }
Beispiel #9
0
        /// <summary>
        /// Given an EffDated model, finds all DB records
        /// that satisfy matching criteria, per EffectiveDateMatchingCriteria
        /// (i.e. finda all "versions" of this record).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="filter"></param>
        /// <param name="useWriteDb"></param>
        /// <returns></returns>
        public static IEnumerable <T> GetEffectiveMatches <T>(T model,
                                                              bool useWriteDb = false,
                                                              Expression <Func <T, bool> > filter = null,
                                                              params Expression <Func <T, object> >[] includes)
            where T : BaseDateRangeModel
        {
            var type             = typeof(T); //model.GetType();
            var tablettribute    = (TableAttribute)Attribute.GetCustomAttribute(type, typeof(TableAttribute));
            var matchingCriteria = EffectiveDateMatchingCriteria[tablettribute.Name];

            var predicate = PredicateBuilder.True <T>();

            foreach (var matchingCriterion in matchingCriteria)
            {
                var prop = type.GetProperty(matchingCriterion);

                if (prop == null)
                {
                    throw new Exception("EffectiveDateMatchingCriteria lookup failed.");
                }

                var propVal = prop.GetValue(model);
                var param   = Expression.Parameter(typeof(T), "x");         //"x =>"
                var expr    = Expression.Lambda <Func <T, bool> >(          //x.param
                    Expression.Equal(                                       // ==
                        Expression.Property(param, matchingCriterion),      //propVal
                        Expression.Convert(Expression.Constant(propVal),
                                           prop.PropertyType)),
                    param);

                predicate = predicate.And(expr);
            }

            if (filter != null)
            {
                predicate = predicate.And(filter);
            }

            using (var db = new RxSenseDb(useWriteDb))
            {
                var query = db.Set <T>()
                            .AsQueryable();

                foreach (var include in includes)
                {
                    query.Include(include);
                }

                return(query
                       .Where(predicate)
                       .ToList()); //concrete the results since this db context will be disposed
            }
        }
Beispiel #10
0
        public static async Task <T> UpdateAsync <T>(T model) where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                db.Set <T>().Attach(model);
                db.Entry(model).State = EntityState.Modified;

                await db.SaveChangesAsync();

                return(model);
            }
        }
Beispiel #11
0
 public static List <T> AddRange <T>(List <T> models) where T : BaseModel
 {
     if (models?.Count > 0)
     {
         using (var db = new RxSenseDb())
         {
             db.Set <T>().AddRange(models);
             db.SaveChanges();
         }
     }
     return(models);
 }
Beispiel #12
0
        public static T AddOrUpdate <T>
            (T model,
            bool useWriteDb = false)
            where T : BaseModel
        {
            using (var db = new RxSenseDb(useWriteDb))
            {
                db.Set <T>().AddOrUpdate(model);

                db.SaveChanges();

                return(model);
            }
        }
Beispiel #13
0
        public static T Update <T>(
            T model,
            bool useWriteDb = false)
            where T : BaseModel
        {
            using (var db = new RxSenseDb(useWriteDb))
            {
                db.Set <T>().Attach(model);
                db.Entry(model).State = EntityState.Modified;

                db.SaveChanges();

                return(model);
            }
        }
Beispiel #14
0
        public static async Task <T> GetAsync <T>(Expression <Func <T, bool> > predicate, params Expression <Func <T, object> >[] includes) where T : BaseModel
        {
            using (var db = new RxSenseDb())
            {
                var dbSet = db.Set <T>().AsQueryable();

                foreach (var property in includes)
                {
                    dbSet = dbSet.Include(property);
                }

                var result = await dbSet.SingleOrDefaultAsync(predicate);

                return(result);
            }
        }
Beispiel #15
0
        public static List <T> Query <T>(
            Expression <Func <T, bool> > predicate,
            bool useWriteDb = false,
            params Expression <Func <T, object> >[] includes)
            where T : BaseModel
        {
            using (var db = new RxSenseDb(useWriteDb))
            {
                var dbSet = db.Set <T>().AsQueryable();

                foreach (var property in includes)
                {
                    dbSet = dbSet.Include(property);
                }

                return(dbSet.Where(predicate).ToList());
            }
        }
Beispiel #16
0
        /// <summary>
        /// Given a T model, find Db record based on EffectiveDateMatchingCriteria
        /// and IDateRangeId (PK Id), and set record to inactive if not already so.
        /// Also record DeactivatedDate as UtcNow.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="useWriteDb"></param>
        /// <returns></returns>
        public static T Deactivate <T>(T model, bool useWriteDb = false) where T : BaseDateRangeModel
        {
            using (var db = new RxSenseDb(useWriteDb))
            {
                var matches = GetEffectiveMatches(model);
                var dbModel = matches.FirstOrDefault(x => x.IDateRangeId == model.IDateRangeId);
                if (dbModel != null)
                {
                    db.Set <T>().Attach(dbModel);    //because GetEffectiveMatches db context is out of scope
                    dbModel.IsActive        = false;
                    dbModel.DeactivatedDate = DateTime.UtcNow;

                    db.SaveChanges();
                }

                return(dbModel);
            }
        }