Table of Contents

Class NHPersistenceManager

Namespace
Breeze.Persistence.NH
Assembly
Breeze.Persistence.NH.dll

Manage persistence for NHibernate entity models

public class NHPersistenceManager : PersistenceManager, IDisposable
Inheritance
NHPersistenceManager
Implements
Inherited Members

Constructors

NHPersistenceManager(NHPersistenceManager)

Creates a new context using the session and metadata from the sourceContext

public NHPersistenceManager(NHPersistenceManager sourceContext)

Parameters

sourceContext NHPersistenceManager

source of the Session and metadata used by this new context.

NHPersistenceManager(ISession)

Create a new context for the given session.
Each thread should have its own NHContext and Session.

public NHPersistenceManager(ISession session)

Parameters

session ISession

Used for queries and updates

Properties

Session

The NHibernate session. Readonly.

public ISession Session { get; }

Property Value

ISession

TypeFilter

Sets a function to filter types from metadata generation and SaveChanges. The function returns true if a Type should be included, false otherwise.

public Func<Type, bool>? TypeFilter { get; set; }

Property Value

Func<Type, bool>

Examples

// exclude the LogRecord entity MyNHContext.TypeFilter = (type) => type.Name != "LogRecord";

// exclude certain entities, and all Audit* entities var excluded = new string[] { "Comment", "LogRecord", "UserPermission" }; MyNHContext.TypeFilter = (type) => { if (excluded.Contains(type.Name)) return false; if (type.Name.StartsWith("Audit")) return false; return true; };

Methods

AddKeyMapping(EntityInfo, Type, IClassMetadata)

Record the value of the temporary key in EntityKeyMapping

protected void AddKeyMapping(EntityInfo entityInfo, Type type, IClassMetadata meta)

Parameters

entityInfo EntityInfo

The entity whose temporary key is recorded

type Type

The entity type

meta IClassMetadata

NHibernate metadata for that type

BeforeSaveEntity(EntityInfo)

If TypeFilter function is defined, returns TypeFilter(entityInfo.Entity.GetType())

protected override bool BeforeSaveEntity(EntityInfo entityInfo)

Parameters

entityInfo EntityInfo

Returns

bool

true if the entity should be saved, false if not

BeforeSaveEntityGraph(List<EntityInfo>)

Allows subclasses to process entities before they are saved. This method is called after BeforeSaveEntities(saveMap), and before any session.Save methods are called. The foreign-key associations on the entities have been resolved, relating the entities to each other, and attaching proxies for other many-to-one associations.

public virtual List<EntityInfo> BeforeSaveEntityGraph(List<EntityInfo> entitiesToPersist)

Parameters

entitiesToPersist List<EntityInfo>

List of entities in the order they will be saved

Returns

List<EntityInfo>

The same entitiesToPersist. Overrides of this method may modify the list.

BeginTransaction(IsolationLevel)

Start a transaction on the session

protected override IDbTransaction BeginTransaction(IsolationLevel isolationLevel)

Parameters

isolationLevel IsolationLevel

Returns

IDbTransaction

BeginTransactionAsync(IsolationLevel, CancellationToken)

Start a transaction on the session

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

Parameters

isolationLevel IsolationLevel
cancellationToken CancellationToken

Returns

Task<IDbTransaction>

BuildJsonMetadata()

Build the JSON string of Breeze metadata for the NHibernate entity model

protected override string BuildJsonMetadata()

Returns

string

CheckForKeyUpdate(EntityInfo, IClassMetadata)

Throw an exception if the OriginalValuesMap shows an attempt to update the entity's key

protected void CheckForKeyUpdate(EntityInfo entityInfo, IClassMetadata classMeta)

Parameters

entityInfo EntityInfo
classMeta IClassMetadata

Close()

Close the session

public void Close()

CloseDbConnection()

Close the session and its associated db connection

protected override void CloseDbConnection()

CloseDbConnectionAsync()

Close the session and its associated db connection

protected override Task CloseDbConnectionAsync()

Returns

Task

Dispose()

Close the session

public void Dispose()

GetDbConnection()

public override IDbConnection GetDbConnection()

Returns

IDbConnection

The connection from the session.

GetIdentifier(object, IClassMetadata?)

Get the identifier value for the entity. If the entity does not have an identifier property, or natural identifiers defined, then the entity itself is returned.

protected object GetIdentifier(object entity, IClassMetadata? meta = null)

Parameters

entity object
meta IClassMetadata

Returns

object

GetIdentifierAsArray(object, IClassMetadata)

Get the identier value for the entity as an object[]. This is needed for creating an EntityError.

protected object[] GetIdentifierAsArray(object entity, IClassMetadata meta)

Parameters

entity object
meta IClassMetadata

Returns

object[]

GetKeyValues(EntityInfo)

Get the primary key values for the entity

public override object[] GetKeyValues(EntityInfo entityInfo)

Parameters

entityInfo EntityInfo

Returns

object[]

GetKeyValues(object)

Get the primary key values for the entity

public object[] GetKeyValues(object entity)

Parameters

entity object

Returns

object[]

GetMetadata()

Build the BreezeMetadata for the NHibernate entity model.

