コード例 #1
0
        public ConfigurationViewModel(ILifetimeScope lifetimeScope, Dispatcher dispatcher, IActionInvoker actionInvoker)
            : base(lifetimeScope, dispatcher, actionInvoker)
        {
            ErrorText = RegisterProperty <string>(nameof(ErrorText));

            ConfigText = RegisterProperty <string>(nameof(ConfigText))
                         .WithValidator(s =>
            {
                try
                {
                    ConfigurationFactory.ParseString(s);
                    return(null);
                }
                catch (Exception e)
                {
                    return(e.Message);
                }
            });

            GetState <ServicesConfigState>().Query(EmptyQuery.Instance)
            .ContinueWith(c => ConfigText.Set(c.Result?.BaseConfiguration));

            IsSetupVisible = RegisterProperty <bool>(nameof(IsSetupVisible));

            Receive <StartConfigurationSetup>(_ => IsSetupVisible += true);

            NewCommad
            .WithExecute(() =>
            {
                IsSetupVisible += false;
                Context.System.EventStream.Publish(StartInitialHostSetup.Get);
            })
            .ThenRegister("SetupNext");

            ConnectionString = RegisterProperty <string>(nameof(ConnectionString))
                               .WithValidator(s =>
            {
                try
                {
                    MongoUrl.Create(s);
                    return(null);
                }
                catch (Exception e)
                {
                    return(e.Message);
                }
            });

            void ValidateMongoUrl()
            {
                var result = ServicesConfigState.MongoUrlValidator.Validate(ConnectionString.Value);

                if (result.IsValid)
                {
                    ErrorText += string.Empty;
                }
                else
                {
                    ErrorText += result.Errors.FirstOrDefault()?.ErrorMessage ?? string.Empty;
                }
            }

            NewCommad
            .WithCanExecute(b => b.FromProperty(ConnectionString.IsValid))
            .WithExecute(ValidateMongoUrl)
            .ThenRegister("ValidateConnection");

            NewCommad
            .WithCanExecute(b => b.FromProperty(ConnectionString.IsValid))
            .ToStateAction(() => new ApplyMongoUrlAction(ConnectionString.Value))
            .ThenRegister("ApplyConnection");
        }