MemoryPersistence<T> class Null safety

Abstract persistence component that stores data in memory.

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 this.items property and calling save method.

The component supports loading and saving items from another data source. That allows to use it as a base class for file and other types of persistence components that cache all data in memory.

References

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages

Example

class MyMemoryPersistence extends MemoryPersistence<MyData> {

   Future<MyData> getByName(String? correlationId, String name) async {
        var item = items.firstWhere((d) => d.name == name);
       return item;
    });

    Future<MyData> set(String correlatonId, MyData item) async {
        items = items.where((d) => d.name != name);
        items.add(item);
        await save(correlationId);
        return item;
    }
}

var persistence = MyMemoryPersistence();

persistence.set("123", { name: "ABC" })
var item = await persistence.getByName("123", "ABC")
print(item);                   // Result: { name: "ABC" }
Implemented types
Implementers

Constructors

MemoryPersistence([ILoader<T>? loader, ISaver<T>? saver])
Creates a new instance of the persistence.

Properties

hashCode int
The hash code for this object.
read-onlyinherited
items List<T>
read / write
loader ILoader<T>?
read / write
logger CompositeLogger
read / write
maxPageSize int
read / write
opened bool
read / write
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited
saver ISaver<T>?
read / write

Methods

clear(String? correlationId) Future
Clears component state.
override
close(String? correlationId) Future
Closes component and frees used resources.
override
create(String? correlationId, T item) Future<T?>
Creates a data item.
deleteByFilterEx(String? correlationId, Function filter) Future
Deletes data items that match to a given filter.
getCountByFilterEx(String? correlationId, Function? filter) Future<int>
Gets a count of data items retrieved by a given filter.
getListByFilterEx(String? correlationId, Function? filter, Function? sort, dynamic select) Future<List<T>>
Gets a list of data items retrieved by a given filter and sorted according to sort parameters.
getOneRandom(String? correlationId, Function? filter) Future<T?>
Gets a random item from items that match to a given filter.
getPageByFilterEx(String? correlationId, Function? filter, PagingParams? paging, Function? sort, [dynamic select]) Future<DataPage<T>>
Gets a page of data items retrieved by a given filter and sorted according to sort parameters.
isOpen() bool
Checks if the component is opened.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
open(String? correlationId) Future
Opens the component.
override
save(String? correlationId) Future
Saves items to external data source using configured saver component.
setReferences(IReferences references) → void
Sets references to dependent components.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited