|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Autofac; |
| 4 | +using NEventLite.Repository; |
| 5 | +using NEventLite.Samples.Common.Domain.Schedule; |
| 6 | +using NEventLite.Samples.Common.Handlers; |
| 7 | +using Newtonsoft.Json; |
| 8 | + |
| 9 | +namespace NEventLite.Samples.Autofac |
| 10 | +{ |
| 11 | + public static class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + RunSample().ConfigureAwait(false).GetAwaiter().GetResult(); |
| 16 | + } |
| 17 | + |
| 18 | + public static async Task RunSample() |
| 19 | + { |
| 20 | + var builder = new ContainerBuilder(); |
| 21 | + builder.RegisterModule<NEventLiteModule>(); |
| 22 | + var container = builder.Build(); |
| 23 | + |
| 24 | + Guid scheduleId; |
| 25 | + Guid todoId; |
| 26 | + |
| 27 | + await using (var scope = container.BeginLifetimeScope()) |
| 28 | + { |
| 29 | + var handler = scope.Resolve<CreateScheduleHandler>(); |
| 30 | + scheduleId = await handler.HandleAsync("test schedule"); |
| 31 | + } |
| 32 | + |
| 33 | + await using (var scope = container.BeginLifetimeScope()) |
| 34 | + { |
| 35 | + var handler = scope.Resolve<CreateTodoHandler>(); |
| 36 | + todoId = await handler.HandleAsync(scheduleId, "todo item 1"); |
| 37 | + } |
| 38 | + |
| 39 | + await using (var scope = container.BeginLifetimeScope()) |
| 40 | + { |
| 41 | + var handler = scope.Resolve<CreateTodoHandler>(); |
| 42 | + await handler.HandleAsync(scheduleId, "todo item 2"); |
| 43 | + } |
| 44 | + |
| 45 | + await using (var scope = container.BeginLifetimeScope()) |
| 46 | + { |
| 47 | + var handler = scope.Resolve<CreateTodoHandler>(); |
| 48 | + await handler.HandleAsync(scheduleId, "todo item 3"); |
| 49 | + } |
| 50 | + |
| 51 | + await using (var scope = container.BeginLifetimeScope()) |
| 52 | + { |
| 53 | + var handler = scope.Resolve<UpdateTodoNameHandler>(); |
| 54 | + await handler.HandleAsync(scheduleId, todoId, "todo item 1 updated"); |
| 55 | + } |
| 56 | + |
| 57 | + await using (var scope = container.BeginLifetimeScope()) |
| 58 | + { |
| 59 | + var handler = scope.Resolve<CompleteTodoHandler>(); |
| 60 | + await handler.HandleAsync(scheduleId, todoId); |
| 61 | + } |
| 62 | + |
| 63 | + await using (var scope = container.BeginLifetimeScope()) |
| 64 | + { |
| 65 | + var session = scope.Resolve<ISession<Schedule>>(); |
| 66 | + var result = await session.GetByIdAsync(scheduleId); |
| 67 | + Console.WriteLine("--------"); |
| 68 | + Console.WriteLine("Final result after applying all events..."); |
| 69 | + PrintToConsole(result, ConsoleColor.Green); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static void PrintToConsole(object @object, ConsoleColor color) |
| 74 | + { |
| 75 | + Console.ForegroundColor = color; |
| 76 | + Console.WriteLine(JsonConvert.SerializeObject(@object, Formatting.Indented)); |
| 77 | + Console.ResetColor(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments