Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.4 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.Caliburn.Micro.Logging;
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
	    }
32

    
33
		/// <summary>
34
		/// By default, we are configured to use MEF
35
		/// </summary>
36
		protected override void Configure() {
37
		    var catalog = new AggregateCatalog(
38
		        AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
39
		        );
40

    
41
            Type[] types = { typeof(PithosMonitor), typeof(CloudFilesClient) };
42
            foreach (var type in types)
43
            {
44
                catalog.Catalogs.Add(new AssemblyCatalog(type.Assembly));
45
            }
46

    
47
			container = new CompositionContainer(catalog);
48

    
49
			var batch = new CompositionBatch();
50

    
51
			batch.AddExportedValue<IWindowManager>(new WindowManager());
52
			batch.AddExportedValue<IEventAggregator>(new EventAggregator());
53
			batch.AddExportedValue(container);
54
		    batch.AddExportedValue(catalog);
55

    
56

    
57
			container.Compose(batch);
58

    
59
            ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
60
		}
61

    
62
		protected override object GetInstance(Type serviceType, string key)
63
		{
64
			string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
65
			var exports = container.GetExportedValues<object>(contract);
66

    
67
			if (exports.Count() > 0)
68
				return exports.First();
69

    
70
			throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
71
		}
72

    
73
		protected override IEnumerable<object> GetAllInstances(Type serviceType)
74
		{
75
			return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
76
		}
77

    
78
		protected override void BuildUp(object instance)
79
		{
80
			container.SatisfyImportsOnce(instance);
81
		}
82
	}
83
}