/// <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 FireRetrieving(PreRetrievalEventArgs e) { this.Retrieving?.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> /// Get message info /// </summary> public MessageInfo GetMessageInfo(string messageId) { IDbConnection conn = m_configuration.CreateConnection(); try { var mpe = new PreRetrievalEventArgs <MessageInfo>(new Identifier <String>(messageId)); this.Retrieving?.Invoke(this, mpe); if (mpe.Cancel) { Trace.TraceInformation("GetMessageInfo: Event handler indicates cancel"); return(null); } conn.Open(); // Create the database command IDbCommand cmd = conn.CreateCommand(); try { // Setup command cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "get_msg_tbl"; // Setup parameter IDataParameter msgIdParm = cmd.CreateParameter(); msgIdParm.DbType = DbType.String; msgIdParm.Value = messageId; msgIdParm.Direction = ParameterDirection.Input; msgIdParm.ParameterName = "msg_id_in"; cmd.Parameters.Add(msgIdParm); // Execute using (IDataReader rdr = cmd.ExecuteReader()) if (rdr.Read()) { var retVal = new MessageInfo() { Body = (byte[])rdr["msg_body"], Destination = rdr["msg_dst"] == DBNull.Value ? null : new Uri(Convert.ToString(rdr["msg_dst"])), Id = Convert.ToString(rdr["msg_id"]), Response = rdr["msg_rsp_id"] == DBNull.Value ? null : Convert.ToString(rdr["msg_rsp_id"]), Source = rdr["msg_src"] == DBNull.Value ? null : new Uri(Convert.ToString(rdr["msg_src"])), Timestamp = (DateTime)rdr["msg_utc"] }; this.Retrieved?.Invoke(this, new PostRetrievalEventArgs <MessageInfo>(retVal)); return(retVal); } else { this.Retrieved?.Invoke(this, new PostRetrievalEventArgs <MessageInfo>(null)); return(null); } } finally { cmd.Dispose(); } } catch (Exception e) { Trace.TraceError(e.ToString()); throw; } finally { conn.Close(); conn.Dispose(); } }
/// <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 } }