-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
84 lines (65 loc) · 2.22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace dapper_unitofwork
{
using System;
using System.Data.SQLite;
using Entities;
internal static class Program
{
private static void Main(string[] args)
{
RunCustomerCrudExample();
RunCategoryCrudExample();
}
private static void RunCustomerCrudExample()
{
static string GetRandomText() => Guid.NewGuid().ToString().Substring(0, 5).ToUpper();
var connectionString = Utilities.GetDatabaseConnectionString();
using var work = new UnitOfWork(new SQLiteConnection(connectionString));
var id = GetRandomText();
// Add
var entity = new Customer {
CustomerId = id,
CompanyName = GetRandomText(),
ContactName = GetRandomText(),
};
work.CustomerRepository.Add(entity);
work.Commit();
Console.WriteLine($"Customer {id} added successfully");
// Update
entity.CompanyName = GetRandomText();
entity.ContactName = GetRandomText();
work.CustomerRepository.Update(entity);
work.Commit();
Console.WriteLine($"Customer {id} updated successfully");
// Delete
work.CustomerRepository.Remove(id);
work.Commit();
Console.WriteLine($"Customer {id} deleted successfully");
}
private static void RunCategoryCrudExample()
{
static string GetRandomText() => Guid.NewGuid().ToString().Substring(0, 5).ToUpper();
var connectionString = Utilities.GetDatabaseConnectionString();
using var work = new UnitOfWork(new SQLiteConnection(connectionString));
var entity = new Category
{
CategoryName = GetRandomText(),
Description = GetRandomText(),
};
// Add
var id = work.CategoryRepository.Add(entity);
work.Commit();
Console.WriteLine($"Category {id} added successfully");
// Update
entity.CategoryName = GetRandomText();
entity.Description = GetRandomText();
work.CategoryRepository.Update(entity);
work.Commit();
Console.WriteLine($"Category {id} updated successfully");
// Delete
work.CategoryRepository.Remove(id);
work.Commit();
Console.WriteLine($"Category {id} deleted successfully");
}
}
}