-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
42 lines (35 loc) · 1.63 KB
/
Program.cs
File metadata and controls
42 lines (35 loc) · 1.63 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
using Microsoft.Extensions.Configuration;
using NetCoreManualDI.Application.School.Dtos;
using NetCoreManualDI.Domain.Core.Students;
using NetCoreManualDI.Persistence.Design;
var connectionString = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build()
.GetConnectionString("DefaultConnection");
using (var schoolDbContext = new SchoolDbContext(connectionString, true))
{
await schoolDbContext.Database.EnsureDeletedAsync();
await schoolDbContext.Database.EnsureCreatedAsync();
}
var schoolContextFactory = () => NetCoreManualDI.Persistence.Factories.CreateSchoolContext(connectionString, true);
var eventsDispatcherFactory = NetCoreManualDI.EventsDispatching.Factories.CreateEventsDispatcher;
var schoolServiceFactory = () => NetCoreManualDI.Application.Factories.CreateSchoolService(schoolContextFactory, eventsDispatcherFactory);
using (var schoolService = schoolServiceFactory())
{
var courseNames = new[] { "Math", "Physics", "History" };
foreach (var name in courseNames)
await schoolService.RegisterCourse(new RegisterCourseDto(name));
await schoolService.RegisterStudent(new RegisterStudentDto("Otto", courseNames.First()));
await schoolService.EnrollStudent(new EnrollStudentDto("Otto", "Physics"));
}
using (var schoolContext = schoolContextFactory())
{
var student = await schoolContext.Students.GetByNameAsync("Otto".ToStudentName());
if (student != null)
{
Console.WriteLine($"{student.Name}, with enrollments:");
foreach(var e in student.Enrollments)
Console.WriteLine(e.Course.Name);
}
}