forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cs
More file actions
70 lines (55 loc) · 2.15 KB
/
App.cs
File metadata and controls
70 lines (55 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using ReactiveUI;
using System.Collections.Generic;
using System.Linq;
using ReactiveUI.Mobile;
namespace AndroidPlayground
{
public class App
{
static App _Current;
public static App Current {
get { return (_Current = _Current ?? new App()); }
}
public FuncServiceLocator Locator { get; protected set; }
protected App()
{
var locator = new FuncServiceLocator();
// TODO: Fix Me
//RxApp.ConfigureServiceLocator(
// (t, s) => locator.GetAllServices(t, s).FirstOrDefault(),
// (t, s) => locator.GetAllServices(t, s).ToArray(),
// (c, t, s) => locator.Register(() => Activator.CreateInstance(c), t, s));
locator.Register(() => typeof(MainView), typeof(IViewFor<MainViewModel>));
locator.Register(() => typeof(SecondaryView), typeof(IViewFor<SecondaryViewModel>));
// TODO: Fix Me
//RxApp.Register(typeof(AppBootstrapper), typeof(IApplicationRootState));
Locator = locator;
}
}
public class FuncServiceLocator
{
readonly Dictionary<Tuple<Type, string>, List<Func<object>>> _registry = new Dictionary<Tuple<Type, string>, List<Func<object>>>();
public void Register(Func<object> factory, Type type, string contract = null)
{
var pair = Tuple.Create(type, contract ?? "");
if (!_registry.ContainsKey(pair)) _registry[pair] = new List<Func<object>>();
_registry[pair].Add(factory);
}
public IEnumerable<object> GetAllServices(Type type, string contract = null)
{
var pair = Tuple.Create(type, contract ?? "");
if (!_registry.ContainsKey(pair)) {
return Enumerable.Empty<object>();
}
return _registry[pair].Select(x => x());
}
public void ClearRegistration(Type type, string contract = null)
{
var pair = Tuple.Create(type, contract ?? "");
if (_registry.ContainsKey(pair)) {
_registry.Remove(pair);
}
}
}
}