Session: Subscribing to Session Events
-
Events let you run custom logic in response to operations made on a
DocumentStoreor aSession.
An event is invoked when the selected action runs on an entity, or when a query is made. -
Subscribing to an event in a
Sessionis valid for that session only.
E.g., to invoke an event afterSaveChanges()is called by this session only, use:
session.Advanced.OnAfterSaveChanges += OnAfterSaveChangesEvent; -
Subscribing to an event at the
DocumentStorelevel subscribes to that event in all subsequent sessions.
Read more about store events here. -
In this article:
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
- BeforeStoreEventArgs
- BeforeDeleteEventArgs
class BeforeStoreEventArgs
{
InMemoryDocumentSessionOperations Session
string DocumentId
object Entity
IMetadataDictionary DocumentMetadata
}
| Property | Type | Description |
|---|---|---|
| Session | InMemoryDocumentSessionOperations | The session on which SaveChanges() was called |
| DocumentId | string | The ID of the document being stored |
| Entity | object | The entity being stored |
| DocumentMetadata | IMetadataDictionary | The document's metadata |
class BeforeDeleteEventArgs
{
InMemoryDocumentSessionOperations Session
string DocumentId
object Entity
IMetadataDictionary DocumentMetadata
}
| Property | Type | Description |
|---|---|---|
| Session | InMemoryDocumentSessionOperations | The session on which SaveChanges() was called |
| DocumentId | string | The ID of the document being deleted |
| Entity | object | The entity being deleted |
| DocumentMetadata | IMetadataDictionary | The document's metadata |
- AfterSaveChangesEventArgs
- BeforeQueryEventArgs
class AfterSaveChangesEventArgs
{
InMemoryDocumentSessionOperations Session
string DocumentId
object Entity
IMetadataDictionary DocumentMetadata
}
| Property | Type | Description |
|---|---|---|
| Session | InMemoryDocumentSessionOperations | The session on which SaveChanges() was called |
| DocumentId | string | The ID of the document that was saved |
| Entity | object | The entity that was saved |
| DocumentMetadata | IMetadataDictionary | The document's metadata |
class BeforeQueryEventArgs
{
InMemoryDocumentSessionOperations Session
IDocumentQueryCustomization QueryCustomization
}
| Property | Type | Description |
|---|---|---|
| Session | InMemoryDocumentSessionOperations | The session that triggered the event |
| QueryCustomization | IDocumentQueryCustomization | The query's customizations |
- BeforeConversionToDocumentEventArgs
- AfterConversionToDocumentEventArgs
class BeforeConversionToDocumentEventArgs
{
string Id
object Entity
InMemoryDocumentSessionOperations Session
}
| Property | Type | Description |
|---|---|---|
| Id | string | The ID of the document being converted |
| Entity | object | The entity being converted to a document |
| Session | InMemoryDocumentSessionOperations | The session that triggered the event |
class AfterConversionToDocumentEventArgs
{
string Id
object Entity
BlittableJsonReaderObject Document
InMemoryDocumentSessionOperations Session
}
| Property | Type | Description |
|---|---|---|
| Id | string | The ID of the converted document |
| Entity | object | The entity that was converted |
| Document | BlittableJsonReaderObject | The resulting JSON document |
| Session | InMemoryDocumentSessionOperations | The session that triggered the event |
- BeforeConversionToEntityEventArgs
- AfterConversionToEntityEventArgs
class BeforeConversionToEntityEventArgs
{
string Id
Type Type
BlittableJsonReaderObject Document
InMemoryDocumentSessionOperations Session
}
| Property | Type | Description |
|---|---|---|
| Id | string | The ID of the document being converted |
| Type | Type | The target entity type |
| Document | BlittableJsonReaderObject | The JSON document being converted |
| Session | InMemoryDocumentSessionOperations | The session that triggered the event |
class AfterConversionToEntityEventArgs
{
string Id
BlittableJsonReaderObject Document
object Entity
InMemoryDocumentSessionOperations Session
}
| Property | Type | Description |
|---|---|---|
| Id | string | The ID of the converted document |
| Document | BlittableJsonReaderObject | The source JSON document |
| Entity | object | The resulting entity |
| Session | InMemoryDocumentSessionOperations | The session that triggered the event |
- SessionDisposingEventArgs
class SessionDisposingEventArgs
{
InMemoryDocumentSessionOperations Session
}
| Property | Type | Description |
|---|---|---|
| Session | InMemoryDocumentSessionOperations | The session being disposed |