Skip to main content

Session: Subscribing to Session Events

Session events

OnBeforeStore

This event is invoked as a part of SaveChanges, but before it is actually sent to the server.
See its arguments in the Syntax section below.

private void OnBeforeStoreEvent(object sender, BeforeStoreEventArgs args);

Example: discontinue out-of-stock products

private void OnBeforeStoreEvent(object sender, BeforeStoreEventArgs args)
{
var product = args.Entity as Product;
if (product?.UnitsInStock == 0)
{
product.Discontinued = true;
}
}

After you subscribe to the event, every stored entity invokes the method:

// Subscribe to the event
session.Advanced.OnBeforeStore += OnBeforeStoreEvent;

session.Store(new Product
{
Name = "RavenDB v3.5",
UnitsInStock = 0
});
session.Store(new Product
{
Name = "RavenDB v4.0",
UnitsInStock = 1000
});

session.SaveChanges(); // Here the method is invoked

OnBeforeDelete

This event is invoked by Delete(id) or Delete(entity). It is executed only when SaveChanges() is called, but before the commands are actually sent to the server.
See its arguments in the Syntax section below.

private void OnBeforeDeleteEvent(object sender, BeforeDeleteEventArgs args);

Example: prevent deletions

private void OnBeforeDeleteEvent(object sender, BeforeDeleteEventArgs args)
{
throw new NotSupportedException();
}

Subscribe to the event:

// Subscribe to the event
session.Advanced.OnBeforeDelete += OnBeforeDeleteEvent;

var product = session.Load<Product>("products/1-A");

// OnBeforeDelete is triggered whether you
// call Delete() on an entity or on its ID
session.Delete(product);
session.SaveChanges(); // NotSupportedException will be thrown

OnAfterSaveChanges

This event is invoked after SaveChanges returns.
See its arguments in the Syntax section below.

private void OnAfterSaveChangesEvent(object sender, AfterSaveChangesEventArgs args);

Example: log each saved document

private void OnAfterSaveChangesEvent(object sender, AfterSaveChangesEventArgs args)
{
if (Log.IsInfoEnabled)
Log.Info($"Document '{args.DocumentId}' was saved.");
}

Subscribe to the event:

session.Advanced.OnAfterSaveChanges += OnAfterSaveChangesEvent;

OnBeforeQuery

This event is invoked just before the query is sent to the server.
See its arguments in the Syntax section below.

private void OnBeforeQueryEvent(object sender, BeforeQueryEventArgs args);

Example: disable query caching

private void OnBeforeQueryEvent(object sender, BeforeQueryEventArgs args)
{
args.QueryCustomization.NoCaching();
}

Example: wait for non-stale results

To have each query wait for non-stale results, define the event as follows:

private void OnBeforeQueryEvent(object sender, BeforeQueryEventArgs args)
{
args.QueryCustomization.WaitForNonStaleResults(TimeSpan.FromSeconds(30));
}

Subscribe to the event:

session.Advanced.OnBeforeQuery += OnBeforeQueryEvent;

OnBeforeConversionToDocument

This event is invoked before an entity is converted to a JSON document, e.g. when a document is sent to the server.
See its arguments in the Syntax section below.

private void OnBeforeConversionToDocumentEvent(object sender, BeforeConversionToDocumentEventArgs args);

Example: modify an entity before conversion

private void OnBeforeConversionToDocument(object sender, BeforeConversionToDocumentEventArgs args)
{
if (args.Entity is Item item)
item.Before = true;
}

Subscribe to the event:

session.Advanced.OnBeforeConversionToDocument += OnBeforeConversionToDocument;

OnAfterConversionToDocument

This event is invoked after an entity is converted to a JSON document.
See its arguments in the Syntax section below.

private void OnAfterConversionToDocumentEvent(object sender, AfterConversionToDocumentEventArgs args);

Example: modify a document after conversion

private void OnAfterConversionToDocument(object sender, AfterConversionToDocumentEventArgs args)
{
if (args.Entity is Item item)
{
if (args.Document.Modifications == null)
args.Document.Modifications = new DynamicJsonValue();

args.Document.Modifications["After"] = true;
args.Document = args.Session.Context.ReadObject(args.Document, args.Id);

item.After = true;
}
}

Subscribe to the event:

session.Advanced.OnAfterConversionToDocument += OnAfterConversionToDocument;

OnBeforeConversionToEntity

