Inherits: IReferenceable, IUnreferenceable, [IConfigurable](../../../components/config/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
Instance methods
ClearAsync
Clears a component’s state.
public virtual
Task ClearAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
CloseAsync
Closes the component and frees used resources.
public virtual
Task CloseAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
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(IContext context, T item)
- context: IContext - (optional) a context to trace execution through a 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(IContext context, FilterDefinition<T> filterDefinition)
- context: IContext - (optional) a context to trace execution through a 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(IContext context, FilterDefinition<T> filterDefinition, SortDefinition<T> sortDefinition = null)
- context: IContext - (optional) a context to trace execution through a 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(IContext context, FilterDefinition<T> filterDefinition)
- context: IContext - (optional) a context to trace execution through a 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(IContext context, FilterDefinition<T> filterDefinition, PagingParams paging = null, SortDefinition<T> sortDefinition = null)
- context: IContext - (optional) a context to trace execution through a 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(IContext context, 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" }