/// <summary> /// Gets the specified container identifier. /// </summary> /// <typeparam name="TIdentifier">The type of the t identifier.</typeparam> /// <param name="containerId">The container identifier.</param> /// <param name="principal">The principal.</param> /// <param name="loadFast">if set to <c>true</c> [load fast].</param> /// <returns>Returns the model instance.</returns> public TModel Get <TIdentifier>(Identifier <TIdentifier> containerId, IPrincipal principal, bool loadFast) { var identifier = containerId as Identifier <Guid>; var preRetrievalArgs = new PreRetrievalEventArgs(containerId, principal); this.Retrieving?.Invoke(this, preRetrievalArgs); if (preRetrievalArgs.Cancel) { this.traceSource.TraceEvent(TraceEventType.Warning, 0, $"Pre-event handler indicates abort retrieve: {containerId.Id}"); return(null); } using (var connection = Configuration.Provider.GetReadonlyConnection()) { try { connection.Open(); this.traceSource.TraceEvent(TraceEventType.Verbose, 0, $"GET: {containerId.Id}"); var result = this.Get(connection, identifier.Id, principal, loadFast); var postRetrievalEventArgs = new PostRetrievalEventArgs <TModel>(result, principal); this.Retrieved?.Invoke(this, postRetrievalEventArgs); return(result); } catch (Exception e) { this.traceSource.TraceEvent(TraceEventType.Error, 0, $"Error: {e}"); throw; } } }
/// <summary> /// Fire retrieving /// </summary> protected void FireRetrieved(PostRetrievalEventArgs <TData> e) { this.Retrieved?.Invoke(this, e); }
/// <summary> /// Gets the specified object /// </summary> public virtual TData Get <TIdentifier>(MARC.HI.EHRS.SVC.Core.Data.Identifier <TIdentifier> containerId, IPrincipal principal, bool loadFast) { // Try the cache if available var guidIdentifier = containerId as Identifier <Guid>; var cacheItem = ApplicationContext.Current.GetService <IDataCachingService>()?.GetCacheItem <TData>(guidIdentifier.Id) as TData; if (loadFast && cacheItem != null) { return(cacheItem); } else { #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif PreRetrievalEventArgs preArgs = new PreRetrievalEventArgs(containerId, principal); this.Retrieving?.Invoke(this, preArgs); if (preArgs.Cancel) { this.m_tracer.TraceEvent(TraceEventType.Warning, 0, "Pre-Event handler indicates abort retrieve {0}", containerId.Id); return(null); } // Query object using (var connection = m_configuration.Provider.GetReadonlyConnection()) try { this.ThrowIfExceeded(); connection.Open(); this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "GET {0}", containerId); if (loadFast) { connection.AddData("loadFast", true); connection.LoadState = LoadState.PartialLoad; } else { connection.LoadState = LoadState.FullLoad; } var result = this.Get(connection, guidIdentifier.Id, principal); var postData = new PostRetrievalEventArgs <TData>(result, principal); this.Retrieved?.Invoke(this, postData); foreach (var itm in connection.CacheOnCommit) { ApplicationContext.Current.GetService <IDataCachingService>()?.Add(itm); } return(result); } catch (NotSupportedException e) { throw new DataPersistenceException("Cannot perform LINQ query", e); } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, 0, "Error : {0}", e); throw; } finally { #if DEBUG sw.Stop(); this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Retrieve took {0} ms", sw.ElapsedMilliseconds); #endif Interlocked.Decrement(ref m_currentRequests); } } }
/// <summary> /// Gets the specified object taking version of the entity into consideration /// </summary> public override TModel Get <TIdentifier>(MARC.HI.EHRS.SVC.Core.Data.Identifier <TIdentifier> containerId, IPrincipal principal, bool loadFast) { var tr = 0; var uuid = containerId as Identifier <Guid>; if (uuid.Id != Guid.Empty) { var cacheItem = ApplicationContext.Current.GetService <IDataCachingService>()?.GetCacheItem <TModel>(uuid.Id) as TModel; if (cacheItem != null && (cacheItem.VersionKey.HasValue && uuid.VersionId == cacheItem.VersionKey.Value || uuid.VersionId == Guid.Empty) && (loadFast && cacheItem.LoadState >= LoadState.PartialLoad || !loadFast && cacheItem.LoadState == LoadState.FullLoad)) { return(cacheItem); } } #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif PreRetrievalEventArgs preArgs = new PreRetrievalEventArgs(containerId, principal); this.FireRetrieving(preArgs); if (preArgs.Cancel) { this.m_tracer.TraceEvent(TraceEventType.Warning, 0, "Pre-Event handler indicates abort retrieve {0}", containerId.Id); return(null); } // Query object using (var connection = m_configuration.Provider.GetReadonlyConnection()) try { connection.Open(); this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "GET {0}", containerId); TModel retVal = null; connection.LoadState = LoadState.FullLoad; // Get most recent version if (uuid.VersionId == Guid.Empty) { retVal = this.Get(connection, uuid.Id, principal); } else { retVal = this.CacheConvert(this.QueryInternal(connection, o => o.Key == uuid.Id && o.VersionKey == uuid.VersionId, Guid.Empty, 0, 1, out tr).FirstOrDefault(), connection, principal); } var postData = new PostRetrievalEventArgs <TModel>(retVal, principal); this.FireRetrieved(postData); return(retVal); } catch (NotSupportedException e) { throw new DataPersistenceException("Cannot perform LINQ query", e); } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, 0, "Error : {0}", e); throw; } finally { #if DEBUG sw.Stop(); this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Retrieve took {0} ms", sw.ElapsedMilliseconds); #endif } }