/// <summary> /// Performs the actual obsoletion /// </summary> /// <param name="context">Context.</param> /// <param name="data">Data.</param> public TData Obsolete(DataContext context, TData data, IPrincipal principal) { var retVal = this.ObsoleteInternal(context, data, principal); //if (retVal != data) System.Diagnostics.Debugger.Break(); context.AddCacheCommit(retVal); return(retVal); }
/// <summary> /// Performthe actual insert. /// </summary> /// <param name="context">Context.</param> /// <param name="data">Data.</param> public TData Insert(DataContext context, TData data) { var retVal = this.InsertInternal(context, data); //if (retVal != data) System.Diagnostics.Debugger.Break(); context.AddCacheCommit(retVal); return(retVal); }
/// <summary> /// Insert or update contents of the bundle /// </summary> /// <returns></returns> public override AuditBundle InsertInternal(DataContext context, AuditBundle data, IPrincipal principal) { if (data.Item == null) { return(data); } this.m_tracer.TraceInfo("Audit Bundle has {0} objects...", data.Item.Count); for (int i = 0; i < data.Item.Count; i++) { var itm = data.Item[i]; var svc = AdoAuditPersistenceService.GetPersister(itm.GetType()); this.ProgressChanged?.Invoke(this, new ProgressChangedEventArgs((float)(i + 1) / data.Item.Count, itm)); try { if (svc == null) { throw new InvalidOperationException($"Cannot find persister for {itm.GetType()}"); } if (itm.TryGetExisting(context, principal, true) != null) { this.m_tracer.TraceInfo("Will update {0} object from bundle...", itm); data.Item[i] = svc.Update(context, itm) as IdentifiedData; } else { this.m_tracer.TraceInfo("Will insert {0} object from bundle...", itm); data.Item[i] = svc.Insert(context, itm) as IdentifiedData; } } catch (TargetInvocationException e) { this.m_tracer.TraceError("Error inserting bundle: {0}", e); throw e.InnerException; } catch (Exception e) { throw new Exception($"Could not insert bundle due to sub-object persistence (bundle item {i})", e); } } // Cache items foreach (var itm in data.Item) { itm.LoadState = LoadState.FullLoad; context.AddCacheCommit(itm); } return(data); }
/// <summary> /// Insert or update contents of the bundle /// </summary> /// <returns></returns> public override Bundle InsertInternal(DataContext context, Bundle data, IPrincipal principal) { if (data.Item == null) { return(data); } this.m_tracer.TraceInformation("Bundle has {0} objects...", data.Item.Count); data = this.ReorganizeForInsert(data); this.m_tracer.TraceInformation("After reorganization has {0} objects...", data.Item.Count); if (AdoPersistenceService.GetConfiguration().PrepareStatements) { context.PrepareStatements = true; } for (int i = 0; i < data.Item.Count; i++) { var itm = data.Item[i]; var idp = typeof(IDataPersistenceService <>).MakeGenericType(new Type[] { itm.GetType() }); var svc = ApplicationContext.Current.GetService(idp); var method = "Insert"; if (itm.TryGetExisting(context, principal, true) != null) { method = "Update"; } this.m_tracer.TraceInformation("Will {0} object from bundle {1}...", method, itm); this.ProgressChanged?.Invoke(this, new ProgressChangedEventArgs((float)(i + 1) / data.Item.Count, itm)); var mi = svc.GetType().GetRuntimeMethod(method, new Type[] { typeof(DataContext), itm.GetType(), typeof(IPrincipal) }); try { data.Item[i] = mi.Invoke(svc, new object[] { context, itm, principal }) as IdentifiedData; } catch (TargetInvocationException e) { throw e.InnerException; } } // Cache items foreach (var itm in data.Item) { itm.LoadState = LoadState.FullLoad; context.AddCacheCommit(itm); } return(data); }
/// <summary> /// Perform the actual update. /// </summary> /// <param name="context">Context.</param> /// <param name="data">Data.</param> public TData Update(DataContext context, TData data, IPrincipal principal) { //// Make sure we're updating the right thing //if (data.Key.HasValue) //{ // var cacheItem = ApplicationContext.Current.GetService<IDataCachingService>()?.GetCacheItem(data.GetType(), data.Key.Value); // if (cacheItem != null) // { // cacheItem.CopyObjectData(data); // data = cacheItem as TData; // } //} var retVal = this.UpdateInternal(context, data, principal); //if (retVal != data) System.Diagnostics.Debugger.Break(); context.AddCacheCommit(retVal); return(retVal); }
/// <summary> /// Performs the actual query /// </summary> public override IEnumerable <TModel> QueryInternal(DataContext context, Expression <Func <TModel, bool> > query, Guid queryId, int offset, int?count, out int totalResults, ModelSort <TModel>[] orderBy, bool countResults = true) { int resultCount = 0; var results = this.DoQueryInternal(context, query, queryId, offset, count, out resultCount, orderBy, countResults).ToList(); totalResults = resultCount; if (!this.m_persistenceService.GetConfiguration().SingleThreadFetch) { return(results.AsParallel().AsOrdered().WithDegreeOfParallelism(2).Select(o => { var subContext = context; var newSubContext = results.Count() > 1; var idx = results.IndexOf(o); try { if (newSubContext) { subContext = subContext.OpenClonedContext(); } if (o is Guid) { return this.Get(subContext, (Guid)o); } else { return this.CacheConvert(o, subContext); } } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error performing sub-query: {0}", e); throw; } finally { if (newSubContext) { foreach (var i in subContext.CacheOnCommit) { context.AddCacheCommit(i); } subContext.Dispose(); } } })); } else { return(results.Select(o => { if (o is Guid) { return this.Get(context, (Guid)o); } else { return this.CacheConvert(o, context); } })); } }
/// <summary> /// Insert or update contents of the bundle /// </summary> /// <returns></returns> public override Bundle InsertInternal(DataContext context, Bundle data) { this.m_tracer.TraceInfo("Bundle has {0} objects...", data.Item.Count); data = this.ReorganizeForInsert(data); this.m_tracer.TraceInfo("After reorganization has {0} objects...", data.Item.Count); context.PrepareStatements = this.m_persistenceService.GetConfiguration().PrepareStatements; // Ensure that provenance objects match var operationalItems = data.Item.Where(o => !data.ExpansionKeys.Any(k => o.Key == k)).ToArray(); var provenance = operationalItems.OfType <NonVersionedEntityData>().Select(o => o.UpdatedByKey.GetValueOrDefault()).Union(operationalItems.OfType <BaseEntityData>().Select(o => o.CreatedByKey.GetValueOrDefault())).Where(o => o != Guid.Empty); if (provenance.Distinct().Count() > 1) { this.m_tracer.TraceError("PROVENANCE OF OBJECTS DO NOT MATCH. WHEN A BUNDLE IS PERSISTED PROVENANCE DATA MUST BE NULL OR MUST MATCH. {0}", String.Join(",", provenance.Distinct().Select(o => o.ToString()))); } for (int i = 0; i < data.Item.Count; i++) { var itm = data.Item[i]; var svc = this.m_persistenceService.GetPersister(itm.GetType()); if (data.ExpansionKeys.Any(k => itm.Key == k)) { continue; // skip refs } this.ProgressChanged?.Invoke(this, new ProgressChangedEventArgs((float)(i + 1) / data.Item.Count, itm)); try { if (svc == null) { throw new InvalidOperationException($"Cannot find persister for {itm.GetType()}"); } if (itm.TryGetExisting(context, true) != null) { this.m_tracer.TraceInfo("Will update {0} object from bundle...", itm); data.Item[i] = svc.Update(context, itm) as IdentifiedData; } else { this.m_tracer.TraceInfo("Will insert {0} object from bundle...", itm); data.Item[i] = svc.Insert(context, itm) as IdentifiedData; } } catch (TargetInvocationException e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error inserting bundle: {0}", e); throw new Exception($"Error inserting bundle at item #{i}", e.InnerException); } catch (DetectedIssueException e) { this.m_tracer.TraceError("### Error Inserting Bundle[{0}]:", i); foreach (var iss in e.Issues) { this.m_tracer.TraceError("\t{0}: {1}", iss.Priority, iss.Text); } throw new DetectedIssueException(e.Issues, $"Could not insert bundle due to sub-object persistence (at item {i})", e); } catch (DbException e) { try { this.TranslateDbException(e); } catch (DetectedIssueException e2) { throw new DetectedIssueException(e2.Issues, $"Could not insert bundle due to sub-object persistence (at item {i})", e2); } catch (Exception e2) { throw new Exception($"Could not insert bundle due to sub-object persistence (bundle item {i})", e2); } } catch (Exception e) { throw new Exception($"Could not insert bundle due to sub-object persistence (bundle item {i})", e); } } // Cache items foreach (var itm in data.Item) { itm.LoadState = LoadState.FullLoad; context.AddCacheCommit(itm); } return(data); }