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); } }
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(); } }
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); } }
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(); } } }