Start here

Getting started

Install the package, describe your data as C# classes, and store, query, update and delete it in a few lines.

Install

Add the package to your project:

dotnet add package Xod

XOD targets .NET 8 and .NET 9 and has no other dependencies.

Describe your data

Your classes are the schema. A property called Id becomes the primary key, and when it is a number or a Guid (nullable included), XOD fills it in for you.

using Xod;

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }

    [Children]
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public string Status { get; set; }

    [ParentKey(nameof(CustomerId))]
    public Customer Customer { get; set; }
}

[Children] and [ParentKey] tell XOD that a customer owns its orders. You can skip them for now; Relationships explains them.

Open a database

using var db = new XodContext("data/app.xod");

If the file doesn’t exist yet, XOD creates it, together with the folder’s table and page files. Keep one XodContext per database for the life of your application and dispose it when you’re done: it is safe to share between threads. Opening a second context on the same database throws DatabaseLockedException (see Processes).

Insert

var nadia = db.Insert(new Customer
{
    Name = "Nadia",
    Orders = new()
    {
        new Order { Status = "open" },
        new Order { Status = "shipped" },
    },
});

Console.WriteLine(nadia.Id); // 1

Insert returns the object with its key filled in. The two orders are stored too, each pointing at its customer.

Query

// every open order
var open = db.Query<Order>(o => o.Status == "open").ToList();

// one customer, with its orders loaded
var customer = db.Find<Customer>(c => c.Name == "Nadia");
Console.WriteLine(customer.Orders.Count); // 2

Query returns every match; Find returns the first one, or null.

Update and delete

customer.Name = "Nadia K.";
db.Update(customer);

var shipped = db.Find<Order>(o => o.Status == "shipped");
db.Delete(shipped);

Every call has written the database’s files when it returns. To make several writes land together, or not at all, see Transactions.

Look at the files

Open the folder. With the default XML format each table has its own files: Customer.xtab lists the customer table’s pages, and each page file (<id>.xpag) holds rows. The customer’s row lists its orders by key and by where each one is stored; the order itself is a row in a page of the Order table, pointing back at its customer. Laid out on several lines (the files keep each page on one):

<!-- a page of the Customer table -->
<Row code="a4051fdbc"><Customer>
  <Id>1</Id>
  <Name>Nadia K.</Name>
  <Orders dataType="Order" collType="generic" refType="children">
    <Order Id="1">debcb586a.c5791e480</Order>
  </Orders>
</Customer></Row>

<!-- a page of the Order table -->
<Row code="c5791e480"><Order>
  <Id>1</Id>
  <CustomerId>1</CustomerId>
  <Status>open</Status>
  <Customer refType="parent" Id="1">792bb61a6.a4051fdbc</Customer>
</Order></Row>

That’s the point of XOD: git diff shows what changed, a backup is a folder copy, and a bug report can include the data. Storage formats and files lists every file and the other two formats.

Source on GitHub · Report a problem with this page