/// <summary> /// Get specified data from persistence /// </summary> public virtual TEntity Get(Guid key, Guid versionKey) { // Demand permission this.DemandRead(key); var persistenceService = ApplicationContext.Current.GetService <IDataPersistenceService <TEntity> >(); if (persistenceService == null) { throw new InvalidOperationException($"Unable to locate {nameof(IDataPersistenceService<TEntity>)}"); } var preEventArg = new DataRetrievingEventArgs <TEntity>(key, versionKey, AuthenticationContext.Current.Principal); this.Retrieving?.Invoke(this, preEventArg); if (preEventArg.Cancel) { this.m_traceSource.TraceWarning("Pre-retrieve event indicates cancel"); return(preEventArg.Result); } var businessRulesService = ApplicationContext.Current.GetService <IBusinessRulesService <TEntity> >(); var result = persistenceService.Get(key, null, false, AuthenticationContext.Current.Principal); var retVal = businessRulesService?.AfterRetrieve(result) ?? result; var postEvt = new DataRetrievedEventArgs <TEntity>(retVal, AuthenticationContext.Current.Principal); this.Retrieved?.Invoke(this, postEvt); return(postEvt.Data); }
/// <summary> /// Fire retrieving /// </summary> protected void FireRetrieved(DataRetrievedEventArgs <TData> e) { if (e.Data != null) { this.Retrieved?.Invoke(this, e); } }
/// <summary> /// Get specified data from persistence /// </summary> public virtual TEntity Get(Guid key, Guid versionKey) { // Demand permission this.DemandRead(key); var persistenceService = ApplicationServiceContext.Current.GetService <IDataPersistenceService <TEntity> >(); if (persistenceService == null) { throw new InvalidOperationException(this.m_localizationService.FormatString("error.server.core.servicePersistence", new { param = nameof(IDataPersistenceService <TEntity>) })); } var businessRulesService = ApplicationServiceContext.Current.GetBusinessRulesService <TEntity>(); var preRetrieve = new DataRetrievingEventArgs <TEntity>(key, versionKey, AuthenticationContext.Current.Principal); this.Retrieving?.Invoke(this, preRetrieve); if (preRetrieve.Cancel) { this.m_traceSource.TraceInfo("Pre-retrieve trigger signals cancel: {0}", key); return(this.m_privacyService?.Apply(preRetrieve.Result, AuthenticationContext.Current.Principal) ?? preRetrieve.Result); } var result = persistenceService.Get(key, versionKey, true, AuthenticationContext.Current.Principal); var retVal = businessRulesService?.AfterRetrieve(result) ?? result; var postEvt = new DataRetrievedEventArgs <TEntity>(retVal, AuthenticationContext.Current.Principal); this.Retrieved?.Invoke(this, postEvt); return(this.m_privacyService?.Apply(postEvt.Data, AuthenticationContext.Current.Principal) ?? postEvt.Data); }
/// <summary> /// Gets the specified container identifier. /// </summary> /// <param name="containerId">The container identifier.</param> /// <param name="overrideAuthContext">The principal to use instead of the default.</param> /// <param name="loadFast">if set to <c>true</c> [load fast].</param> /// <param name="containerVersion">The version of the container to load</param> /// <returns>Returns the model instance.</returns> public TModel Get(Guid containerId, Guid?containerVersion, bool loadFast = false, IPrincipal overrideAuthContext = null) { var preRetrievalArgs = new DataRetrievingEventArgs <TModel>(containerId, null, overrideAuthContext); this.Retrieving?.Invoke(this, preRetrievalArgs); if (preRetrievalArgs.Cancel) { this.traceSource.TraceEvent(EventLevel.Warning, $"Pre-event handler indicates abort retrieve: {containerId}"); return(null); } using (var connection = Configuration.Provider.GetReadonlyConnection()) { try { connection.Open(); this.traceSource.TraceEvent(EventLevel.Verbose, $"GET: {containerId}"); var result = this.Get(connection, containerId, loadFast); var postRetrievalEventArgs = new DataRetrievedEventArgs <TModel>(result, overrideAuthContext); this.Retrieved?.Invoke(this, postRetrievalEventArgs); return(result); } catch (Exception e) { this.traceSource.TraceEvent(EventLevel.Error, $"Error: {e}"); throw; } } }
/// <summary> /// Gets the specified object by identifier /// </summary> public AuditData Get(Guid containerId, Guid?versionId, bool loadFast = false, IPrincipal overrideAuthContext = null) { var preEvtData = new DataRetrievingEventArgs <AuditData>(containerId, versionId, overrideAuthContext); this.Retrieving?.Invoke(this, preEvtData); if (preEvtData.Cancel) { this.m_traceSource.TraceWarning("Pre-retrieval event indicates cancel {0}", containerId); return(null); } try { var pk = containerId; // Fetch using (var context = this.m_configuration.Provider.GetReadonlyConnection()) { context.Open(); var sql = this.m_builder.CreateQuery <AuditData>(o => o.Key == pk).Build(); var res = context.FirstOrDefault <CompositeResult <DbAuditData, DbAuditCode> >(sql); var result = this.ToModelInstance(context, res as CompositeResult <DbAuditData, DbAuditCode>, false); var postEvtData = new DataRetrievedEventArgs <AuditData>(result, overrideAuthContext); this.Retrieved?.Invoke(this, postEvtData); return(postEvtData.Data); } } catch (Exception e) { this.m_traceSource.TraceError("Error retrieving audit {0} : {1}", containerId, e); throw; } }
/// <summary> /// Gets the specified object /// </summary> public override TModel Get(Guid containerId, Guid?versionId, bool loadFast, IPrincipal principal = null) { var tr = 0; if (containerId != Guid.Empty) { var cacheItem = ApplicationServiceContext.Current.GetService <IDataCachingService>()?.GetCacheItem <TModel>(containerId) as TModel; if (cacheItem != null && (cacheItem.VersionKey.HasValue && versionId == cacheItem.VersionKey.Value || versionId == Guid.Empty) && (loadFast && cacheItem.LoadState >= LoadState.PartialLoad || !loadFast && cacheItem.LoadState == LoadState.FullLoad)) { return(cacheItem); } } #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif DataRetrievingEventArgs <TModel> preArgs = new DataRetrievingEventArgs <TModel>(containerId, versionId, principal); this.FireRetrieving(preArgs); if (preArgs.Cancel) { this.m_tracer.TraceEvent(EventLevel.Warning, "Pre-Event handler indicates abort retrieve {0}", containerId); return(preArgs.Result); } // Query object using (var connection = m_configuration.Provider.GetReadonlyConnection()) try { connection.Open(); this.m_tracer.TraceEvent(EventLevel.Verbose, "GET {0}", containerId); TModel retVal = null; if (loadFast) { connection.LoadState = LoadState.PartialLoad; } else { connection.LoadState = LoadState.FullLoad; } // Get most recent version if (versionId.GetValueOrDefault() == Guid.Empty) { retVal = this.Get(connection, containerId); } else { retVal = this.QueryInternal(connection, o => o.Key == containerId && o.VersionKey == versionId && o.ObsoletionTime == null || o.ObsoletionTime != null, Guid.Empty, 0, 1, out tr, null).FirstOrDefault(); } var postData = new DataRetrievedEventArgs <TModel>(retVal, principal); this.FireRetrieved(postData); // Add to cache foreach (var d in connection.CacheOnCommit) { ApplicationServiceContext.Current.GetService <IDataCachingService>()?.Add(d); } return(retVal); } catch (NotSupportedException e) { throw new DataPersistenceException("Cannot perform LINQ query", e); } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error : {0}", e); throw new DataPersistenceException($"Cannot retrieve object {containerId}", e); } finally { #if DEBUG sw.Stop(); this.m_tracer.TraceEvent(EventLevel.Verbose, "Retrieve took {0} ms", sw.ElapsedMilliseconds); #endif } }
/// <summary> /// Cannot query bundles /// </summary> protected void OnRetrieved(object sender, DataRetrievedEventArgs <Bundle> e) { throw new NotSupportedException("Cannot retrieve bundles"); }
/// <summary> /// Gets the specified object /// </summary> public virtual TData Get(Guid containerId, Guid?versionId, bool loadFast, IPrincipal overrideAuthContext) { var cacheItem = ApplicationServiceContext.Current.GetService <IDataCachingService>()?.GetCacheItem <TData>(containerId) as TData; if (loadFast && cacheItem != null && versionId == Guid.Empty) { return(cacheItem); } else { #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif DataRetrievingEventArgs <TData> preArgs = new DataRetrievingEventArgs <TData>(containerId, versionId, overrideAuthContext); this.Retrieving?.Invoke(this, preArgs); if (preArgs.Cancel) { this.m_tracer.TraceEvent(EventLevel.Warning, "Pre-Event handler indicates abort retrieve {0}", containerId); return(preArgs.Result); } // Query object using (var connection = this.m_settingsProvider.GetConfiguration().Provider.GetReadonlyConnection()) try { connection.Open(); this.m_tracer.TraceEvent(EventLevel.Verbose, "GET {0}", containerId); if (loadFast) { connection.AddData("loadFast", true); connection.LoadState = LoadState.PartialLoad; } else { connection.LoadState = LoadState.FullLoad; } connection.AddData("principal", overrideAuthContext); var result = this.Get(connection, containerId); var postData = new DataRetrievedEventArgs <TData>(result, overrideAuthContext); this.Retrieved?.Invoke(this, postData); ApplicationServiceContext.Current.GetService <IDataCachingService>()?.Add(result); return(result); } catch (NotSupportedException e) { throw new DataPersistenceException("Cannot perform LINQ query as underlying provider doesn't support it", e); } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error : {0}", e); throw new DataPersistenceException($"General error retrieving {containerId}", e); } finally { #if DEBUG sw.Stop(); this.m_tracer.TraceEvent(EventLevel.Verbose, "Retrieve took {0} ms", sw.ElapsedMilliseconds); #endif } } }
/// <summary> /// Get the specified resource /// </summary> /// <typeparam name="TIdentifier">The type of identifier</typeparam> /// <param name="containerId">The id of the identifier to get</param> /// <param name="principal">The security principal to execute as</param> /// <param name="loadFast">True if to skip loading of some properties</param> /// <returns>The loaded model</returns> public TModel Get(Guid containerId, Guid?containerVersion, bool loadFast, IPrincipal principal) { // Try the cache if available var guidIdentifier = containerId; var cacheItem = ApplicationServiceContext.Current.GetService <IDataCachingService>()?.GetCacheItem <TModel>(guidIdentifier) as TModel; if (loadFast && cacheItem != null) { return(cacheItem); } else { #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif DataRetrievingEventArgs <TModel> preArgs = new DataRetrievingEventArgs <TModel>(containerId, containerVersion, principal); this.Retrieving?.Invoke(this, preArgs); if (preArgs.Cancel) { this.m_tracer.TraceWarning("Pre-Event handler indicates abort retrieve {0}", containerId); return(null); } // Query object using (var connection = AdoAuditPersistenceService.GetConfiguration().Provider.GetReadonlyConnection()) try { connection.Open(); this.m_tracer.TraceVerbose("GET {0}", containerId); if (loadFast) { connection.AddData("loadFast", true); connection.LoadState = LoadState.PartialLoad; } else { connection.LoadState = LoadState.FullLoad; } var result = this.GetInternal(connection, guidIdentifier, principal); var postData = new DataRetrievedEventArgs <TModel>(result, principal); this.Retrieved?.Invoke(this, postData); foreach (var itm in connection.CacheOnCommit) { ApplicationServiceContext.Current.GetService <IDataCachingService>()?.Add(itm); } return(result); } catch (NotSupportedException e) { throw new DataPersistenceException("Cannot perform LINQ query", e); } catch (Exception e) { this.m_tracer.TraceError("Error : {0}", e); throw; } finally { #if DEBUG sw.Stop(); this.m_tracer.TraceVerbose("Retrieve took {0} ms", sw.ElapsedMilliseconds); #endif } } }