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:
+16
-1
@@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.0.31903.59
|
VisualStudioVersion = 17.0.31903.59
|
||||||
@@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{07C2787E
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractMethod", "tools\ExtractMethod\ExtractMethod.csproj", "{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractMethod", "tools\ExtractMethod\ExtractMethod.csproj", "{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Before.Console", "src\Before.Console\Before.Console.csproj", "{AC39E693-E83D-4B50-A120-0DE259108AF2}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -75,6 +77,18 @@ Global
|
|||||||
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}.Release|x64.Build.0 = Release|Any CPU
|
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}.Release|x86.ActiveCfg = Release|Any CPU
|
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}.Release|x86.Build.0 = Release|Any CPU
|
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -84,5 +98,6 @@ Global
|
|||||||
{E65C0410-A863-44B0-88A4-6C74EF929419} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
{E65C0410-A863-44B0-88A4-6C74EF929419} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
||||||
{F5E0C182-CBC3-4440-AF8F-32887F9B589C} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
|
{F5E0C182-CBC3-4440-AF8F-32887F9B589C} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
|
||||||
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84}
|
{3E8944E9-BB07-424B-B3A6-59FB072BAB3C} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84}
|
||||||
|
{AC39E693-E83D-4B50-A120-0DE259108AF2} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -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.");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user