protected NHBreezeMetadata GetMetadata()

Returns

NHBreezeMetadata

Remarks

The metadata structure is cached by the NHPersistenceManager instance.

GetQuery<T>(bool)

Return a query for the given entity

public IQueryable<T> GetQuery<T>(bool cacheable = false)

Parameters

cacheable bool

Whether to mark the query Cacheable. Default is false.

Returns

IQueryable<T>

Type Parameters

T

Entity type

GetQuery<T>(string)

Return a cacheable query for the given entity, using the given cache region

public IQueryable<T> GetQuery<T>(string cacheRegion)

Parameters

cacheRegion string

Cache region to use.

Returns

IQueryable<T>

Type Parameters

T

Entity type

GetRelationshipFixer(Dictionary<Type, List<EntityInfo>>)

Get a new NHRelationshipFixer using the saveMap and the foreign-key map from the metadata.

protected NHRelationshipFixer GetRelationshipFixer(Dictionary<Type, List<EntityInfo>> saveMap)

Parameters

saveMap Dictionary<Type, List<EntityInfo>>

Returns

NHRelationshipFixer

OpenDbConnection()

Does nothing; connection is already open when session is created

protected override void OpenDbConnection()

OpenDbConnectionAsync(CancellationToken)

Does nothing; connection is already open when session is created

protected override Task OpenDbConnectionAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task

ProcessEntity(EntityInfo, IClassMetadata)

Add, update, or delete the entity according to its EntityState.

protected void ProcessEntity(EntityInfo entityInfo, IClassMetadata classMeta)

Parameters

entityInfo EntityInfo

The entity, and the state it should be put into

classMeta IClassMetadata

NHibernate metadata for the entity class

ProcessEntityAsync(EntityInfo, IClassMetadata, CancellationToken)

Add, update, or delete the entity according to its EntityState.

protected Task ProcessEntityAsync(EntityInfo entityInfo, IClassMetadata classMeta, CancellationToken cancellationToken)

Parameters

entityInfo EntityInfo
classMeta IClassMetadata
cancellationToken CancellationToken

Returns

Task

ProcessSaves(List<EntityInfo>)

Persist the changes to the entities in the saveOrder.

protected void ProcessSaves(List<EntityInfo> saveOrder)

Parameters

saveOrder List<EntityInfo>

Entities in the order they should be saved

ProcessSavesAsync(List<EntityInfo>, CancellationToken)

Persist the changes to the entities in the saveOrder.

protected Task ProcessSavesAsync(List<EntityInfo> saveOrder, CancellationToken cancellationToken)

Parameters

saveOrder List<EntityInfo>

Entities in the order they should be saved

cancellationToken CancellationToken

Cancels the save

Returns

Task

RefreshFromSession(Dictionary<Type, List<EntityInfo>>)

Refresh the entities from the database. This picks up changes due to triggers, etc.

protected void RefreshFromSession(Dictionary<Type, List<EntityInfo>> saveMap)

Parameters

saveMap Dictionary<Type, List<EntityInfo>>

RefreshFromSessionAsync(Dictionary<Type, List<EntityInfo>>, CancellationToken)

Refresh the entities from the database. This picks up changes due to triggers, etc.

protected Task RefreshFromSessionAsync(Dictionary<Type, List<EntityInfo>> saveMap, CancellationToken cancellationToken)

Parameters

saveMap Dictionary<Type, List<EntityInfo>>

The entities that were saved

cancellationToken CancellationToken

Cancels the refresh

Returns

Task

RestoreOldVersionValue(EntityInfo, IClassMetadata)

Restore the old value of the concurrency column so Hibernate will save the entity. Otherwise it will complain because Breeze has already changed the value.

protected void RestoreOldVersionValue(EntityInfo entityInfo, IClassMetadata classMeta)

Parameters

entityInfo EntityInfo
classMeta IClassMetadata

SaveChangesCore(SaveWorkState)

Persist the changes to the entities in the saveMap. This implements the abstract method in PersistenceManager. Assigns saveWorkState.KeyMappings, which map the temporary keys to their real generated keys. Note that this method sets session.FlushMode = FlushMode.Never, so manual flushes are required.

protected override void SaveChangesCore(SaveWorkState saveWorkState)

Parameters

saveWorkState SaveWorkState

SaveChangesCoreAsync(SaveWorkState, CancellationToken)

Persist the changes to the entities in the saveMap. This implements the abstract method in PersistenceManager. Assigns saveWorkState.KeyMappings, which map the temporary keys to their real generated keys. Note that this method sets session.FlushMode = FlushMode.Never, so manual flushes are required.

protected override Task SaveChangesCoreAsync(SaveWorkState saveWorkState, CancellationToken cancellationToken)

Parameters

saveWorkState SaveWorkState
cancellationToken CancellationToken

Returns

Task

UpdateAutoGeneratedKeys(List<EntityInfo>)

Update the KeyMappings with their real values.

protected List<KeyMapping> UpdateAutoGeneratedKeys(List<EntityInfo> entitiesWithAutoGeneratedKeys)

Parameters

entitiesWithAutoGeneratedKeys List<EntityInfo>

Returns

List<KeyMapping>