-
Notifications
You must be signed in to change notification settings - Fork 1
10. Sharpify.Data
For basic information check this
Lets see an high performance example, obviously, if you are using this package you care about performance, and here we appreciate that, a lot.
using var database = Database.CreateOrLoad(new DatabaseConfiguration {
Path = path, // local
EncryptionKey = "mykey",
SerializeOnUpdate = true,
IgnoreCase = true
});
lets first create a value type that implements IMemoryPackable
such as:
[MemoryPackable]
public readonly partial record struct Dog(string Species, int Age, float Weight);
Notice the MemoryPackable
attribute from MemoryPack, this will implement the IMemoryPackable
interface behind the scenes using a source generator. Database
utilizes this to enable unrivaled performance.
database.Upsert<Dog>("Brian", new("Bipedal Talking Dog", 20));
Dog? brian = database.Get<Dog>("Brian"); // Get returns null if the key doesn't exist
// because SerializeOnUpdate options was chosen, it will handle this automatically
// otherwise use .Serialize() or .SerializeAsync()
// We also use the filter
var table = Database.CreateMemoryPackFilter<Dog>();
table.Upsert("Buster", new Dog("Buster", 5));
bool exists = table.TryGetValue("Buster", out Dog buster);
As an alternative to MemoryPackable
the database also has an option to create a FlexibleDatabaseFilter{T}
. This filter can be used when you want to implement easy access to the existing apis with a type that can't implement IMemoryPackable{T}
, one example would be collections.
For that the type itself, would need to implement the interface IFilterable{T}
. If the type has the MemoryPackable
attribute, it means the serializer knows how to handle it, it would be required to add practically a single line of code to each of the methods from IFilterable{T}
to implement it. If some methods aren't required, you could simply return null;
.
When your type lets say TCustom
implements IFilterable<T>
, the following method will become available:
var table = Database.CreateFlexibleFilter<TCustom>();
this filter implements the same interface as the MemoryPackFilter
and will support the same apis. To allow a very uniform and cohesive codebase.