Class EntityContext
Inheritance
EntityContext
Assembly: LemonEdge.Database.Connector.dll
Syntax
public class EntityContext : Context, IAsyncDisposable, IEntityRetriever, IDisposable, ICustomServiceQueryHelper
Constructors
EntityContext(IDataAccessor, IApplicationDbContextFactory, IDatabaseCacheFactory, IDatabaseConnectorServiceContext, IStorageServiceFactory)
Declaration
public EntityContext(IDataAccessor accessor, IApplicationDbContextFactory dbContextFactory, IDatabaseCacheFactory cacheFactory, IDatabaseConnectorServiceContext dbConnectorServices, IStorageServiceFactory storageServiceFactory)
Parameters
Fields
_cache
Declaration
protected readonly ConcurrentDictionary<(Guid ID, Type Type), IBaseEntity> _cache
Field Value
_token
Declaration
protected CancellationToken _token
Field Value
Properties
Cache
Declaration
public override IReadOnlyCache Cache { get; set; }
Property Value
Overrides
GetUseAsOfDate
The as of date all queries in the context are running in. You should not change this for a system provided
IENtityRetriever, however you can get a new IEntityRetriever and run that with any As Of Date you wish
Declaration
public DateTimeOffset? GetUseAsOfDate { get; }
Property Value
GetUseLabels
If this context is running within a server, or for non UI displayed reasons, you don't need queries returning
labels. This significantly imprives the speed of queries that have a lot of relationships.
This indicates if queries will populate _Label fields for relationships by default or not
Declaration
public bool GetUseLabels { get; }
Property Value
IsBusy
Declaration
protected override bool IsBusy { get; }
Property Value
Overrides
IsDirectDBConnection
Indicates if this context is directly connected to the database server or not
Used by certain validation that you only want to run when connected to the db as could be expensive.
If not connected it wont be run, but will be run by the service when it processes the changes and performs its own
validation
Declaration
public bool IsDirectDBConnection { get; }
Property Value
IsODataQuery
Indicates if this underlying query is being executed through a web service or direct db connection
If this is a direct db connection some queries are changed to make sure the results are consistent across
service or direct db connection.
For instance string comparisons are case insensitive against the database so we change comparisons to use
ToLower to make sure in memory queries provide the same results.
We can't do this on an OData query as ToLower isn't translatable from Linq to OData, and the odata web service
implementation is performing a case insensitive comparison anyway
Declaration
public bool IsODataQuery { get; }
Property Value
TeamID
Declaration
public Guid TeamID { get; }
Property Value
UniqueContextID
Unique id for each instance of a context
Declaration
public Guid UniqueContextID { get; }
Property Value
Methods
AddItemByIDToCache(IBaseEntity)
Returns true if added, false if already in cache
Declaration
public bool AddItemByIDToCache(IBaseEntity item)
Parameters
Returns
ClearAsOfDate()
Declaration
protected void ClearAsOfDate()
ClearChanges()
Declaration
public override void ClearChanges()
Overrides
Dispose(bool)
Declaration
protected override void Dispose(bool disposing)
Parameters
Type |
Name |
Description |
bool |
disposing |
|
Overrides
ExecuteCustomQuery<T>(string, params object[])
Declaration
public Task<IEnumerable<T>> ExecuteCustomQuery<T>(string customQueryName, params object[] parameters) where T : IBaseEntity
Parameters
Type |
Name |
Description |
string |
customQueryName |
The name of the method in the class that inherits the CustomServiceQueryExtender that you
want to execute
|
object[] |
parameters |
A param array of parameters to be passed to this method
|
Returns
Type |
Description |
Task<IEnumerable<T>> |
A task holding the results of executing the custom LINQ query
|
Type Parameters
Name |
Description |
T |
The type of item returned by the CustomServiceQueryExtender
|
See Also
ExecuteQuery(QueryableExecuter)
Takes a QueryableExecuter and executes the query against the database (possibly via a web service)
and returns the results
If this context contains any changes of the query entity type then those changes won't be overwritten by the
results, they will be included if they match the QueryableExecuter filter
Declaration
public virtual Task<IEnumerable<IBaseEntity>> ExecuteQuery(QueryableExecuter items)
Parameters
Returns
ExecuteQueryAsCSV(DateTimeOffset?, Guid?, string, string, string)
Executes the specified query and returns the results as a CSV file in bytes
Declaration
public Task<byte[]> ExecuteQueryAsCSV(DateTimeOffset? asOfDate, Guid? CanvasID, string typeKey, string base64SerializedQueryExecuter, string base64SerializedColumnsToReturn)
Parameters
Type |
Name |
Description |
DateTimeOffset? |
asOfDate |
Runs the query using the specified As Of Date. Null indicates no as of date, and to just use the
current data.
|
Guid? |
CanvasID |
|
string |
typeKey |
The key (UniqueKey) of the entity type
we want to run a query against.
|
string |
base64SerializedQueryExecuter |
The serialization string of a QueryableExecuter containing
the query to execute against this type typeKey of entities
|
string |
base64SerializedColumnsToReturn |
A serialized string of an array of
ColumnDescriptor indicating the properties for the type to return. Null
indicates all properties for this entity type.
|
Returns
Type |
Description |
Task<byte[]> |
A Task holding the result of running the specified query, converting the results to a formatted csv file and
then to a byte array
|
ExecuteQueryFromSQL(Type, string, Guid)
Declaration
protected Task<IEnumerable<IBaseEntity>> ExecuteQueryFromSQL(Type t, string sql, Guid forType = default)
Parameters
Returns
ExecuteQueryFromSQL<T>(string, Guid)
Declaration
public Task<IEnumerable<T>> ExecuteQueryFromSQL<T>(string sql, Guid forType = default) where T : IBaseEntity
Parameters
Returns
Type Parameters
ExecuteQuery<T>(QueryableExecuter<T>)
Takes a QueryableExecuter<T> and executes the query against the database (possibly via a web
service) and returns the results
If this context contains any changes of type T then those changes won't be overwritten
by the results, they will be included if they match the QueryableExecuter filter
Declaration
public Task<IEnumerable<T>> ExecuteQuery<T>(QueryableExecuter<T> items) where T : IBaseEntity
Parameters
Type |
Name |
Description |
QueryableExecuter<T> |
items |
The queryable executer containing the definition of the query to execute. Can be created from
GetItems<T>()
|
Returns
Type |
Description |
Task<IEnumerable<T>> |
A task holding the result set of the query as an enumeration
|
Type Parameters
Name |
Description |
T |
The type of items we are executing a query against
|
ExecuteSQLResultsAsCSV(Guid, string, string, string)
Executes the specified ISQLWrapper and returns the results as a byte array of a csv fotmatted file
of the results
Declaration
public Task<byte[]> ExecuteSQLResultsAsCSV(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedExecuteQueryAsCSVQueryExecuter, string base64SerializedColumnsToReturn)
Parameters
Type |
Name |
Description |
Guid |
sqlWrapperID |
The ID of the SQL Wrapper to execute
|
string |
base64SerializedParameters |
A serialized Dictionary<TKey, TValue> of String, String,
holding the parameters for this query. The key the ParameterName for the paramater,
and the value the value
|
string |
base64SerializedExecuteQueryAsCSVQueryExecuter |
|
string |
base64SerializedColumnsToReturn |
A serialized string of an array of
ColumnDescriptor indicating the columns for the SQLWrapper query to
return. Null indicates all columns defined in this SQLWrapper.
|
Returns
Type |
Description |
Task<byte[]> |
A Task holding the result of running the specified query, converting the results to a formatted csv file and
then to a byte array
|
GenericExecuteQueryAsCSV<T, TT>(EntityDescriptor, DateTimeOffset?, Guid?, string, string)
Declaration
public Task<byte[]> GenericExecuteQueryAsCSV<T, TT>(EntityDescriptor desc, DateTimeOffset? asOfDate, Guid? CanvasID, string base64SerializedQueryExecuter, string base64SerializedColumnsToReturn) where T : IBaseEntity where TT : T
Parameters
Returns
Type Parameters
GenericExecuteQueryCount<T, TT>(EntityDescriptor, DateTimeOffset?, Guid?, string, string)
Declaration
public Task<int> GenericExecuteQueryCount<T, TT>(EntityDescriptor desc, DateTimeOffset? asOfDate, Guid? CanvasID, string base64SerializedQueryExecuter, string base64SerializedColumnsToReturn) where T : IBaseEntity where TT : T
Parameters
Returns
Type Parameters
GetChangedItem(Type, Guid)
Declaration
protected virtual IBaseEntity GetChangedItem(Type t, Guid id)
Parameters
Returns
GetFromStorage(Guid, Guid?, string, string, bool, Guid?)
Declaration
public Task<byte[]> GetFromStorage(Guid typeID, Guid? id, string fileName, string folder, bool restricted, Guid? taskId = null)
Parameters
Returns
GetGenericExecuteQueryBuilder<T, TT>(EntityDescriptor, DateTimeOffset?, Guid?, QueryableExecuter<T, TT>, List<ColumnDescriptor>)
Declaration
public Task<(IQuerySelectBuilder sb, List<string> colsToReturn, List<string> subColsToReturn, List<Type> colsType, Dictionary<string, string> colChanges)> GetGenericExecuteQueryBuilder<T, TT>(EntityDescriptor desc, DateTimeOffset? asOfDate, Guid? CanvasID, QueryableExecuter<T, TT> executer, List<ColumnDescriptor> cols) where T : IBaseEntity where TT : T
Parameters
Returns
Type Parameters
GetItemByID(Type, Guid)
Returns the item of type type
specified by the id
Declaration
public Task<IBaseEntity> GetItemByID(Type t, Guid id)
Parameters
Type |
Name |
Description |
Type |
t |
|
Guid |
id |
The ID of the entity to return
|
Returns
Type |
Description |
Task<IBaseEntity> |
A task holding the result of the request
|
GetItemByID<T>(Guid)
Returns the item of type T specified by the id
Declaration
public Task<T> GetItemByID<T>(Guid id) where T : IBaseEntity
Parameters
Type |
Name |
Description |
Guid |
id |
The ID of the entity to return
|
Returns
Type |
Description |
Task<T> |
A task holding the result of the request
|
Type Parameters
Name |
Description |
T |
The type of entity to return
|
GetItems(Type)
Declaration
public QueryableExecuter GetItems(Type type)
Parameters
Type |
Name |
Description |
Type |
type |
The type of items that the QueryableExecuter can query
|
Returns
GetItems<T>()
Declaration
public QueryableExecuter<T> GetItems<T>() where T : IBaseEntity
Returns
Type |
Description |
QueryableExecuter<T> |
The new QueryableExecuter for this specified type
|
Type Parameters
Name |
Description |
T |
The type of items that the QueryableExecuter can query
|
GetQueryCount(DateTimeOffset?, Guid?, string, string, string)
Executes the specified query and returns the count of the matching results
Declaration
public Task<int> GetQueryCount(DateTimeOffset? asOfDate, Guid? CanvasID, string typeKey, string base64SerializedQueryExecuter, string base64SerializedColumnsToReturn)
Parameters
Type |
Name |
Description |
DateTimeOffset? |
asOfDate |
Runs the query using the specified As Of Date. Null indicates no as of date, and to just use the
current data.
|
Guid? |
CanvasID |
|
string |
typeKey |
The key (UniqueKey) of the entity type
we want to run a query against.
|
string |
base64SerializedQueryExecuter |
The serialization string of a QueryableExecuter containing
the query to execute against this type typeKey of entities
|
string |
base64SerializedColumnsToReturn |
A serialized string of an array of
ColumnDescriptor indicating the properties for the type to return. Null
indicates all properties for this entity type.
|
Returns
Type |
Description |
Task<int> |
A task holding the count of the row results from running the specified query
|
GetQueryableFromSQL<T>(string, Guid)
Declaration
public IQueryable<T> GetQueryableFromSQL<T>(string sql, Guid forType = default) where T : IBaseEntity
Parameters
Returns
Type Parameters
GetQueryableItems<T>(out IQueryLock, Guid)
Declaration
public IQueryable<T> GetQueryableItems<T>(out IQueryLock lockReleaserAfterQueryableExecution, Guid forType = default) where T : IBaseEntity
Parameters
Type |
Name |
Description |
IQueryLock |
lockReleaserAfterQueryableExecution |
|
Guid |
forType |
|
Returns
Type Parameters
GetSQLCall(EntityDescriptor, long, Guid?, Guid, Guid?, bool, DateTimeOffset?, Guid?)
Declaration
public static string GetSQLCall(EntityDescriptor desc, long accountID, Guid? canvasID, Guid teamID, Guid? userID, bool useLabels, DateTimeOffset? asOfDate, Guid? userSpecificID)
Parameters
Returns
GetSQLCall(EntityDescriptor, Guid?, bool, DateTimeOffset?, Guid?)
Declaration
public string GetSQLCall(EntityDescriptor desc, Guid? CanvasID, bool useLabels, DateTimeOffset? asOfDate, Guid? userSpecificID)
Parameters
Returns
GetSQLResults(Guid, Dictionary<string, string>, QueryableExecuter, List<string>)
Declaration
public Task<IEnumerable<ISQLWrapperResult>> GetSQLResults(Guid sqlWrapperID, Dictionary<string, string> parameters, QueryableExecuter queryableExecuter, List<string> columnsToReturn)
Parameters
Returns
GetSQLResults(Guid, string, string, string, CancellationToken)
Executes the specified ISQLWrapper and returns the results set of ISQLWrapperResults.
SQLWrapperInterpretor can be used to interrogate the values of each result
item.
Declaration
public Task<IEnumerable<ISQLWrapperResult>> GetSQLResults(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedQueryExecuter, string base64SerializedColumnsToReturn, CancellationToken token = default)
Parameters
Returns
See Also
GetSQLResultsClassCount<T, TT>(Guid, string, string)
Declaration
public Task<int> GetSQLResultsClassCount<T, TT>(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedQueryExecuter) where TT : class, T, new()
Parameters
Type |
Name |
Description |
Guid |
sqlWrapperID |
|
string |
base64SerializedParameters |
|
string |
base64SerializedQueryExecuter |
|
Returns
Type Parameters
GetSQLResultsClass<T, TT>(Guid, string, string)
Declaration
public Task<IEnumerable> GetSQLResultsClass<T, TT>(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedQueryExecuter) where TT : class, T, new()
Parameters
Type |
Name |
Description |
Guid |
sqlWrapperID |
|
string |
base64SerializedParameters |
|
string |
base64SerializedQueryExecuter |
|
Returns
Type Parameters
GetSQLResultsCount(Guid, string, string)
Executes the specified sqlWrapperID
and returns the count of the results
Declaration
public Task<int> GetSQLResultsCount(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedQueryExecuter)
Parameters
Returns
Type |
Description |
Task<int> |
A task holding the count of the number of items that would be returned from executing this query
|
GetSQLResultsCount<T>(Guid, string, string)
Executes the specified sqlWrapperID
and returns the count of the specified custom types (either
or complex or entity types) that have properties matching the Name of the columns
returned by the SQLWrapper
Declaration
public Task<int> GetSQLResultsCount<T>(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedQueryExecuter)
Parameters
Type |
Name |
Description |
Guid |
sqlWrapperID |
The ID of the SQL Wrapper to execute
|
string |
base64SerializedParameters |
A serialized Dictionary<TKey, TValue> of String, String,
holding the parameters for this query. The key the ParameterName for the paramater,
and the value the value
|
string |
base64SerializedQueryExecuter |
The serialization string of a QueryableExecuter<T> of type
T containing the query to execute against this result set
|
Returns
Type |
Description |
Task<int> |
A task holding the count of the number of results that would be returned from executing this query
|
Type Parameters
Name |
Description |
T |
The type (eith a complex or entity type) that has properties matching the columns returned by the
query
|
GetSQLResultsJson(Guid, Dictionary<string, string>, QueryableExecuter, List<string>)
Declaration
public Task<IEnumerable<Dictionary<string, object>>> GetSQLResultsJson(Guid sqlWrapperID, Dictionary<string, string> parameters, QueryableExecuter queryableExecuter, List<string> columnsToReturn)
Parameters
Returns
GetSQLResults<T>(Guid, string, string)
Executes the specified sqlWrapperID
and returns the results set as a collection of specified
custom types (either or complex or entity types) that have properties matching the
Name of the columns returned by the SQLWrapper
Declaration
public Task<IEnumerable<T>> GetSQLResults<T>(Guid sqlWrapperID, string base64SerializedParameters, string base64SerializedQueryExecuter)
Parameters
Type |
Name |
Description |
Guid |
sqlWrapperID |
The ID of the SQL Wrapper to execute
|
string |
base64SerializedParameters |
A serialized Dictionary<TKey, TValue> of String, String,
holding the parameters for this query. The key the ParameterName for the paramater,
and the value the value
|
string |
base64SerializedQueryExecuter |
The serialization string of a QueryableExecuter<T> of type
T containing the query to execute against this result set
|
Returns
Type |
Description |
Task<IEnumerable<T>> |
A task holding the results of the query
|
Type Parameters
Name |
Description |
T |
The type (eith a complex or entity type) that has properties matching the columns returned by the
query
|
ReleaseQueryLock()
Releases the lock held during execution of a query. MUST be released after execution.
Declaration
public void ReleaseQueryLock()
SetAccountID(long)
Declaration
public void SetAccountID(long id)
Parameters
Type |
Name |
Description |
long |
id |
|
SetCancellationToken(CancellationToken)
Declaration
public void SetCancellationToken(CancellationToken token)
Parameters
SetTeamID(Guid)
Declaration
public void SetTeamID(Guid teamID)
Parameters
Type |
Name |
Description |
Guid |
teamID |
|
UseAsOfDate(DateTimeOffset)
Specifies the as of date all queries in the context are running in. You should not change this for a system
provided IENtityRetriever, however you can get a new IEntityRetriever and run that with any As Of Date you wish
If you are returning items as of a prior point in time you should not attempt to modify them - it will fail
Declaration
public IEntityRetriever UseAsOfDate(DateTimeOffset asOfDate)
Parameters
Type |
Name |
Description |
DateTimeOffset |
asOfDate |
The as of date to use for running queries from this context with.
|
Returns
Type |
Description |
IEntityRetriever |
The current context so commands can be chained
|
UseCanvas(Guid)
Declaration
public override IEntityRetriever UseCanvas(Guid canvasId)
Parameters
Type |
Name |
Description |
Guid |
canvasId |
|
Returns
Overrides
UseLabels(bool)
Indicates if items returned will have relationship fields with _Label properties automatically populated or not.
This is only required for items that are displayed in the UI, all other items don't need this and can benefit
from performance improvements
Declaration
public IEntityRetriever UseLabels(bool useLabels)
Parameters
Type |
Name |
Description |
bool |
useLabels |
Indicates if queries should use labels or not
|
Returns
Type |
Description |
IEntityRetriever |
The current context so commands can be chained
|
VerifyFunctions(EntityDescriptor)
Declaration
public Task<bool> VerifyFunctions(EntityDescriptor desc)
Parameters
Returns
Implements
Extension Methods