示例#1
0
        /// <summary>
        /// 子类在合适的时间调用此方法来启动整个 Rafy 应用程序。
        ///
        /// 注意,为了支持重新启动,这个类中的所有方法都应该可以运行多次。
        ///
        /// 但是,第二次及之后的重启,不应该使用同一个 AppImplementationBase 的实例,否则可能会造成插件重复监听同一事件。
        /// </summary>
        protected void StartupApplication()
        {
            this.Phase = AppPhase.Starting;
            this.PrepareToStartup();

            this.InitEnvironment();
            RafyEnvironment.CreateStartupPlugins();

            //注册所有扩展属性
            //由于插件的 Intialize 方法中的应用层代码,有可能主动使用实体类而造成实体类的静态构造函数被执行,
            //所以必须在 Intialize 方法执行前,即任何实体被使用前,初始化所有的扩展属性。
            RafyEnvironment.InitExtensionProperties();

            //调用所有插件的 Initialize 方法。
            RafyEnvironment.InitializeStartupPlugins();
            this.OnStartupPluginsIntialized();

            //初始化启动期元数据
            this.CreateMeta();
            this.OnMetaCreating();

            this.OnMetaCreated();
            this.Phase = AppPhase.MetaCreated;

            //开始运行时行为。此行代码后的所有代码都可以看作运行时行为。
            this.OnRuntimeStarting();

            //启动主过程
            this.StartMainProcess();

            //整个初始化完毕。
            this.OnStartupCompleted();
            this.Phase = AppPhase.Running;
        }
示例#2
0
        /// <summary>
        /// 触发 Exit 事件。
        /// </summary>
        protected virtual void OnExit()
        {
            var handler = this.Exit;

            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }

            this.Phase = AppPhase.Exited;
        }
        public void Run()
        {
            InitialiseApplication();


            Console.WriteLine("Welcome to Secret Santa. Would you like to draw?");

            while (phase != AppPhase.Complete && phase != AppPhase.ConfigurationError)
            {
                switch (phase)
                {
                case AppPhase.DrawNotStarted:
                    Console.WriteLine("Press 'd' to draw, 'b' to test the assignments or 't' to send a test email");
                    var input = GetKeyInput(new char[] { 'd', 't', 'b' });
                    switch (input)
                    {
                    case 'd':
                    case 'b':
                        var debug       = input == 'b';
                        var success     = false;
                        var attempts    = 0;
                        var maxAttempts = 10;
                        while (!success && attempts < maxAttempts)
                        {
                            success = DoDraw(debug);
                            attempts++;
                        }
                        if (success)
                        {
                            phase = debug ? AppPhase.DrawNotStarted : AppPhase.EmailSending;
                        }
                        else
                        {
                            Console.WriteLine("Failed to pick Secret Santa recipients");
                        }

                        break;

                    case 't':
                        Console.WriteLine("Sending test email");
                        var sent = emailSender.SendEmail(appConfig.Smtp.TestEmail, "This is a test email from\n\r Secret Santa");
                        if (sent)
                        {
                            Console.WriteLine("Test Email sent");
                        }
                        else
                        {
                            phase = AppPhase.ConfigurationError;
                        }
                        break;
                    }

                    break;

                case AppPhase.EmailSending:
                    foreach (var result in drawResults)
                    {
                        var sent = emailSender.SendEmail(result.Person.Email,
                                                         $"Hi, {result.Person.Name},\n\rYou have been assigned {result.AssignedTo} in round 2 of the Secret Santa draw.");

                        if (!sent)
                        {
                            phase = AppPhase.ConfigurationError;
                            break;
                        }
                    }
                    if (phase != AppPhase.ConfigurationError)
                    {
                        phase = AppPhase.Complete;
                    }
                    break;
                }
            }

            Console.WriteLine("Complete: Press any key to quit");
            Console.ReadKey();
        }
 private void InitialiseApplication()
 {
     phase = AppPhase.DrawNotStarted;
 }