root / trunk / Pithos.Client.WPF / AppBootstrapper.cs @ 2c27cdd0
History | View | Annotate | Download (2.3 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.Core; |
7 |
using Pithos.Network; |
8 |
using log4net.Appender; |
9 |
using log4net.Config; |
10 |
using log4net.Filter; |
11 |
using log4net.Layout; |
12 |
|
13 |
namespace Pithos.Client.WPF |
14 |
{ |
15 |
using System; |
16 |
using System.Collections.Generic; |
17 |
using System.ComponentModel.Composition; |
18 |
using System.ComponentModel.Composition.Hosting; |
19 |
using System.ComponentModel.Composition.Primitives; |
20 |
using System.Linq; |
21 |
using Caliburn.Micro; |
22 |
|
23 |
public class AppBootstrapper : Bootstrapper<IShell> |
24 |
{ |
25 |
CompositionContainer container; |
26 |
|
27 |
public AppBootstrapper() |
28 |
{ |
29 |
LogManager.GetLog = type => new log4netLogger(type); |
30 |
} |
31 |
|
32 |
/// <summary> |
33 |
/// By default, we are configured to use MEF |
34 |
/// </summary> |
35 |
protected override void Configure() { |
36 |
var catalog = new AggregateCatalog( |
37 |
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>() |
38 |
); |
39 |
|
40 |
Type[] types = { typeof(PithosMonitor), typeof(CloudFilesClient) }; |
41 |
foreach (var type in types) |
42 |
{ |
43 |
catalog.Catalogs.Add(new AssemblyCatalog(type.Assembly)); |
44 |
} |
45 |
|
46 |
container = new CompositionContainer(catalog); |
47 |
|
48 |
var batch = new CompositionBatch(); |
49 |
|
50 |
batch.AddExportedValue<IWindowManager>(new WindowManager()); |
51 |
batch.AddExportedValue<IEventAggregator>(new EventAggregator()); |
52 |
batch.AddExportedValue(container); |
53 |
batch.AddExportedValue(catalog); |
54 |
|
55 |
|
56 |
container.Compose(batch); |
57 |
|
58 |
ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click"); |
59 |
} |
60 |
|
61 |
protected override object GetInstance(Type serviceType, string key) |
62 |
{ |
63 |
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key; |
64 |
var exports = container.GetExportedValues<object>(contract); |
65 |
|
66 |
if (exports.Count() > 0) |
67 |
return exports.First(); |
68 |
|
69 |
throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract)); |
70 |
} |
71 |
|
72 |
protected override IEnumerable<object> GetAllInstances(Type serviceType) |
73 |
{ |
74 |
return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType)); |
75 |
} |
76 |
|
77 |
protected override void BuildUp(object instance) |
78 |
{ |
79 |
container.SatisfyImportsOnce(instance); |
80 |
} |
81 |
} |
82 |
} |