Skip to content

Commit 6d1d46f

Browse files
committed
Added LiteDB Example
1 parent 1a54a2a commit 6d1d46f

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

LiteDatabase/LiteDatabase.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="LiteDB" Version="5.0.12" />
12+
</ItemGroup>
13+
14+
</Project>

LiteDatabase/LiteDatabase.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LiteDatabase", "LiteDatabase.csproj", "{7F76AA88-BBDF-4031-8FA2-1B159CEF9B4F}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{7F76AA88-BBDF-4031-8FA2-1B159CEF9B4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{7F76AA88-BBDF-4031-8FA2-1B159CEF9B4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{7F76AA88-BBDF-4031-8FA2-1B159CEF9B4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{7F76AA88-BBDF-4031-8FA2-1B159CEF9B4F}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

LiteDatabase/Program.cs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using LiteDB;
2+
3+
using var database = new LiteDatabase("database.db");
4+
int selection;
5+
do
6+
{
7+
Console.WriteLine("BlogPost Editor");
8+
Console.WriteLine("1: Write blog post");
9+
Console.WriteLine("2: Read blog post");
10+
Console.WriteLine("3: Get count of all published blog posts");
11+
Console.WriteLine("4: Get blog posts with tag");
12+
Console.WriteLine("5: Quit");
13+
if (!int.TryParse(Console.ReadLine(), out selection))
14+
{
15+
Console.WriteLine("Could not understand your selection. Try again");
16+
continue;
17+
}
18+
19+
Console.WriteLine();
20+
21+
switch (selection)
22+
{
23+
case 1:
24+
WriteBlogPost(database);
25+
break;
26+
case 2:
27+
ReadBlogPost(database);
28+
break;
29+
case 3:
30+
GetAll(database);
31+
break;
32+
case 4:
33+
GetAllByTag(database);
34+
break;
35+
case 5:
36+
break;
37+
default:
38+
Console.WriteLine("Could not understand your selection. Try again");
39+
break;
40+
}
41+
} while (selection != 5);
42+
43+
void WriteBlogPost(ILiteDatabase db)
44+
{
45+
Console.Write("Title:");
46+
var title = Console.ReadLine();
47+
Console.Write("Published? (true/false): ");
48+
bool.TryParse(Console.ReadLine(), out var isPublished);
49+
Console.Write("Tags (comma-seperated):");
50+
var tags = Console.ReadLine();
51+
var blogPost = new BlogPost { Title = title, IsPublished = isPublished, Tags = tags.Split(",").ToList() };
52+
db.GetCollection<BlogPost>().Insert(blogPost);
53+
Console.WriteLine($"Created blog post with id: {blogPost.Id}");
54+
}
55+
56+
void ReadBlogPost(ILiteDatabase db)
57+
{
58+
Console.Write("Which id?: ");
59+
if (!int.TryParse(Console.ReadLine(), out var id))
60+
{
61+
Console.WriteLine("Id has to be an integer");
62+
}
63+
64+
var blogPost = db.GetCollection<BlogPost>().FindById(id);
65+
66+
Console.WriteLine(blogPost is null
67+
? "Could not find blog post"
68+
: $"Found blog post with title: {blogPost.Title}");
69+
}
70+
71+
void GetAll(ILiteDatabase db)
72+
{
73+
var published = db.GetCollection<BlogPost>().Count(bp => bp.IsPublished);
74+
Console.WriteLine($"All published {published}");
75+
}
76+
77+
void GetAllByTag(ILiteDatabase db)
78+
{
79+
Console.Write("Which tag: ");
80+
var tag = Console.ReadLine();
81+
82+
var blogPosts = db.GetCollection<BlogPost>().Query();
83+
84+
var blogPostWithTag =
85+
(from bp in blogPosts
86+
where bp.Tags.Contains(tag)
87+
select bp).ToList();
88+
89+
Console.WriteLine($"Found {blogPostWithTag.Count} entries");
90+
foreach (var post in blogPostWithTag)
91+
{
92+
Console.WriteLine($"Title: {post.Title}");
93+
}
94+
}
95+
96+
public class BlogPost
97+
{
98+
public int Id { get; set; }
99+
100+
public string Title { get; set; }
101+
102+
public bool IsPublished { get; set; }
103+
104+
public List<string> Tags { get; set; }
105+
}

LiteDatabase/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# LiteDB - A .NET embedded NoSQL database
2+
3+
In this article we will have a closer look at **LiteDB**, a .NET NoSQL Document Store in a single data file. We will discover the advantages of **LiteDB** and why it is a viable candidate for your next project.
4+
5+
6+
We will also explore what are the differences between a **NoSQL** and a *classical* **SQL** database are and what this has to do with the reminiscent **SQL CE** or the more modern **SQLite** database.
7+
8+
Found [here](https://steven-giesel.com/blogPost/d376a46c-ec5b-4e37-81b3-23772c47ed85)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| ---------------------------------------------------------------------------------------- | ------------ |
7+
| [LiteDB - A .NET embedded NoSQL database](LiteDatabase/) | 19.10.2022 |
78
| [Cursed C# - Doing shenanigans in C#](CursedCSharp/) | 15.10.2022 |
89
| [Introduction to WebApplicationFactory](WebAppFactory/) | 02.10.2022 |
910
| [EF7 - Bulk updates and Bulk deletes](EF7Bulk/) | 21.08.2022 |

0 commit comments

Comments
 (0)