Example #1
0
        public App()
        {
            if (_navigationPage == null)
            {
                var loginView = new LoginView();
                _navigationPage = new NavigationServicePage(loginView);

                var messenger         = new Messenger();
                var dispatcher        = new DispatcherHelper();
                var serviceCollection = new ServiceCollection();
                var errorService      = new ErrorService(_navigationPage);

                serviceCollection.AddSingleton <IDispatcherHelper, DispatcherHelper>();

                // Register services
                serviceCollection.AddSingleton <IErrorService>(errorService);
                serviceCollection.AddSingleton <IProfilesService, ProfilesService>();
                serviceCollection.AddSingleton <IMessenger>(messenger);

                var clipboardService = new ClipboardService();

                var profileHandler = new LocalProfileHandler(messenger, dispatcher, clipboardService);
                profileHandler
                .SetNext(new RestProfileHandler(messenger, dispatcher, errorService, clipboardService))
                .SetNext(new RmqProfileHandler(messenger, dispatcher, errorService, clipboardService));

                serviceCollection.AddSingleton <IProfileHandler>(profileHandler);
                serviceCollection.AddTransient((c) => c.GetRequiredService <IProfileHandler>().GetSnippetService());
                serviceCollection.AddTransient((c) => c.GetRequiredService <IProfileHandler>().GetUserService());

                // Register viewmodels
                serviceCollection.AddSingleton <LoginViewModel>();
                serviceCollection.AddTransient <ProfileViewModel>();
                serviceCollection.AddSingleton <Func <ProfileViewModel> >(c => () => c.GetRequiredService <ProfileViewModel>());
                serviceCollection.AddTransient <LoginProfileViewModel>();
                serviceCollection.AddSingleton <Func <LoginProfileViewModel> >(c => () => c.GetRequiredService <LoginProfileViewModel>());
                serviceCollection.AddScoped <MenuViewModel>();

                serviceCollection.AddScoped <AllSnippetsViewModel>();
                serviceCollection.AddScoped <AllUsersViewModel>();

                serviceCollection.AddTransient <SnippetViewModel>();
                serviceCollection.AddSingleton <Func <SnippetViewModel> >(c => () => c.GetRequiredService <SnippetViewModel>());
                serviceCollection.AddTransient <UserViewModel>();
                serviceCollection.AddSingleton <Func <UserViewModel> >(c => () => c.GetRequiredService <UserViewModel>());
                serviceCollection.AddTransient <VariableViewModel>();
                serviceCollection.AddSingleton <Func <VariableViewModel> >(c => () => c.GetRequiredService <VariableViewModel>());

                serviceCollection.AddSingleton <INavigationService>(_navigationPage);

                var serviceProvider = serviceCollection.BuildServiceProvider();

                loginView.BindingContext = serviceProvider.GetRequiredService <LoginViewModel>();
            }

            MainPage = _navigationPage;
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            var messenger         = new Messenger();
            var dispatcher        = new DispatcherHelper();
            var serviceCollection = new ServiceCollection();

            Main = new MainViewModel();
            var errorService = new ErrorService(Main);

            serviceCollection.AddSingleton <IDiskSearcher, DiskSearcher>();
            serviceCollection.AddSingleton <IDispatcherHelper, DispatcherHelper>();

            // Register services
            serviceCollection.AddSingleton <IErrorService>(errorService);
            serviceCollection.AddSingleton <IProfilesService, ProfilesService>();
            serviceCollection.AddSingleton <IMessenger>(messenger);

            var clipboardService = new ClipboardService();

            var profileHandler = new LocalProfileHandler(messenger, dispatcher, clipboardService);

            profileHandler
            .SetNext(new RestProfileHandler(messenger, dispatcher, errorService, clipboardService))
            .SetNext(new RmqProfileHandler(messenger, dispatcher, errorService, clipboardService));

            serviceCollection.AddSingleton <IProfileHandler>(profileHandler);
            serviceCollection.AddTransient((c) => c.GetRequiredService <IProfileHandler>().GetSnippetService());
            serviceCollection.AddTransient((c) => c.GetRequiredService <IProfileHandler>().GetUserService());

            // Register viewmodels
            serviceCollection.AddSingleton <LoginViewModel>();
            serviceCollection.AddTransient <ProfileViewModel>();
            serviceCollection.AddSingleton <Func <ProfileViewModel> >(c => () => c.GetRequiredService <ProfileViewModel>());
            serviceCollection.AddTransient <LoginProfileViewModel>();
            serviceCollection.AddSingleton <Func <LoginProfileViewModel> >(c => () => c.GetRequiredService <LoginProfileViewModel>());
            serviceCollection.AddScoped <MenuViewModel>();

            serviceCollection.AddScoped <AllSnippetsViewModel>();
            serviceCollection.AddScoped <AllUsersViewModel>();

            serviceCollection.AddTransient <SnippetViewModel>();
            serviceCollection.AddSingleton <Func <SnippetViewModel> >(c => () => c.GetRequiredService <SnippetViewModel>());
            serviceCollection.AddTransient <UserViewModel>();
            serviceCollection.AddSingleton <Func <UserViewModel> >(c => () => c.GetRequiredService <UserViewModel>());
            serviceCollection.AddTransient <VariableViewModel>();
            serviceCollection.AddSingleton <Func <VariableViewModel> >(c => () => c.GetRequiredService <VariableViewModel>());

            serviceCollection.AddSingleton <INavigationService>(Main);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            Main.NavigateForward(serviceProvider.GetRequiredService <LoginViewModel>());
        }