Пример #1
0
        static void Main(string[] args)
        {
            // Initialize IoC container
            var services = new ServiceCollection();

            if (args.Length > 0 && !string.IsNullOrEmpty(args[0]))
            {
                _dbType = DatabaseType.TryParse(args[0]);
            }

            if (_dbType.CompareTo(DatabaseType.InMemory) == 0)
            {
                services.AddDbContext <EventContext>(options =>
                                                     options.UseInMemoryDatabase("events"));
                services.AddDbContext <TodoContext>(options =>
                                                    options.UseInMemoryDatabase("todos"));
            }
            else if (_dbType.CompareTo(DatabaseType.Sqlite) == 0)
            {
                services.AddDbContext <EventContext>(options =>
                                                     options.UseSqlite("Data Source=events.db"));
                services.AddDbContext <TodoContext>(options =>
                                                    options.UseSqlite("Data Source=todos.db"));
            }
            else
            {
                throw new InvalidCastException("Not supported database type");
            }

            services.AddSingleton <ITodoService, TodoService>();
            services.AddLogging();
            _serviceProvider = services.BuildServiceProvider();


            var eventContext = _serviceProvider.GetService <EventContext>();

            eventContext.Database.EnsureCreated();
            _store = new EventStore(eventContext, TodoEventHandler);

            var todoContext = _serviceProvider.GetService <TodoContext>();

            todoContext.Database.EnsureCreated();
            var loggerFactory = _serviceProvider.GetService <ILoggerFactory>()
                                .AddConsole(LogLevel.Trace);

            _logger = loggerFactory.CreateLogger <Program>();

            _todoService = new TodoService(todoContext, loggerFactory);

            _logger.LogInformation("{Time} | Todo Application Started", DateTime.Now);
            if (_dbType == DatabaseType.InMemory)
            {
                _logger.LogWarning("Storage type in-memory, data will be lost at the end of the application");
            }

            /* Command */
            CreateTodoItem(1, "Pickup Milk");
            CreateTodoItem(2, "Pickup Bread");
            PrintTodos();
            MarkTodoItemCompleted(1, true);
            PrintTodos();
            DeleteTodoItem(1);
            PrintTodos();

            // PrintEvents();
            _logger.LogInformation("{Time} | End of Application", DateTime.Now);
        }