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
sourceContextNHPersistenceManagersource 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
sessionISessionUsed 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
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
entityInfoEntityInfoThe entity whose temporary key is recorded
typeTypeThe entity type
metaIClassMetadataNHibernate metadata for that type
BeforeSaveEntity(EntityInfo)
If TypeFilter function is defined, returns TypeFilter(entityInfo.Entity.GetType())
protected override bool BeforeSaveEntity(EntityInfo entityInfo)
Parameters
entityInfoEntityInfo
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
entitiesToPersistList<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
isolationLevelIsolationLevel
Returns
BeginTransactionAsync(IsolationLevel, CancellationToken)
Start a transaction on the session
protected override Task<IDbTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)
Parameters
isolationLevelIsolationLevelcancellationTokenCancellationToken
Returns
BuildJsonMetadata()
Build the JSON string of Breeze metadata for the NHibernate entity model
protected override string BuildJsonMetadata()
Returns
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
entityInfoEntityInfoclassMetaIClassMetadata
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
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
entityobjectmetaIClassMetadata
Returns
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
entityobjectmetaIClassMetadata
Returns
- object[]
GetKeyValues(EntityInfo)
Get the primary key values for the entity
public override object[] GetKeyValues(EntityInfo entityInfo)
Parameters
entityInfoEntityInfo
Returns
- object[]
GetKeyValues(object)
Get the primary key values for the entity
public object[] GetKeyValues(object entity)
Parameters
entityobject
Returns
- object[]
GetMetadata()
Build the BreezeMetadata for the NHibernate entity model.
protected NHBreezeMetadata GetMetadata()
Returns
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
cacheableboolWhether to mark the query Cacheable. Default is false.
Returns
- IQueryable<T>
Type Parameters
TEntity 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
cacheRegionstringCache region to use.
Returns
- IQueryable<T>
Type Parameters
TEntity 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
saveMapDictionary<Type, List<EntityInfo>>
Returns
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
cancellationTokenCancellationToken
Returns
ProcessEntity(EntityInfo, IClassMetadata)
Add, update, or delete the entity according to its EntityState.
protected void ProcessEntity(EntityInfo entityInfo, IClassMetadata classMeta)
Parameters
entityInfoEntityInfoThe entity, and the state it should be put into
classMetaIClassMetadataNHibernate 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
entityInfoEntityInfoclassMetaIClassMetadatacancellationTokenCancellationToken
Returns
ProcessSaves(List<EntityInfo>)
Persist the changes to the entities in the saveOrder.
protected void ProcessSaves(List<EntityInfo> saveOrder)
Parameters
saveOrderList<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
saveOrderList<EntityInfo>Entities in the order they should be saved
cancellationTokenCancellationTokenCancels the save
Returns
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
saveMapDictionary<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
saveMapDictionary<Type, List<EntityInfo>>The entities that were saved
cancellationTokenCancellationTokenCancels the refresh
Returns
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
entityInfoEntityInfoclassMetaIClassMetadata
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
saveWorkStateSaveWorkState
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
saveWorkStateSaveWorkStatecancellationTokenCancellationToken
Returns
UpdateAutoGeneratedKeys(List<EntityInfo>)
Update the KeyMappings with their real values.
protected List<KeyMapping> UpdateAutoGeneratedKeys(List<EntityInfo> entitiesWithAutoGeneratedKeys)
Parameters
entitiesWithAutoGeneratedKeysList<EntityInfo>