Skip to main content

Your first store and CRUD

In this tutorial, you build a console app that runs a Zaris store inside your own process and performs the three core operations — store a value, read it back, and delete it — inspecting the result of each one. You need the .NET SDK; no Zaris server or cluster is required.

1. Create a project

Create a console project to hold the code.

dotnet new console -o zaris-crud
cd zaris-crud

2. Add the SDK

Add the Zaris SDK package, which brings in the client and the in-process store.

dotnet add package Clustron.Zaris.SDK

3. Register an in-process store

Open Program.cs and replace its contents with the following. AddClustronZaris registers a named store from a connection string, and zaris://inproc/demo selects an in-process store so it runs inside this program with no server to install.

using Clustron.Zaris.Client;
using Clustron.Zaris.Client.DependencyInjection;
using Clustron.Zaris.Abstractions;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection()
.AddClustronZaris("demo", "zaris://inproc/demo")
.BuildServiceProvider();

4. Get a client

Resolve a client for the demo store from the service provider. You use this client for every operation in the steps that follow.

var client = await services
.GetRequiredService<IZarisClientProvider>()
.GetAsync("demo");

5. Store a value

Call PutAsync to store a value under a key. The method returns a KvResult that describes the outcome; Zaris does not throw for an expected outcome, so you check the result instead of catching an exception.

var put = await client.PutAsync("user:1", "Ali");

if (!put.IsSuccess)
{
Console.WriteLine($"Put failed: {put.Status} {put.Error}");
return;
}

6. Read the value back

Call GetAsync<string> and read Value only after you confirm the read succeeded. When the key is missing, IsSuccess is false and Status is KvStatus.NotFound.

var get = await client.GetAsync<string>("user:1");

if (get.IsSuccess)
{
Console.WriteLine($"Got: {get.Value}");
}
else if (get.Status == KvStatus.NotFound)
{
Console.WriteLine("Key not found");
}

7. Delete the value

Call DeleteAsync to remove the key. Deleting a key that does not exist is safe: the call reports success or KvStatus.NotFound rather than throwing. Read Mutated when you need to know whether the delete actually removed something.

var delete = await client.DeleteAsync("user:1");

Console.WriteLine($"Deleted: {delete.Mutated}");

var afterDelete = await client.GetAsync<string>("user:1");
Console.WriteLine($"Still present: {afterDelete.IsSuccess}");

8. Run it

Run the program.

dotnet run

You see output similar to the following:

Got: Ali
Deleted: True
Still present: False

What you built

You created a key-value store that runs entirely inside your process and drove it through a full create-read-delete cycle. Along the way you read a KvResult the way Zaris expects: check IsSuccess first, branch on Status to tell a missing key (NotFound) apart from a transient failure such as Timeout, and read Value only after a successful read. Because in-process and distributed modes share the same client API, this same code runs against a cluster once you switch the store to remote mode.

Next steps