Statistics
| Branch: | Revision:

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

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

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

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

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

    
59
			container = new CompositionContainer(catalog);
60

    
61
			var batch = new CompositionBatch();
62

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

    
68

    
69
			container.Compose(batch);
70

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

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

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

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

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

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