Table of Contents

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

Lazy<Type>

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

Func<Dictionary<Type, List<EntityInfo>>, List<KeyMapping>, CancellationToken, Task>

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

Func<EntityInfo, CancellationToken, Task<bool>>

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

Func<EntityInfo, bool>

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

IKeyGenerator

SaveOptions

Options for the current save operation; null before a save starts

public SaveOptions? SaveOptions { get; set; }

Property Value

SaveOptions

SaveWorkState

The SaveWorkState representing the save in progress; null before the first save

protected SaveWorkState? SaveWorkState { get; }

Property Value

SaveWorkState

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

saveMap Dictionary<Type, List<EntityInfo>>

The same saveMap that was returned from BeforeSaveEntities

keyMappings List<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

saveMap Dictionary<Type, List<EntityInfo>>

The same saveMap that was returned from BeforeSaveEntities

keyMappings List<KeyMapping>

The mapping of temporary keys to real keys

cancellationToken CancellationToken

Returns

Task

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

saveMap Dictionary<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

saveMap Dictionary<Type, List<EntityInfo>>

A List of EntityInfo for each Type

cancellationToken CancellationToken

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

entityInfo EntityInfo

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

entityInfo EntityInfo
cancellationToken CancellationToken

Returns

Task<bool>

true to include the entity in the save, false to exclude

BeginTransaction(IsolationLevel)

Begin a transaction before saving entities; null if there is no connection

protected virtual IDbTransaction? BeginTransaction(IsolationLevel isolationLevel)

Parameters

isolationLevel IsolationLevel

Returns

IDbTransaction

BeginTransactionAsync(IsolationLevel, CancellationToken)

Begin a transaction before saving entities

protected abstract Task<IDbTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)

Parameters

isolationLevel IsolationLevel
cancellationToken CancellationToken

Returns

Task<IDbTransaction>

BuildJsonMetadata()

Build the metadata from the entity model

protected abstract string BuildJsonMetadata()

Returns

string

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

Task

CreateEntityInfo()

Create an EntityInfo instance (override to return a subclass)

protected virtual EntityInfo CreateEntityInfo()

Returns

EntityInfo

CreateEntityInfo(object, EntityState)

Create an EntityInfo instance

public EntityInfo CreateEntityInfo(object entity, EntityState entityState = EntityState.Added)

Parameters

entity object
entityState EntityState

Returns

EntityInfo

CreateEntityInfoFromJson(dynamic, Type)

Build an EntityInfo from the JObject

protected EntityInfo CreateEntityInfoFromJson(dynamic jo, Type entityType)

Parameters

jo dynamic
entityType Type

Returns

EntityInfo

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

dynSaveBundle dynamic

Returns

SaveOptions

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

entityInfo EntityInfo

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

e Exception
saveWorkState SaveWorkState

Returns

bool

InitializeSaveState(JObject)

Prepare the SaveWorkState from the JSON payload

[MemberNotNull("SaveWorkState")]
protected void InitializeSaveState(JObject saveBundle)

Parameters

saveBundle JObject

LookupEntityType(string)

Find the Type from the entityTypeName

protected Type LookupEntityType(string entityTypeName)

Parameters

entityTypeName string

Returns

Type

Metadata()

Get the JSON metadata for the entity model

public string Metadata()

Returns

string

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

cancellationToken CancellationToken

Returns

Task

SaveChanges(JObject, TransactionSettings?)

Extract the entities from the saveBundle, save them, and return the SaveResult

public SaveResult SaveChanges(JObject saveBundle, TransactionSettings? transactionSettings = null)

Parameters

saveBundle JObject
transactionSettings TransactionSettings

Returns

SaveResult

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

saveBundle JObject
transactionSettings TransactionSettings
cancellationToken CancellationToken

Returns

Task<SaveResult>

SaveChangesCore(SaveWorkState)

Save the changes to the database

protected abstract void SaveChangesCore(SaveWorkState saveWorkState)

Parameters

saveWorkState SaveWorkState

SaveChangesCoreAsync(SaveWorkState, CancellationToken)

Save the changes to the database

protected abstract Task SaveChangesCoreAsync(SaveWorkState saveWorkState, CancellationToken cancellationToken)

Parameters

saveWorkState SaveWorkState
cancellationToken CancellationToken

Returns

Task

XDocToJson(XDocument)

Converts XML to JSON (for converting EF metadata documents)

public static string XDocToJson(XDocument xDoc)

Parameters

xDoc XDocument

Returns

string