Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / AppBootstrapper.cs @ aba9e6d9

History | View | Annotate | Download (2.7 kB)

1
using System.Windows;
2
using System.Windows.Controls;
3
using System.Windows.Navigation;
4
using Caliburn.Micro;
5
using Caliburn.Micro.Logging;
6
using Pithos.Client.WPF.Properties;
7
using Pithos.Core;
8
using Pithos.Network;
9
using log4net.Appender;
10
using log4net.Config;
11
using log4net.Filter;
12
using log4net.Layout;
13

    
14
namespace Pithos.Client.WPF
15
{
16
	using System;
17
	using System.Collections.Generic;
18
	using System.ComponentModel.Composition;
19
	using System.ComponentModel.Composition.Hosting;
20
	using System.ComponentModel.Composition.Primitives;
21
	using System.Linq;
22
	using Caliburn.Micro;
23

    
24
	public class AppBootstrapper : Bootstrapper<IShell>
25
	{
26
		CompositionContainer container;
27

    
28
	    public AppBootstrapper()
29
	    {
30
            LogManager.GetLog = type => new log4netLogger(type);
31
	        UpgradeSettings();
32
	    }
33

    
34
	    private void UpgradeSettings()
35
	    {
36
            if (Settings.Default.MustUpgrade)
37
            {
38
                Settings.Default.Upgrade();
39
                Settings.Default.MustUpgrade = false;
40
                Settings.Default.Save();
41
            }
42
	    }
43

    
44
	    /// <summary>
45
		/// By default, we are configured to use MEF
46
		/// </summary>
47
		protected override void Configure() {
48
		    var catalog = new AggregateCatalog(
49
		        AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
50
		        );
51

    
52
            Type[] types = { typeof(PithosMonitor), typeof(CloudFilesClient) };
53
            foreach (var type in types)
54
            {
55
                catalog.Catalogs.Add(new AssemblyCatalog(type.Assembly));
56
            }
57

    
58
			container = new CompositionContainer(catalog);
59

    
60
			var batch = new CompositionBatch();
61

    
62
			batch.AddExportedValue<IWindowManager>(new WindowManager());
63
			batch.AddExportedValue<IEventAggregator>(new EventAggregator());
64
			batch.AddExportedValue(container);
65
		    batch.AddExportedValue(catalog);
66

    
67

    
68
			container.Compose(batch);
69

    
70
            ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
71
		}
72

    
73
		protected override object GetInstance(Type serviceType, string key)
74
		{
75
			string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
76
			var exports = container.GetExportedValues<object>(contract);
77

    
78
			if (exports.Count() > 0)
79
				return exports.First();
80

    
81
			throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
82
		}
83

    
84
		protected override IEnumerable<object> GetAllInstances(Type serviceType)
85
		{
86
			return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
87
		}
88

    
89
		protected override void BuildUp(object instance)
90
		{
91
			container.SatisfyImportsOnce(instance);
92
		}
93
	}
94
}