Document Storage Interceptors

Document Storage Interceptors allow to intercept invocations to the IDocumentStorage<TDocument> or IDocumentStorage instances. This useful feature can help you implement some infrastructure mechanism, for instance, log an audit event when user accessing a document, validate documents and arguments of the accessing, inject or restrict data and so forth.

Intercept Typed Documents

When a document is presented as a normal .NET class and working with it goes through the IDocumentStorage<TDocument>, to define the interceptor for this document implement the IDocumentStorageInterceptor<TDocument> interface:

class MyDocumentStorageInterceptor : DocumentStorageInterceptor<MyDocument>
{
    public override void OnAfterInsertOne(DocumentInsertOneCommand<MyDocument> command,
                                          DocumentStorageWriteResult<object> result,
                                          Exception exception)
    {
        // Handling the invocations IDocumentStorage<MyDocument>.InsertOne()...
    }

    // ...
}

After that register the interceptor in IoC-container:

builder.RegisterType<MyDocumentStorageInterceptor>()
       .As<IDocumentStorageInterceptor>()
       .SingleInstance();

Intercept Dynamic Documents

When a document is presented as the DynamicDocument class and working with it goes through the IDocumentStorage, to define the interceptor for this document implement the IDocumentStorageInterceptor interface:

class MyDocumentStorageInterceptor : DocumentStorageInterceptor
{
    public override void OnAfterInsertOne(DocumentInsertOneCommand command,
                                          DocumentStorageWriteResult<object> result,
                                          Exception exception)
    {
        // Handling the invocations IDocumentStorage<MyDocument>.InsertOne()...
    }

    // ...
}

After that register the interceptor in IoC-container:

builder.RegisterType<MyDocumentStorageInterceptor>()
       .As<IDocumentStorageInterceptor>()
       .SingleInstance();