Inherits: IReferenceable, IUnreferenceable, IConfigurable, IOpenable, ICleanable
Description
The SqlServerPersistence class allows you to create persistence components that store data in a SQL Server database using the official driver.
Important points
- This is the most basic persistence component that is able to store data items of any type.
- Specific CRUD operations over the data items must be implemented in child classes by accessing this._model or this._collection properties.
Configuration parameters
- collection: (optional) SQLServer 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)
- 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) ICredentialStore to resolve credentials
Constructors
Creates a new instance of the persistence component.
public
SqlServerPersistence(string tableName)
- tableName: string - (optional) table name.
Fields
Instance methods
AutoCreateObject
Adds an index definition to be created on opening.
- This is a deprecated method. Use EnsureSchema instead.
protected
void AutoCreateObject(string schemaStatement)
- schemaStatement: string - DML statement to autocreate a database object
ClearAsync
Clears a component’s state.
public virtual
Task ClearAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
ClearSchema
Clears all auto-created objects
protected
void ClearSchema()
CloseAsync
Closes a component and frees used resources.
public virtual
Task CloseAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
Configure
Configures a component by passing its configuration parameters.
public virtual
void Configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
ConvertFromPublic
Converts an object value from public to internal format.
protected virtual
AnyValueMap ConvertFromPublic(T value)
- value: T - object in public format to convert.
- returns: AnyValueMap - converted object in internal format.
ConvertToPublic
Converts an object value from internal to public format.
protected virtual
T ConvertToPublic(AnyValueMap map)
- map: AnyValueMap - object in internal format to convert.
- returns: T - converted object in public format.
CreateAsync
Creates a data item.
public virtual
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
createSchema
Creates a schema.
protected
Task CreateSchemaAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
DefineSchema
Defines a database schema via auto create objects or convenience methods. Override it in child classes
protected virtual
void DefineSchema()
DeleteByFilterAsync
Deletes data items that match to a given filter. This method shall be called by a public DeleteByFilterAsync method from a child class that receives FilterParams and converts them into a filter function.
public virtual
Task DeleteByFilterAsync(IContext context, string filter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: string - (optional) filter for JSON object.
EnsureIndex
Adds an index definition to be created on opening.
protected
void EnsureIndex(string name, Dictionary<string, bool> keys, IndexOptions options)
- name: string - index name
- keys: Dictionary<string, bool> - index keys (fields)
- options: IndexOptions - index options
EnsureSchema
Adds a statement to a schema definition.
protected
void EnsureSchema(string dmlStatement)
- dmlStatement: string - statement to be added to the schema
GenerateColumns
Generates a list of column names to use in SQL statements like: “column1,column2,column3”
protected
string GenerateColumns(AnyValueMap map)
- map: AnyValueMap - array with column values or a key-value map
- returns: string - generated list of column names
GenerateParameters
Generates a list of value parameters to use in SQL statements like: "@Param1,@Param2,@Param3".
protected
string GenerateParameters(AnyValueMap map)
- map: AnyValueMap - array with values or a key-value map
- returns: string - generated list of value parameters
protected
string GenerateParameters<K>(IEnumerable<K> values)
- values: IEnumerable<K> - key-value map with columns and values
- returns: string - generated list of column sets
GenerateSetParameters
Generates a list of value parameters to use in SQL statements like: “@Param1,@Param2,@Param3”
protected
string GenerateSetParameters(AnyValueMap map)
- map: AnyValueMap - key-value map with columns and values
- returns: string - generated list of column sets
protected
string GenerateSetParameters(IEnumerable<string> values)
- values: IEnumerable<string> - key-value map with columns and values
- returns: string - generated list of column sets
GenerateValues
Generates a list of column parameters
protected
List<object> GenerateValues(AnyValueMap map)
- map: AnyValueMap - key-value map with columns and values
- returns: List<object> - generated list of column values
GetCountByFilterAsync
Gets a number of data items retrieved by a given filter.
This method shall be called by a public GetCountByFilterAsync method from a child class that receives FilterParams and converts them into a filter function.
protected virtual
Task<long> GetCountByFilterAsync(IContext context, string filter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: string - (optional) filter for JSON objects.
- returns: Task<long> - number of filtered items.
GetListByFilterAsync
Gets a list of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public GetListByFilterAsync method from a child class that receives FilterParams and converts them into a filter function.
protected
Task<List<T>> GetListByFilterAsync(IContext context, string filter, string sort = null, string select = null)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: string - (optional) filter function used to filter items
- sort: string - (optional) sorting parameters
- select: string - (optional) projection parameters (not used yet)
- returns: Task<List<T>> - data list of filtered results
GetOneRandomAsync
Gets a random item from items that match to a given filter.
This method shall be called by a public GetOneRandomAsync method from a child class that receives FilterParams and converts them into a filter function.
protected virtual
Task<T> GetOneRandomAsync(IContext context, string filter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: string - (optional) filter for JSON objects
- returns: Task<T> - random item.
GetPageByFilter
Gets a page of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public getPageByFilter method from a child class that receives FilterParams and converts them into a filter function.
public virtual
Task<DataPage> GetPageByFilterAsync(IContext context, string filter, PagingParams paging = null, string sort = null, string select = null)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: string - (optional) filter for JSON objects.
- paging: PagingParams - (optional) paging parameters
- sort: string - (optional) sorting JSON object
- select: string - (optional) projection JSON object
- returns: Task<DataPage
> - data page of result by filter
IsOpen
Checks if the component is open.
public virtual
bool IsOpen()
- returns: bool - true if the component is open and false otherwise.
OpenAsync
Opens the component.
public virtual
Task OpenAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
QuoteIdentifier
Adds single quotes to a string.
protected
string QuoteIdentifier(string value)
- value: string - string where quotes need to be added
- returns: string - string with added quotes
SetReferences
Sets references to dependent components.
public virtual
void SetReferences(IReferences references)
- references: IReferences - references to locate the component’s dependencies.
UnsetReferences
Unsets (clears) previously set references to dependent components.
public virtual
void UnsetReferences()
Examples
class MySqlServerPersistence: SqlServerPersistence<MyData>
{
public MySqlServerPersistence(): 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 MySqlServerPersistence();
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" }