Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple dapper+sqlite console app demo #48

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Sample/ConsoleDb/ConsoleDb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RestoreSources>https://api.nuget.org/v3/index.json;$(PackageOutputPath)</RestoreSources>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.10" />
<PackageReference Include="StructId" Version="42.*" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="8.0.11" />
<PackageReference Include="Ulid" Version="1.3.4" />
<PackageReference Include="Dapper" Version="2.1.35" />
</ItemGroup>

<ItemGroup>
<None Update="dapper.db" CopyToOutputDirectory="Always" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions src/Sample/ConsoleDb/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Product>("SELECT * FROM Products WHERE Id = @Id", new { Id = productId });
var product3 = connection.QueryFirst<Product>("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<Ulid>;

public record Product(ProductId Id, string Name);
Binary file added src/Sample/ConsoleDb/dapper.db
Binary file not shown.
6 changes: 6 additions & 0 deletions src/Sample/Sample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalApi", "MinimalApi\Mi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcWebApp", "MvcWebApp\MvcWebApp.csproj", "{98DCCA61-3848-967E-E422-1DEDB4CB8569}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleDb", "ConsoleDb\ConsoleDb.csproj", "{A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{98DCCA61-3848-967E-E422-1DEDB4CB8569}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98DCCA61-3848-967E-E422-1DEDB4CB8569}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98DCCA61-3848-967E-E422-1DEDB4CB8569}.Release|Any CPU.Build.0 = Release|Any CPU
{A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading