From e9bd923aa175b1301066e613db63acd89abd1645 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sat, 21 Dec 2024 04:06:06 -0300 Subject: [PATCH] Add simple dapper+sqlite console app demo --- src/Sample/ConsoleDb/ConsoleDb.csproj | 24 +++++++++++++++++++++ src/Sample/ConsoleDb/Program.cs | 29 ++++++++++++++++++++++++++ src/Sample/ConsoleDb/dapper.db | Bin 0 -> 12288 bytes src/Sample/Sample.sln | 6 ++++++ 4 files changed, 59 insertions(+) create mode 100644 src/Sample/ConsoleDb/ConsoleDb.csproj create mode 100644 src/Sample/ConsoleDb/Program.cs create mode 100644 src/Sample/ConsoleDb/dapper.db diff --git a/src/Sample/ConsoleDb/ConsoleDb.csproj b/src/Sample/ConsoleDb/ConsoleDb.csproj new file mode 100644 index 0000000..01160c0 --- /dev/null +++ b/src/Sample/ConsoleDb/ConsoleDb.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + enable + enable + https://api.nuget.org/v3/index.json;$(PackageOutputPath) + true + + + + + + + + + + + + + + + diff --git a/src/Sample/ConsoleDb/Program.cs b/src/Sample/ConsoleDb/Program.cs new file mode 100644 index 0000000..a50b83e --- /dev/null +++ b/src/Sample/ConsoleDb/Program.cs @@ -0,0 +1,29 @@ +using ConsoleDb; +using Dapper; +using Microsoft.Data.Sqlite; + +SQLitePCL.Batteries.Init(); + +using var connection = new SqliteConnection("Data Source=dapper.db") + .UseStructId(); + +connection.Open(); + +// Seed data +var productId = Ulid.NewUlid(); +var product = new Product(new ProductId(productId), "Product"); + +connection.Execute("INSERT INTO Products (Id, Name) VALUES (@Id, @Name)", new Product(ProductId.New(), "Product1")); +connection.Execute("INSERT INTO Products (Id, Name) VALUES (@Id, @Name)", product); +connection.Execute("INSERT INTO Products (Id, Name) VALUES (@Id, @Name)", new Product(ProductId.New(), "Product2")); + +// showcase we can query by the underlying ulid +var product2 = connection.QueryFirst("SELECT * FROM Products WHERE Id = @Id", new { Id = productId }); +var product3 = connection.QueryFirst("SELECT * FROM Products WHERE Id = @Id", new { Id = new ProductId(productId) }); + +Console.WriteLine("Found saved product by value: " + product.Equals(product2)); +Console.WriteLine("Found saved product by id: " + product.Equals(product3)); + +public readonly partial record struct ProductId : IStructId; + +public record Product(ProductId Id, string Name); \ No newline at end of file diff --git a/src/Sample/ConsoleDb/dapper.db b/src/Sample/ConsoleDb/dapper.db new file mode 100644 index 0000000000000000000000000000000000000000..b0d7cd6cf057b55e36759e077f439ab7479ab227 GIT binary patch literal 12288 zcmeI#&r8EF6u|MMiVk6Px19>_s33~?4_HSI!&+yp;7(n&84NayTL)hK>-=B*3q0CP za^894`yfAFLIU~Z^qwA*Sz~2dFBfGa*Q%$qRyQI_Dc!b%w)t@Rp-;fp6dt%5I_I{1Q0*~0R#|0009IL*jIrQ_b}@J bef?hEg#ZEwAb