diff --git a/db-subclass-to-dto.sln b/db-subclass-to-dto.sln index 8d2c2b1..41bdd13 100644 --- a/db-subclass-to-dto.sln +++ b/db-subclass-to-dto.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{07C2787E EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractMethod", "tools\ExtractMethod\ExtractMethod.csproj", "{3E8944E9-BB07-424B-B3A6-59FB072BAB3C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Before.Console", "src\Before.Console\Before.Console.csproj", "{AC39E693-E83D-4B50-A120-0DE259108AF2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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|x86.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -84,5 +98,6 @@ Global {E65C0410-A863-44B0-88A4-6C74EF929419} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} {F5E0C182-CBC3-4440-AF8F-32887F9B589C} = {0AB3BF05-4346-4AA6-1389-037BE0695223} {3E8944E9-BB07-424B-B3A6-59FB072BAB3C} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84} + {AC39E693-E83D-4B50-A120-0DE259108AF2} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} EndGlobalSection EndGlobal diff --git a/src/Before.Console/Before.Console.csproj b/src/Before.Console/Before.Console.csproj new file mode 100644 index 0000000..c936346 --- /dev/null +++ b/src/Before.Console/Before.Console.csproj @@ -0,0 +1,14 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + diff --git a/src/Before.Console/Program.cs b/src/Before.Console/Program.cs new file mode 100644 index 0000000..ecec572 --- /dev/null +++ b/src/Before.Console/Program.cs @@ -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()) +{ + 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."); +}