Add Before.Console — console app demonstrating ActiveRecord pattern

Scaffold Before.Console project referencing the Before library.
Implements a straight-line demo that creates clients/orders, saves
them via DbBase.Context (singleton), lists all clients with orders,
and finds a client by name using Client.FindByName().
This commit is contained in:
2026-09-14 20:50:25 +01:00
parent 8b46fccf08
commit 06a12e55f8
3 changed files with 84 additions and 1 deletions
+14
View File
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Before\Before.csproj" />
</ItemGroup>
</Project>
+54
View File
@@ -0,0 +1,54 @@
using Before;
// ---------------------------------------------------------------------------
// Before.Console — a straight-line demo of the ActiveRecord-style pattern.
//
// The console app sets DbBase.Context (the shared singleton) once at startup,
// then creates client/order entities, saves them through the context, and
// queries back using the ActiveRecord entry points:
// • Client.FindByName(name, context)
// • listing via DbBase.Context!.Tracked
// ---------------------------------------------------------------------------
var ctx = new DbContext();
DbBase.Context = ctx;
// ----- seed data ----------------------------------------------------------
var jane = new Client { Name = "Jane Doe" };
var bob = new Client { Name = "Bob Smith" };
jane.Orders.Add(new Order { Description = "Design consultation" });
jane.Orders.Add(new Order { Description = "Website redesign" });
bob.Orders.Add(new Order { Description = "Monthly retainer" });
// Orders need to be attached too so they get IDs:
ctx.Attach(jane.Orders[0]);
ctx.Attach(jane.Orders[1]);
ctx.Attach(bob.Orders[0]);
ctx.Attach(jane);
ctx.Attach(bob);
ctx.Save();
// ----- list all clients ---------------------------------------------------
Console.WriteLine("=== All Clients ===");
foreach (var client in DbBase.Context!.Tracked.OfType<Client>())
{
Console.WriteLine($"{client.Name} [{client.Id}]");
foreach (var order in client.Orders)
Console.WriteLine($" - order [{order.Id}]: {order.Description}");
}
// ----- find by name -------------------------------------------------------
Console.WriteLine("\n=== Find by Name ===");
var found = Client.FindByName("Jane Doe", DbBase.Context);
if (found is not null)
{
Console.WriteLine($"Found: {found.Name} [{found.Id}]");
Console.WriteLine($" Orders: {found.Orders.Count}");
}
else
{
Console.WriteLine("Not found.");
}