Exemple #1
0
        protected override async void OnStartup(StartupEventArgs e)
        {
            var appId = Marshal.GetTypeLibGuidForAssembly(Assembly.GetExecutingAssembly()).ToString();

            _appMutex = new Mutex(initiallyOwned: true, name: appId, createdNew: out _isOwner);

            if (!_isOwner)
            {
                MessageBox.Show("The application is already running.");
                Current.Shutdown();
                return;
            }

            XmlConfigurator.Configure();

            base.OnStartup(e);

            _dbFile = ConfigurationManager.ConnectionStrings["HomeBankConnection"].ConnectionString;

            _sessionFactoryProvider = new SessionFactoryProvider(_dbFile);
            _sessionProvider        = new SessionProvider(_sessionFactoryProvider);

            _unitOfWorkFactory     = new UnitOfWorkFactory(_sessionProvider);
            _categoryRepository    = new SqliteCategoryRepository(_sessionProvider);
            _transactionRepository = new SqliteTransactionRepository(_sessionProvider);

            _statisticService   = new StatisticService(_transactionRepository);
            _communalCalculator = new CommunalCalculator(_communalSettings);

            var categoryItemViewModel = new CategoryItemViewModel(_eventBus);
            var categoryViewModel     = await CategoryViewModel.CreateAsync(
                _eventBus,
                _yesNoDialogServiceFactory,
                _errorDialogServiceFactory,
                _unitOfWorkFactory,
                _categoryRepository);

            var transactionItemViewModel = new TransactionItemViewModel(_eventBus, _transactionRepository, categoryViewModel.Categories);
            var transactionViewModel     = await TransactionViewModel.CreateAsync(
                _eventBus,
                _yesNoDialogServiceFactory,
                _unitOfWorkFactory,
                _categoryRepository,
                _transactionRepository);

            var statisticViewModel = await StatisticViewModel.CreateAsync(_eventBus, _statisticService);

            statisticViewModel.Type      = CategoryTypeFilter.All;
            statisticViewModel.StartDate = DateTime.Now;
            statisticViewModel.EndDate   = DateTime.Now;

            var communalViewModel = new CommunalViewModel(_eventBus, _communalCalculator);
            var settingsViewModel = new SettingsViewModel(_eventBus, _communalSettings);

            var childrenViewModels = new ViewModel[]
            {
                transactionViewModel,
                categoryViewModel,
                statisticViewModel,
                communalViewModel,
                settingsViewModel,
                categoryItemViewModel,
                transactionItemViewModel
            };

            if (_mainView == null)
            {
                _mainViewModel = new MainViewModel(
                    _eventBus,
                    childrenViewModels,
                    _categoryRepository,
                    _transactionRepository);

                transactionViewModel.Type      = CategoryTypeFilter.All;
                transactionViewModel.StartDate = DateTime.Now;
                transactionViewModel.EndDate   = DateTime.Now;

                _mainView = new MainView(_mainViewModel);

                _mainView.Show();
            }

            HandleExceptions();
        }