Path: redb/redb.Examples/Examples/E165_BaseFieldsBatch.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/Examples/E165_BaseFieldsBatch.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Models.Contracts;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Demonstrates batch save/load of non-generic RedbObject with base fields.
/// Efficient for bulk operations on simple objects without Props.
/// All data stored in _objects table - no props rows.
///
[ExampleMeta("E165", "Base Fields - Batch Operations", "Core",
ExampleTier.Free, 165, "BaseFields", "Batch", "NonGeneric", RelatedApis = ["RedbObject", "IRedbService.SaveAsync"])]
public class E165_BaseFieldsBatch : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Create batch of non-generic objects
var objects = new List();
var batchId = DateTime.UtcNow.Ticks;
for (int i = 0; i < 1000; i++)
{
objects.Add(new RedbObject
{
name = $"Batch_BaseFields_{batchId}_{i}",
value_string = $"Item_{i}",
value_long = i * 100,
value_bool = i % 2 == 0
});
}
// Batch save (cast to IRedbObject for batch API)
var ids = await redb.SaveAsync(objects.Cast());
// Batch load
var loaded = await redb.LoadAsync(ids);
sw.Stop();
if (loaded.Count != 1000)
{
return Fail("E165", "Base Fields - Batch Operations", ExampleTier.Free, sw.ElapsedMilliseconds,
$"Expected 1000 objects, got {loaded.Count}.");
}
// Verify data integrity
var verified = loaded.All(x => x.ValueString != null && x.ValueString.StartsWith("Item_"));
var result = Ok("E165", "Base Fields - Batch Operations", ExampleTier.Free, sw.ElapsedMilliseconds, loaded.Count,
[$"Saved and loaded: {loaded.Count} objects",
$"IDs: [{string.Join(", ", ids.Take(3))}...]",
$"Data verified: {verified}"]);
// Cleanup: one delete for the whole batch
await redb.DeleteAsync(ids);
return result;
}
}