Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / AppBootstrapper.cs @ 70f12b36

History | View | Annotate | Download (2.8 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 Microsoft.Windows.Controls;
7
using Pithos.Client.WPF.Properties;
8
using Pithos.Core;
9
using Pithos.Network;
10
using log4net.Appender;
11
using log4net.Config;
12
using log4net.Filter;
13
using log4net.Layout;
14

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

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

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

    
34
	    }
35

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

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

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

    
60
			container = new CompositionContainer(catalog,true);
61

    
62
			var batch = new CompositionBatch();
63

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

    
69

    
70
			container.Compose(batch);
71

    
72
            ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
73
	        ConventionManager.AddElementConvention<IntegerUpDown>(IntegerUpDown.ValueProperty, "Value", "ValueChanged");
74
		}
75

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

    
81
			if (exports.Any())
82
				return exports.First();
83

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

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

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