-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (34 loc) · 1.39 KB
/
Program.cs
File metadata and controls
44 lines (34 loc) · 1.39 KB
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
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.DependencyInjection;
using myNOC.EntityFramework.Query.Extensions;
using QuerySample.Data;
using QuerySample.Entities;
using QuerySample.Queries;
await SeedSampleData();
IServiceCollection services = new ServiceCollection();
services.AddQueryPattern();
var provider = services.BuildServiceProvider();
var queryRepo = provider.GetRequiredService<IAddressBookContextRepository>();
var result = await queryRepo.Query(new ContactGetAll());
DisplayResults("Return All Contacts", result);
Console.WriteLine();
result = await queryRepo.Query(new ContactNameContains("a"));
DisplayResults("Contacts Where Name Contain 'a'", result);
Console.WriteLine();
var id = await queryRepo.Query(new ContactGetIdByName("Bob"));
Console.WriteLine($"Bob's Id is: {id}");
static async Task SeedSampleData()
{
var addressBook = new AddressBookDbContext();
addressBook.Add(new ContactEntity { Id = 1, Name = "Abby" });
addressBook.Add(new ContactEntity { Id = 2, Name = "Bob" });
addressBook.Add(new ContactEntity { Id = 3, Name = "Charlie" });
addressBook.Add(new ContactEntity { Id = 4, Name = "David" });
await addressBook.SaveChangesAsync();
}
static void DisplayResults(string title, IEnumerable<QuerySample.Models.ContactModel> result)
{
Console.WriteLine(title);
foreach (var item in result)
Console.WriteLine(item.DisplayName);
}