Getting Started with Tables as C# Classes in the ScalarDB Cluster .NET Client SDK
The ScalarDB Cluster .NET Client SDK helps you write code to access a cluster by abstracting ScalarDB tables as C# objects. After defining a class that represents a table in the cluster, you can ensure that a column name or its type won't be mixed up when querying the cluster. In addition, if a table's structure changes, you can apply the changes to the code by using the refactoring feature in your IDE.
Although we recommend using asynchronous methods, as in the following examples, you can use synchronous methods instead.
Create classes for all ScalarDB tables
To work with ScalarDB tables as C# objects, you must create a class for each table that you want to use. For example:
using System.ComponentModel.DataAnnotations.Schema;
using ScalarDB.Net.Client.DataAnnotations;
// ...
[Table("ns.statements")]
public class Statement
{
[Column("order_id", Order = 0), PartitionKey]
public string OrderId { get; set; } = String.Empty;
[Column("item_id", Order = 1), ClusteringKey]
public int ItemId { get; set; }
[Column("count", Order = 2)]
public int Count { get; set; }
}
Execute CRUD operations
After creating a class for each table, you can use the classes as objects by using the generic GetAsync
, ScanAsync
, InsertAsync
, UpdateAsync
, DeleteAsync
, PutAsync
, or MutateAsync
method of IDistributedTransaction
(or more specifically, of ITransactionCrudOperable
).
To use these generic methods, add the following namespace to the using
section:
using ScalarDB.Net.Client.Extensions;
Get one object by using the GetAsync
method
var keys = new Dictionary<string, object>
{
{ nameof(Statement.OrderId), "1" }
};
var statement = await transaction.GetAsync<Statement>(keys);
Console.WriteLine($"ItemId: {statement.ItemId}, Count: {statement.Count}");
Get multiple objects by using the ScanAsync
method
var startKeys = new Dictionary<string, object>
{
{ nameof(Statement.OrderId), "1" },
{ nameof(Statement.ItemId), 3 }
};
var endKeys = new Dictionary<string, object>
{
{ nameof(Statement.ItemId), 6}
};
var statements = await transaction.ScanAsync<Statement>(startKeys, endKeys);
foreach (var s in statements)
Console.WriteLine($"ItemId: {s.ItemId}, Count: {s.Count}");
Insert a new object by using the InsertAsync
method
var statement = new Statement
{
OrderId = "2",
ItemId = 4,
Count = 8
};
await transaction.InsertAsync(statement);
Update an object by using the UpdateAsync
method
// ...
statement.ItemId = 4;
statement.Count = 8;
await transaction.UpdateAsync(statement);
Delete an object by using the DeleteAsync
method
// ...
await transaction.DeleteAsync(statement);
Upsert an object by using the PutAsync
method
var statement = new Statement
{
OrderId = "2",
ItemId = 4,
Count = 8
};
await transaction.PutAsync(statement);
Put and delete multiple objects at once by using the MutateAsync
method
var statement = new Statement
{
OrderId = "2",
ItemId = 4,
Count = 16
};
// ...
await client.MutateAsync(objectsToPut: new[] { statement },
objectsToDelete: new[] { statement2 });