First collection
Define a document type and register a typed collection.
Define a document type
A document is an ordinary C# class or record. NetlyDB assigns each stored document a Guid identifier.
Nested objects are stored with the document and can be queried and indexed by path.
public record Order
{
public Guid Id { get; init; }
public string Reference { get; init; } = "";
public string CustomerName { get; init; } = "";
public string Country { get; init; } = "";
public string Status { get; init; } = "open";
public decimal Total { get; init; }
public int LineCount { get; init; }
public Address? ShippingAddress { get; init; }
}
public record Address(string City, string Country);
Register a typed collection
AddNetlyDB registers the engine; AddNetlyDBCollection<T>() registers a typed collection
as INetlyDBCollection<T> in dependency injection. Call it once per document type you want to inject.
The collection name defaults to the type name, so Order maps to an Order collection.
var builder = Host.CreateApplicationBuilder(args); // or WebApplication.CreateBuilder(args)
builder.Services
.AddNetlyDB(options => options.StorageRoot = "netlydb-data")
.AddNetlyDBCollection<Order>();
using var host = builder.Build();
await host.StartAsync();
var orders = host.Services.GetRequiredService<INetlyDBCollection<Order>>();
Inject it where you need it
public sealed class OrderService(INetlyDBCollection<Order> orders)
{
public Task<IReadOnlyList<Order>> OpenOrdersAsync(string country) =>
orders.ListAsync(x => x.Country == country && x.Status == "open");
}
To use several collections of one type, or to open collections at runtime, see Documents & collections. Next: basic CRUD.