示例#1
0
        public static void UpdateEntityViewWithChanges(IUpdateEntityViewWithChanges <TView> msg)
        {
            try
            {
                var exp = FindExpressionClass.FindExpression <TDbEntity, TDbView>();
                using (var ctx = new TDbContext())
                {
                    // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault cuz EF7 bugging LEAVE JUST SO
                    TDbEntity res;//
                    if (msg.EntityId == 0)
                    {
                        res = new TDbEntity();
                        ctx.Set <TDbEntity>().Add(res);
                    }
                    else
                    {
                        res = ctx.Set <TDbEntity>().FirstOrDefault(x => x.Id == msg.EntityId);
                    }

                    res.ApplyChanges(msg.Changes);
                    ctx.SaveChanges(true);
                    //TODO: retrieve whole item
                    var ures = ctx.Set <TDbEntity>().Select(exp).FirstOrDefault(x => x.Id == res.Id);//
                    EventMessageBus.Current.Publish(new EntityViewWithChangesUpdated <TView>((TView)(object)ures, msg.Changes, new StateEventInfo(msg.Process.Id, EntityView.Events.EntityViewFound), msg.Process, Source), Source);
                }
            }
            catch (Exception ex)
            {
                PublishProcesError(msg, ex, typeof(IEntityViewWithChangesUpdated <TView>));
            }
        }
示例#2
0
 public static void GetEntityViewWithChanges(IGetEntityViewWithChanges <TView> msg)
 {
     try
     {
         var exp = FindExpressionClass.FindExpression <TDbEntity, TDbView>();
         using (var ctx = new TDbContext())
         {
             // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault cuz EF7 bugging LEAVE JUST SO
             var whereStr = msg.Changes.Aggregate("", (str, itm) => str + ($"{itm.Key} == \"{itm.Value}\" &&"));
             whereStr = whereStr.TrimEnd('&');
             if (string.IsNullOrEmpty(whereStr))
             {
                 return;
             }
             var res = ctx.Set <TDbEntity>().AsNoTracking().Select(exp).Where(whereStr).FirstOrDefault();//
             if (res != null)
             {
                 EventMessageBus.Current.Publish(new EntityViewWithChangesFound <TView>((TView)(object)res, msg.Changes, new StateEventInfo(msg.Process.Id, EntityView.Events.EntityViewFound), msg.Process, Source), Source);
             }
             else
             {
                 // not found
             }
         }
     }
     catch (Exception ex)
     {
         PublishProcesError(msg, ex, typeof(IEntityViewWithChangesFound <TView>));
     }
 }
示例#3
0
        public static void LoadEntityViewSetWithChanges(ILoadEntityViewSetWithChanges <TView, IMatchType> msg)
        {
            try
            {
                var exp = FindExpressionClass.FindExpression <TDbEntity, TDbView>();
                using (var ctx = new TDbContext())
                {
                    // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault cuz EF7 bugging LEAVE JUST SO
                    var matchtype = msg.GetType().GenericTypeArguments[1];
                    var whereStr  = msg.Changes.Aggregate("", IMatchTypeFunctions[matchtype]);
                    whereStr = whereStr.TrimEnd('&');
                    IQueryable <TDbView> res;
                    res = string.IsNullOrEmpty(whereStr)
                        ? ctx.Set <TDbEntity>().OrderByDescending(x => x.Id).AsNoTracking().Select(exp)
                        : ctx.Set <TDbEntity>().OrderByDescending(x => x.Id).AsNoTracking().Select(exp).Where(whereStr);


                    EventMessageBus.Current.Publish(new EntityViewSetWithChangesLoaded <TView>(res.Select(x => (TView)(object)x).ToList(), msg.Changes, new StateEventInfo(msg.Process.Id, EntityView.Events.EntityViewFound), msg.Process, Source), Source);
                }
            }
            catch (Exception ex)
            {
                PublishProcesError(msg, ex, typeof(IEntityViewLoaded <TView>));
            }
        }
示例#4
0
        public static void GetEntityViewById(IGetEntityViewById <TView> msg)
        {
            try
            {
                var exp = FindExpressionClass.FindExpression <TDbEntity, TDbView>();
                using (var ctx = new TDbContext())
                {
                    // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault cuz EF7 bugging LEAVE JUST SO
                    var res = ctx.Set <TDbEntity>().AsNoTracking().Select(exp).FirstOrDefault(x => x.Id == msg.EntityId);//

                    EventMessageBus.Current.Publish(new EntityFound <TView>((TView)(object)res, new StateEventInfo(msg.Process.Id, EntityView.Events.EntityViewFound), msg.Process, Source), Source);
                }
            }
            catch (Exception ex)
            {
                PublishProcesError(msg, ex, typeof(IEntityFound <TView>));
            }
        }