AbraxasDB - 1 - Plain objects and persistence

CSharp · Console

/Program.cs

// @nuget: AbraxasDB -Version 1.0.4
// AbraxasDB playground 1: plain C# objects, saved records, and reopening.
// Try changing the names/ages, or add another customer.
// Each Run uses a fresh temporary database and removes it when finished.
using System;
using System.IO;
using AbraxasDB;

public static class Program
{
    /// <summary>Creates records from ordinary C# objects, retrieves one by its allocated ID, and verifies it after reopening the database.<br/></summary>
    public static void Main()
    {
        const string firstName = "Ada";
        const int firstAge = 37;
        string directory = Path.Combine(Path.GetTempPath(), "abraxas-records-" + Guid.NewGuid().ToString("N"));
        ulong adaId;
        try
        {
            using (var database = RecordBase.Create(directory, "playground"))
            {
                var customers = database.CreateOrOpenStore<Customer>("customers");
                adaId = customers.Put(new Customer { Name = firstName, Age = firstAge });
                ulong graceId = customers.Put(new Customer { Name = "Grace", Age = 85 });
                Console.WriteLine("Saved " + firstName + " with ID " + adaId + " and Grace with ID " + graceId);
                Customer customer = customers.Get(adaId);
                Console.WriteLine("Read by ID: " + customer.Name + ", age " + customer.Age);
                Console.WriteLine("Record count: " + customers.Aggregates.Count());
            }

            using (var database = RecordBase.Open(directory, "playground"))
            {
                var customers = database.OpenStore<Customer>("customers");
                Customer customer = customers.Get(adaId);
                Console.WriteLine("After closing and reopening: " + customer.Name + ", age " + customer.Age);
                Console.WriteLine("Reopened record count: " + customers.Aggregates.Count());
                if (customer.Name != firstName || customer.Age != firstAge)
                    throw new InvalidOperationException("Reopened data did not match the sample.");
            }
            Console.WriteLine("EXAMPLE_01_PASS");
        }
        finally
        {
            if (Directory.Exists(directory)) Directory.Delete(directory, true);
        }
    }
}

/// <summary>An ordinary application type; this example requires no persistence base class or mapping attributes.<br/></summary>
public sealed class Customer
{
    /// <summary>Gets or sets the customer name.<br/></summary>
    public string Name { get; set; } = "";
    /// <summary>Gets or sets the customer age.<br/></summary>
    public int Age { get; set; }
}
;