private static IPersistenceContext InheritOrCreateContext(PersistenceContextType contextType, PersistenceScopeOption scopeOption) { if (scopeOption == PersistenceScopeOption.RequiresNew) { // need to create a new context return((contextType == PersistenceContextType.Update) ? (IPersistenceContext) PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush) : PersistentStoreRegistry.GetDefaultStore().OpenReadContext()); } // create a context if one doesn't exist, or attempt to inherit the existing one if (contextType == PersistenceContextType.Update) { // if no current context, create an update context if (CurrentContext == null) { return(PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush)); } // if the current context is an update context, inherit if (CurrentContext is IUpdateContext) { return(CurrentContext); } // can't ask for an update context when current context is a read context throw new InvalidOperationException(SR.ExceptionIncompatiblePersistenceContext); } else { // if no current context, create a read context // otherwise return the current context, regardless of its type // (read operations are allowed to execute in an update context) return(CurrentContext ?? PersistentStoreRegistry.GetDefaultStore().OpenReadContext()); } }
/// <summary> /// Creates a new persistence scope for the specified context type, using the specified option to determine /// whether to create a new persistence context, or attempt to inherit an existing context. /// </summary> /// <remarks> /// If there is no parent scope, or <see cref = "PersistenceScopeOption.RequiresNew"/> was specified, /// a new context of the specified type will be opened. Otherwise, /// if there is a parent scope holding a context of the correct type, that context will be inherited. /// If a new context is opened, the scope owns the context, and closes it when the scope terminates. /// If a context was inherited, the scope does not own the context, and takes no action on it when the scope terminates. /// If an update context is requested and while a parent scope is holding a read context, an exception will be thrown. /// If a read context is requested while a parent scope is holding an update context, the update context will be inherited. /// </remarks> /// <param name="contextType"></param> /// <param name="scopeOption"></param> public PersistenceScope(PersistenceContextType contextType, PersistenceScopeOption scopeOption) : this(InheritOrCreateContext(contextType, scopeOption)) { }
public ServiceOperationAttribute() { // a persistence context is required, by default _scopeOption = PersistenceScopeOption.Required; }
private static IPersistenceContext InheritOrCreateContext(PersistenceContextType contextType, PersistenceScopeOption scopeOption) { if (scopeOption == PersistenceScopeOption.RequiresNew) { // need to create a new context return (contextType == PersistenceContextType.Update) ? (IPersistenceContext)PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush) : (IPersistenceContext)PersistentStoreRegistry.GetDefaultStore().OpenReadContext(); } else { // create a context if one doesn't exist, or attempt to inherit the existing one if (contextType == PersistenceContextType.Update) { // if no current context, create an update context if (PersistenceScope.CurrentContext == null) return PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush); // if the current context is an update context, inherit if (PersistenceScope.CurrentContext is IUpdateContext) return PersistenceScope.CurrentContext; // can't ask for an update context when current context is a read context throw new InvalidOperationException(SR.ExceptionIncompatiblePersistenceContext); } else { // if no current context, create a read context if (PersistenceScope.CurrentContext == null) return PersistentStoreRegistry.GetDefaultStore().OpenReadContext(); // otherwise return the current context, regardless of its type // (read operations are allowed to execute in an update context) return PersistenceScope.CurrentContext; } } }