Class PersistenceManager
- Namespace
- Breeze.Persistence
- Assembly
- Breeze.Persistence.dll
Manages persistence for Breeze entity models.
public abstract class PersistenceManager
- Inheritance
-
PersistenceManager
- Derived
- Inherited Members
Fields
KeyGeneratorType
Get an IKeyGenerator implementation from loaded assemblies
protected static Lazy<Type> KeyGeneratorType
Field Value
Properties
AfterSaveEntitiesAsyncDelegate
If assigned, this function is called after all entities are saved using SaveChangesAsync.
public Func<Dictionary<Type, List<EntityInfo>>, List<KeyMapping>, CancellationToken, Task>? AfterSaveEntitiesAsyncDelegate { get; set; }
Property Value
AfterSaveEntitiesDelegate
If assigned, this function is called after all entities are saved.
public Action<Dictionary<Type, List<EntityInfo>>, List<KeyMapping>>? AfterSaveEntitiesDelegate { get; set; }
Property Value
- Action<Dictionary<Type, List<EntityInfo>>, List<KeyMapping>>
BeforeSaveEntitiesAsyncDelegate
If assigned, this function is called before entities are saved using SaveChangesAsync. Entities in the dictionary can be added, removed, or changed before saving.
public Func<Dictionary<Type, List<EntityInfo>>, CancellationToken, Task<Dictionary<Type, List<EntityInfo>>>>? BeforeSaveEntitiesAsyncDelegate { get; set; }
Property Value
- Func<Dictionary<Type, List<EntityInfo>>, CancellationToken, Task<Dictionary<Type, List<EntityInfo>>>>
BeforeSaveEntitiesDelegate
If assigned, this function is called before entities are saved. Entities in the dictionary can be added, removed, or changed before saving.
public Func<Dictionary<Type, List<EntityInfo>>, Dictionary<Type, List<EntityInfo>>>? BeforeSaveEntitiesDelegate { get; set; }
Property Value
- Func<Dictionary<Type, List<EntityInfo>>, Dictionary<Type, List<EntityInfo>>>
BeforeSaveEntityAsyncDelegate
If assigned, this function is called before each entity is saved using SaveChangesAsync. If the function returns false, the entity will not be saved.
public Func<EntityInfo, CancellationToken, Task<bool>>? BeforeSaveEntityAsyncDelegate { get; set; }
Property Value
BeforeSaveEntityDelegate
If assigned, this function is called before each entity is saved. If the function returns false, the entity will not be saved.
public Func<EntityInfo, bool>? BeforeSaveEntityDelegate { get; set; }
Property Value
JsonSerializer
The configured serializer for reading the save payload
protected JsonSerializer JsonSerializer { get; }
Property Value
- JsonSerializer
KeyGenerator
Generates primary key values for new entities. Null until one is assigned or first needed.
public IKeyGenerator? KeyGenerator { get; set; }
Property Value
SaveOptions
Options for the current save operation; null before a save starts
public SaveOptions? SaveOptions { get; set; }
Property Value
SaveWorkState
The SaveWorkState representing the save in progress; null before the first save
protected SaveWorkState? SaveWorkState { get; }
Property Value
Methods
AfterSaveEntities(Dictionary<Type, List<EntityInfo>>, List<KeyMapping>)
Called after the entities have been saved, and all the temporary keys have been replaced by real keys. The base implementation calls AfterSaveEntitiesDelegate, or does nothing if AfterSaveEntitiesDelegate is null.
protected virtual void AfterSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap, List<KeyMapping> keyMappings)
Parameters
saveMapDictionary<Type, List<EntityInfo>>The same saveMap that was returned from BeforeSaveEntities
keyMappingsList<KeyMapping>The mapping of temporary keys to real keys
AfterSaveEntitiesAsync(Dictionary<Type, List<EntityInfo>>, List<KeyMapping>, CancellationToken)
Called after the entities have been saved using SaveChangesAsync, and all the temporary keys have been replaced by real keys. The base implementation calls AfterSaveEntitiesAsyncDelegate, or does nothing if AfterSaveEntitiesAsyncDelegate is null.
protected virtual Task AfterSaveEntitiesAsync(Dictionary<Type, List<EntityInfo>> saveMap, List<KeyMapping> keyMappings, CancellationToken cancellationToken)
Parameters
saveMapDictionary<Type, List<EntityInfo>>The same saveMap that was returned from BeforeSaveEntities
keyMappingsList<KeyMapping>The mapping of temporary keys to real keys
cancellationTokenCancellationToken
Returns
BeforeSaveEntities(Dictionary<Type, List<EntityInfo>>)
Called after BeforeSaveEntity, and before saving the entities to the persistence layer. Allows adding, changing, and removing entities prior to save. The base implementation returns the result of BeforeSaveEntitiesDelegate, or the unchanged saveMap if BeforeSaveEntitiesDelegate is null.
protected virtual Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap)
Parameters
saveMapDictionary<Type, List<EntityInfo>>A List of EntityInfo for each Type
Returns
- Dictionary<Type, List<EntityInfo>>
The EntityInfo for each entity that should be saved
BeforeSaveEntitiesAsync(Dictionary<Type, List<EntityInfo>>, CancellationToken)
Called after BeforeSaveEntity, and before saving the entities to the persistence layer using SaveChangesAsync. Allows adding, changing, and removing entities prior to save. The base implementation returns the result of BeforeSaveEntitiesAsyncDelegate, or the unchanged saveMap if BeforeSaveEntitiesAsyncDelegate is null.
protected virtual Task<Dictionary<Type, List<EntityInfo>>> BeforeSaveEntitiesAsync(Dictionary<Type, List<EntityInfo>> saveMap, CancellationToken cancellationToken)
Parameters
saveMapDictionary<Type, List<EntityInfo>>A List of EntityInfo for each Type
cancellationTokenCancellationToken
Returns
- Task<Dictionary<Type, List<EntityInfo>>>
The EntityInfo for each entity that should be saved
BeforeSaveEntity(EntityInfo)
The method is called for each entity to be saved before the save occurs. If this method returns 'false' then the entity will be excluded from the save. The base implementation returns the result of BeforeSaveEntityDelegate, or 'true' if BeforeSaveEntityDelegate is null.
protected virtual bool BeforeSaveEntity(EntityInfo entityInfo)
Parameters
entityInfoEntityInfo
Returns
- bool
true to include the entity in the save, false to exclude
BeforeSaveEntityAsync(EntityInfo, CancellationToken)
The method is called for each entity to be saved before the save occurs using SaveChangeAsync. If this method returns 'false' then the entity will be excluded from the save. The base implementation returns the result of BeforeSaveEntityAsyncDelegate, or 'true' if BeforeSaveEntityAsyncDelegate is null.
protected virtual Task<bool> BeforeSaveEntityAsync(EntityInfo entityInfo, CancellationToken cancellationToken)
Parameters
entityInfoEntityInfocancellationTokenCancellationToken
Returns
BeginTransaction(IsolationLevel)
Begin a transaction before saving entities; null if there is no connection
protected virtual IDbTransaction? BeginTransaction(IsolationLevel isolationLevel)
Parameters
isolationLevelIsolationLevel
Returns
BeginTransactionAsync(IsolationLevel, CancellationToken)
Begin a transaction before saving entities
protected abstract Task<IDbTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)
Parameters
isolationLevelIsolationLevelcancellationTokenCancellationToken
Returns
BuildJsonMetadata()
Build the metadata from the entity model
protected abstract string BuildJsonMetadata()
Returns
CloseDbConnection()
Internal use only. Should only be called by PersistenceManager during SaveChanges. Closes the DbConnection used by the PersistenceManager's implementation.
protected abstract void CloseDbConnection()
CloseDbConnectionAsync()
Internal use only. Should only be called by PersistenceManager during SaveChangesAsync. Closes the DbConnection used by the PersistenceManager's implementation.
protected abstract Task CloseDbConnectionAsync()
Returns
CreateEntityInfo()
Create an EntityInfo instance (override to return a subclass)
protected virtual EntityInfo CreateEntityInfo()
Returns
CreateEntityInfo(object, EntityState)
Create an EntityInfo instance
public EntityInfo CreateEntityInfo(object entity, EntityState entityState = EntityState.Added)
Parameters
entityobjectentityStateEntityState
Returns
CreateEntityInfoFromJson(dynamic, Type)
Build an EntityInfo from the JObject
protected EntityInfo CreateEntityInfoFromJson(dynamic jo, Type entityType)
Parameters
jodynamicentityTypeType
Returns
ExtractSaveOptions(dynamic)
Get the SaveOptions from the JSON save bundle; null if the bundle's saveOptions is JSON null
public static SaveOptions? ExtractSaveOptions(dynamic dynSaveBundle)
Parameters
dynSaveBundledynamic
Returns
GetDbConnection()
Should only be called from BeforeSaveEntities and AfterSaveEntities.
public abstract IDbConnection GetDbConnection()
Returns
- IDbConnection
Open DbConnection used by the PersistenceManager's implementation
GetKeyValues(EntityInfo)
Get the primary key values from the entity
public virtual object[] GetKeyValues(EntityInfo entityInfo)
Parameters
entityInfoEntityInfo
Returns
- object[]
HandleSaveException(Exception, SaveWorkState)
Allows subclasses to plug in own save exception handling. Either throw an exception here, return false, or return true and modify the saveWorkState.
protected virtual bool HandleSaveException(Exception e, SaveWorkState saveWorkState)
Parameters
eExceptionsaveWorkStateSaveWorkState
Returns
InitializeSaveState(JObject)
Prepare the SaveWorkState from the JSON payload
[MemberNotNull("SaveWorkState")]
protected void InitializeSaveState(JObject saveBundle)
Parameters
saveBundleJObject
LookupEntityType(string)
Find the Type from the entityTypeName
protected Type LookupEntityType(string entityTypeName)
Parameters
entityTypeNamestring
Returns
Metadata()
Get the JSON metadata for the entity model
public string Metadata()
Returns
OpenDbConnection()
Internal use only. Should only be called by PersistenceManager during SaveChanges. Opens the DbConnection used by the PersistenceManager's implementation. Method must be idempotent; after it is called the first time, subsequent calls have no effect.
protected abstract void OpenDbConnection()
OpenDbConnectionAsync(CancellationToken)
Internal use only. Should only be called by PersistenceManager during SaveChangesAsync. Opens the DbConnection used by the PersistenceManager's implementation. Method must be idempotent; after it is called the first time, subsequent calls have no effect.
protected abstract Task OpenDbConnectionAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationToken
Returns
SaveChanges(JObject, TransactionSettings?)
Extract the entities from the saveBundle, save them, and return the SaveResult
public SaveResult SaveChanges(JObject saveBundle, TransactionSettings? transactionSettings = null)
Parameters
saveBundleJObjecttransactionSettingsTransactionSettings
Returns
SaveChangesAsync(JObject, TransactionSettings?, CancellationToken)
Extract the entities from the saveBundle, save them, and return the SaveResult
public Task<SaveResult> SaveChangesAsync(JObject saveBundle, TransactionSettings? transactionSettings = null, CancellationToken cancellationToken = default)
Parameters
saveBundleJObjecttransactionSettingsTransactionSettingscancellationTokenCancellationToken
Returns
SaveChangesCore(SaveWorkState)
Save the changes to the database
protected abstract void SaveChangesCore(SaveWorkState saveWorkState)
Parameters
saveWorkStateSaveWorkState
SaveChangesCoreAsync(SaveWorkState, CancellationToken)
Save the changes to the database
protected abstract Task SaveChangesCoreAsync(SaveWorkState saveWorkState, CancellationToken cancellationToken)
Parameters
saveWorkStateSaveWorkStatecancellationTokenCancellationToken
Returns
XDocToJson(XDocument)
Converts XML to JSON (for converting EF metadata documents)
public static string XDocToJson(XDocument xDoc)
Parameters
xDocXDocument