MongoDbPersistence<T>

Abstract persistence component that stores data in MongoDB using the official MongoDB driver.

Inherits: IReferenceable, IUnreferenceable, IConfigurable, IOpenable, ICleanable

Description

The MongoDbPersistence class allows you to create persistence components that store data in MongoDBs using the official MongoDB driver.

Important points

  • This is the most basic persistence component that is only able to store data items of any type.
  • Specific CRUD operations over the data items must be implemented in child classes by accessing the this._collection property.

Configuration parameters

  • collection: (optional) MongoDB collection name

connection(s):

  • discovery_key: (optional) key to retrieve the connection from IDiscovery
  • host: host name or IP address
  • port: port number (default: 27017)
  • uri: resource URI or connection string with all parameters in it

credential(s):

  • store_key: (optional) key to retrieve the credentials from ICredentialStore
  • username: (optional) username
  • password: (optional) user’s password

options:

  • max_pool_size: (optional) maximum connection pool size (default: 2)
  • keep_alive: (optional) enable connection keep alive (default: true)
  • connect_timeout: (optional) connection timeout in milliseconds (default: 5 sec)
  • auto_reconnect: (optional) enable auto reconnection (default: true)
  • max_page_size: (optional) maximum page size (default: 100)
  • debug: (optional) enable debug output (default: false).

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:credential-store:*:*:1.0 - (optional) credential stores to resolve credentials

Constructors

Creates a new instance of the persistence component.

public MongoDbPersistence(string collectionName)

  • collectionName: string - (optional) collection name.

Fields

_dependencyResolver

Dependency resolver.

protected _dependencyResolver: DependencyResolver

_logger

Logger.

protected _logger: CompositeLogger

_connection

MongoDB connection component.

protected _connection: MongoDBConnection

_collectionName

MongoDB collection name.

protected _collectionName: string;

_collection

MongoDb collection object.

protected _collection: IMongoCollection<T>

_client

MongoDB connection pool object.

protected _client: MongoClient

_database

MongoDb database object.

protected _database: IMongoDatabase

_maxPageSize

Maximum number of records to return from the database per request.

protected _maxPageSize: int = 100

Instance methods

ClearAsync

Clears a component’s state.

public virtual Task ClearAsync(string correlationId)

  • correlationId: string - object to convert from the public partial format.

CloseAsync

Closes the component and frees used resources.

public virtual Task CloseAsync(string correlationId)

  • correlationId: string - object to convert from the public partial format.

Configure

Closes the component and frees used resources.

public virtual void Configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

CreateAsync

Creates a data item.

public virtual async Task<T> CreateAsync(string correlationId, T item)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • item: T - item to be created.
  • returns: Task<T> - created item

DeleteByFilterAsync

This method shall be called by a public DeleteByFilterAsync method from the child class that receives FilterParams and converts them into a filter function.

public virtual Task DeleteByFilterAsync(string correlationId, FilterDefinition<T> filterDefinition)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • filterDefinition: FilterDefinition<T> - (optional) filter function used to filter items.

GetListByFilterAsync

Gets a list of data items retrieved by a given filter and sorted according to sort parameters.

This method shall be called by a public GetListByFilterAsync method from the child class that receives FilterParams and converts them into a filter function.

public virtual Task<List<T>> GetListByFilterAsync(string correlationId, FilterDefinition<T> filterDefinition, SortDefinition<T> sortDefinition = null)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • filterDefinition: FilterDefinition<T> - (optional) filter function used to filter items
  • sortDefinition: SortDefinition<T> - (optional) sorting parameters
  • returns: Task<List<T>> - data list of results by filter.

GetOneRandomAsync

Gets a random item from items that match to a given filter.

This method shall be called by a public GetOneRandomAsync method from the child class that receives FilterParams and converts them into a filter function.

public virtual async Task<T> GetOneRandomAsync(string correlationId, FilterDefinition<T> filterDefinition)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • filterDefinition: FilterDefinition<T> - filter JSON object.
  • returns: Task<T> - random item.

GetPageByFilter

Gets a page of data items retrieved by a given filter and sorted according to sort parameters.

This method shall be called by a public GetPageByFilter method from the child class that receives FilterParams and converts them into a filter function.

public virtual Task<DataPage<T>> GetPageByFilterAsync(string correlationId, FilterDefinition<T> filterDefinition, PagingParams paging = null, SortDefinition<T> sortDefinition = null)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • filterDefinition: FilterDefinition<T> - (optional) filter for JSON object
  • paging: PagingParams - (optional) paging parameters
  • sortDefinition: SortDefinition<T> - (optional) sorting JSON object
  • returns: Task<DataPage<T>> - data page obtained by filtering

UnsetReferences

Unsets (clears) previously set references to dependent components.

public virtual void UnsetReferences()

SetReferences

Sets references to dependent components.

public virtual void SetReferences(IReferences references)

  • references: IReferences - references to locate the component’s dependencies.

Examples

class MyMongoDbPersistence: MongoDbPersistence<MyData> 
{
    public MyMongoDbPersistence(): base("mydata") { }
    
    public MyData getByName(string correlationId, string name)
    {
        var builder = Builders<BeaconV1>.Filter;
        var filter = builder.Eq(x => x.Name, name);
        var result = await _collection.Find(filter).FirstOrDefaultAsync();
        return result;
    }
    public MyData set(String correlatonId, MyData item)
    {
        var filter = Builders<T>.Filter.Eq(x => x.Id, item.Id);
        var options = new FindOneAndReplaceOptions<T>
        {
            ReturnDocument = ReturnDocument.After,
            IsUpsert = true
        };
        var result = await _collection.FindOneAndReplaceAsync(filter, item, options);
        return result;
    }
}

var persistence = new MyMongoDbPersistence();
persistence.Configure(ConfigParams.FromTuples(
   "host", "localhost",
   "port", 27017 
));

persitence.Open("123");
var mydata = new MyData("ABC");
persistence.Set("123", mydata);
persistence.GetByName("123", "ABC");
Console.Out.WriteLine(item);                   // Result: { name: "ABC" }