Inherits: IConfigurable, IReferenceable, IOpenable, ICleanable
Description
The MemoryPersistence class allows you to create persistence components that store data in memory.
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._items property and calling the Save method.
 - The component supports loading and saving items from another data source. This allows to use it as a base class for file and other types of persistence components that cache all data in memory.
 
Configuration parameters
options:
- max_page_size: maximum number of items returned in a single page (default: 100)
 
References
- *:logger:*:*:1.0 - (optional) (../../../components/log/ilogger) components to pass log messages
 
Constructors
Creates a new instance of the memory persistence component.
- loader: ILoader
- (optional) loader used to load items from an external datasource.  - saver: ISaver
- (optional) saver used to save items to an external datasource.  
Creates a new instance of the persistence.
publicMemoryPersistence()
Fields
Instance methods
Clear
Clears the component’s state.
publicTask ClearAsync(string correlationId)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 
Configure
Configures the component by passing its configuration parameters.
public virtualvoid Configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
 
Create
Creates a data item.
public virtualTask<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
 
DeleteByFilter
Deletes data items that match to a given filter. This method shall be called by a public DeleteByFilter method from a child class that receives FilterParams and converts them into a filter function.
publicTask DeleteByFilterAsync(string correlationId, IList<Func<T, bool>> matchFunctions)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 - matchFunctions: IList<Func<T, bool>> - (optional) filter function used to filter items.
 
GetCountByFilter
Note: this method is not implemented yet
Gets the number of items retrieved by a given filter.
This method shall be called by a public GetCountByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedGetCountByFilter(correlationId: string, filter: object): Task<int>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 - filter: object - id of the item to be deleted
 - returns: Task<int> - number of data items that satisfy the filter.
 
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.
publicTask<List<T>> GetListByFilterAsync(string correlationId, IList<Func<T, bool>> matchFunctions)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 - matchFunctions: IList<Func<T, bool>> - (optional) filter function used to filter items
 - 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.
publicTask<T> GetOneRandomAsync(string correlationId, IList<Func<T, bool>> matchFunctions)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 - matchFunctions: IList<Func<T, bool>> - (optional) filter function to filter items.
 - 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.
publicasync Task<DataPage> GetPageByFilterAsync(string correlationId, IList<Func<T, bool>> matchFunctions, PagingParams paging) 
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 - matchFunctions: IList<Func<T, bool>> - filter function used to filter items
 - paging: PagingParams - (optional) paging parameters
 - returns: Task<DataPage
> - data page with filterd results.  
IsOpen
Checks if the component is open.
publicbool IsOpen()
- returns: bool - true if the component is open and false otherwise.
 
LoadAsync
Loads items.
privateTask LoadAsync(string correlationId)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 
OpenAsync
Opens the component.
publicTask OpenAsync(string correlationId)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 
SaveAsync
Saves items to an external data source using a configured saver component.
publicTask SaveAsync(string correlationId)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
 
SetReferences
Sets the component’s references. References must match configured dependencies.
publicvoid SetReferences(IReferences references)
- references: IReferences - references to set.
 
Examples
class MyMemoryPersistence extends MemoryPersistence<MyData>
{
    public MyData GetByName(String correlationId, String name)
    {
        MyData item = _items.Find((mydata) => { return mydata.Name == name; });
        ...
        return item;
    } 
    public MyData Set(String correlatonId, MyData item)
    {
        this._items = _items.Filter((mydata) => { return mydata.Name != name; });
        ...
        this._items.add(item);
        this.save(correlationId);
    }
    
var persistence = new MyMemoryPersistence();
persistence.Set("123", new MyData("ABC"));
Console.Out.WriteLine(persistence.getByName("123", "ABC")).toString(); // Result: { name: "ABC" }