Esempio n. 1
0
        public static T SingleWithCache <T>(Expression <Func <T, bool> > predictate) where T : class, IEntityClass
        {
            var ci = GetFromCache(predictate);

            if (ci != null)
            {
                return(ci);
            }

            using (var workspace = WorkspaceFactory.CreateReadOnly())
            {
                var result = workspace.Single(predictate);
                AddToCache(result);
                return(result);
            }
        }
Esempio n. 2
0
 public static void SafeSave <T>(T entity) where T : class, IEntityClass
 {
     using (var w = WorkspaceFactory.Create())
     {
         AddEntities(entity, w, entity.Id);
         if (entity.Id == 0)
         {
             w.Add(entity);
         }
         else
         {
             var currentItem = w.Single <T>(x => x.Id == entity.Id);
             currentItem.InjectFrom <EntityInjection>(entity);
         }
         w.CommitChanges();
     }
 }
Esempio n. 3
0
        public static T SingleWithCache <T>(Expression <Func <T, bool> > predictate, params Expression <Func <T, object> >[] includes) where T : class, IEntity
        {
            var lpredict = predictate;
            var key      = ObjectCloner.DataHash(includes);
            var ci       = GetFromCache(lpredict, key);

            if (ci != null)
            {
                return(ci);
            }

            using (var workspace = WorkspaceFactory.CreateReadOnly())
            {
                var result = workspace.Single(lpredict, includes);
                AddToCache(key, result);
                return(result);
            }
        }
Esempio n. 4
0
        public static string CheckConcurrency <T>(T entity) where T : class, ICacheable
        {
            var lup = Dao.Single <T, DateTime>(entity.Id, x => x.LastUpdateTime);

            if (entity.LastUpdateTime.CompareTo(lup) == 0)
            {
                return("");
            }

            using (var w = WorkspaceFactory.Create())
            {
                var loaded = w.Single <T>(x => x.Id == entity.Id);
                var cr     = ValidatorRegistry.GetConcurrencyErrorMessage(entity, loaded);
                if (cr.SuggestedOperation == SuggestedOperation.Refresh)
                {
                    RemoveFromEntityCache(entity);
                }
                return(cr.ErrorMessage);
            }
        }
Esempio n. 5
0
        public static void CacheSave <T>(T entity) where T : class, ICacheable
        {
            if (entity.Id > 0)
            {
                var ce = GetFromEntitiyCache <T>(entity.Id);
                if (ce != null)
                {
                    if (ce.Cacheable != entity)
                    {
                        ce.Cacheable.InjectFrom <EntityInjection>(entity);
                    }
                    entity.LastUpdateTime = DateTime.Now;
                    AddEntities(ce.Cacheable, ce.Workspace, entity.Id);
                    ce.Workspace.CommitChanges();
                    RemoveFromEntityCache <T>(ce);
                    return;
                }
            }

            if (entity.Id == 0)
            {
                var w = WorkspaceFactory.Create();
                AddEntities(entity, w, entity.Id);
                w.Add(entity);
                entity.LastUpdateTime = DateTime.Now;
                w.CommitChanges();
                AddToEntityCache <T>(entity, w);
            }
            else
            {
                using (var w = WorkspaceFactory.Create())
                {
                    var currentItem = w.Single <T>(x => x.Id == entity.Id);
                    currentItem.InjectFrom <EntityInjection>(entity);
                    entity.LastUpdateTime = DateTime.Now;
                    AddEntities(currentItem, w, entity.Id);
                    w.CommitChanges();
                }
            }
        }