This event is invoked before a JSON document is converted to an entity, e.g. when a document is loaded.
See its arguments in the Syntax section below.

private void OnBeforeConversionToEntity(object sender, BeforeConversionToEntityEventArgs args);

Example: modify a document before conversion

private void OnBeforeConversionToEntity(object sender, BeforeConversionToEntityEventArgs args)
{
var document = args.Document;
if (document.Modifications == null)
document.Modifications = new DynamicJsonValue();

document.Modifications["Before"] = true;
args.Document = args.Session.Context.ReadObject(document, args.Id);
}

Subscribe to the event:

session.Advanced.OnBeforeConversionToEntity += OnBeforeConversionToEntity;

OnAfterConversionToEntity

This event is invoked after a JSON document is converted to an entity.
See its arguments in the Syntax section below.

private void OnAfterConversionToEntity(object sender, AfterConversionToEntityEventArgs args);

Example: modify an entity after conversion

private void OnAfterConversionToEntity(object sender, AfterConversionToEntityEventArgs args)
{
if (args.Entity is Item item)
item.After = true;
}

Subscribe to the event:

session.Advanced.OnAfterConversionToEntity += OnAfterConversionToEntity;

OnSessionDisposing

This event is invoked by the disposal of a session, before the session is disposed of.
Tracking session disposal lets you, among other uses, verify that sessions you no longer need are disposed of.
See its arguments in the Syntax section below.

private void OnSessionDisposingEvent(object sender, SessionDisposingEventArgs args);

Subscribe to the event:

session.Advanced.OnSessionDisposing += OnSessionDisposingEvent;

Unsubscribing from an event

To stop responding to an event, detach the handler with the -= operator, passing the same method you subscribed with:

session.Advanced.OnBeforeStore -= OnBeforeStoreEvent;

Session events can be unsubscribed at any time.

Syntax

Events

public event EventHandler<BeforeStoreEventArgs> OnBeforeStore
public event EventHandler<BeforeDeleteEventArgs> OnBeforeDelete
public event EventHandler<AfterSaveChangesEventArgs> OnAfterSaveChanges
public event EventHandler<BeforeQueryEventArgs> OnBeforeQuery
public event EventHandler<BeforeConversionToDocumentEventArgs> OnBeforeConversionToDocument
public event EventHandler<AfterConversionToDocumentEventArgs> OnAfterConversionToDocument
public event EventHandler<BeforeConversionToEntityEventArgs> OnBeforeConversionToEntity
public event EventHandler<AfterConversionToEntityEventArgs> OnAfterConversionToEntity
public event EventHandler<SessionDisposingEventArgs> OnSessionDisposing

Event arguments

class BeforeStoreEventArgs
{
InMemoryDocumentSessionOperations Session
string DocumentId
object Entity
IMetadataDictionary DocumentMetadata
}
PropertyTypeDescription
SessionInMemoryDocumentSessionOperationsThe session on which SaveChanges() was called
DocumentIdstringThe ID of the document being stored
EntityobjectThe entity being stored
DocumentMetadataIMetadataDictionaryThe document's metadata
class AfterSaveChangesEventArgs
{
InMemoryDocumentSessionOperations Session
string DocumentId
object Entity
IMetadataDictionary DocumentMetadata
}
PropertyTypeDescription
SessionInMemoryDocumentSessionOperationsThe session on which SaveChanges() was called
DocumentIdstringThe ID of the document that was saved
EntityobjectThe entity that was saved
DocumentMetadataIMetadataDictionaryThe document's metadata
class BeforeConversionToDocumentEventArgs
{
string Id
object Entity
InMemoryDocumentSessionOperations Session
}
PropertyTypeDescription
IdstringThe ID of the document being converted
EntityobjectThe entity being converted to a document
SessionInMemoryDocumentSessionOperationsThe session that triggered the event
class BeforeConversionToEntityEventArgs
{
string Id
Type Type
BlittableJsonReaderObject Document
InMemoryDocumentSessionOperations Session
}
PropertyTypeDescription
IdstringThe ID of the document being converted
TypeTypeThe target entity type
DocumentBlittableJsonReaderObjectThe JSON document being converted
SessionInMemoryDocumentSessionOperationsThe session that triggered the event
class SessionDisposingEventArgs
{
InMemoryDocumentSessionOperations Session
}
PropertyTypeDescription
SessionInMemoryDocumentSessionOperationsThe session being disposed

In this article