main
 1using System;
 2using System.ComponentModel;
 3using System.ComponentModel.Design;
 4using System.Diagnostics;
 5using System.Globalization;
 6using System.Security.Principal;
 7using System.Threading;
 8using System.Windows.Forms;
 9using Gorilla.Commons.Infrastructure.Container;
10using Gorilla.Commons.Infrastructure.Logging;
11using gorilla.commons.utility;
12using momoney.boot;
13using MoMoney.boot.container;
14using momoney.presentation.model.eventing;
15using MoMoney.Presentation.Presenters;
16using MoMoney.Service.Infrastructure.Eventing;
17using momoney.service.infrastructure.threading;
18
19namespace MoMoney.boot
20{
21    public class WindowsFormsApplication<Shell> : Command where Shell : Form
22    {
23        protected WindowsFormsApplication()
24        {
25            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
26            Application.EnableVisualStyles();
27            Application.SetCompatibleTextRenderingDefault(false);
28        }
29
30        public void run()
31        {
32            using (new LogTime())
33            {
34                Func<ISplashScreenPresenter> presenter = () => new SplashScreenPresenter();
35                presenter = presenter.memorize();
36
37                var startup_screen = new display_the_splash_screen(presenter).on_a_background_thread();
38
39                AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
40                hookup
41                    .the<global_error_handling>()
42                    .then(startup_screen)
43                    .then<wire_up_the_container>()
44                    .then(new start_the_application(startup_screen))
45                    .run();
46            }
47            start();
48        }
49
50        void start()
51        {
52            try
53            {
54                Application.Run(Resolve.the<Shell>());
55            }
56            catch (Exception e)
57            {
58                this.log().error(e);
59                Resolve.the<IEventAggregator>().publish(new UnhandledErrorOccurred(e));
60            }
61        }
62    }
63
64    public class LogTime : IDisposable
65    {
66        Stopwatch stopwatch;
67
68        public LogTime()
69        {
70            stopwatch = new Stopwatch();
71            stopwatch.Start();
72        }
73
74        public void Dispose()
75        {
76            stopwatch.Stop();
77            this.log().debug("application startup took: {0}", stopwatch.Elapsed);
78        }
79    }
80
81    public class ApplicationContainer : Container
82    {
83        readonly IServiceContainer container;
84
85        public ApplicationContainer() : this(new ServiceContainer()) {}
86
87        public ApplicationContainer(IServiceContainer container)
88        {
89            this.container = container;
90        }
91
92        protected override object GetService(Type service)
93        {
94            return container.GetService(service) ?? base.GetService(service);
95        }
96    }
97}