forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializer.cs
More file actions
96 lines (89 loc) · 3.21 KB
/
Initializer.cs
File metadata and controls
96 lines (89 loc) · 3.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using RegisterOptions = React.TinyIoC.TinyIoCContainer.RegisterOptions;
namespace React
{
/// <summary>
/// Handles initialisation of ReactJS.NET. This is only called once, at application start.
/// </summary>
public static class Initializer
{
/// <summary>
/// Assemblies that are ignored when finding <see cref="IAssemblyRegistration"/>
/// implementations to register dependencies.
/// </summary>
private readonly static ISet<string> _obsoleteAssemblies = new HashSet<string>
{
"React.JavaScriptEngine.VroomJs",
"React.JavaScriptEngine.ClearScriptV8",
};
/// <summary>
/// Intialise ReactJS.NET
/// </summary>
/// <param name="requestLifetimeRegistration">
/// A function used to register IoC components with a per-request lifetime
/// </param>
public static void Initialize(Func<RegisterOptions, RegisterOptions> requestLifetimeRegistration)
{
InitializeIoC(requestLifetimeRegistration);
}
/// <summary>
/// Initialises the IoC container by finding all <see cref="IAssemblyRegistration"/>
/// implementations and calling their <see cref="IAssemblyRegistration.Register"/> methods.
/// </summary>
/// <param name="requestLifetimeRegistration">
/// A function used to register IoC components with a per-request lifetime
/// </param>
private static void InitializeIoC(Func<RegisterOptions, RegisterOptions> requestLifetimeRegistration)
{
TinyIoCExtensions.AsRequestLifetime = requestLifetimeRegistration;
var types = AppDomain.CurrentDomain.GetAssemblies()
// Only bother checking React assemblies
.Where(IsReactAssembly)
.SelectMany(assembly => assembly.GetTypes())
.Where(IsComponentRegistration);
foreach (var type in types)
{
var reg = (IAssemblyRegistration)Activator.CreateInstance(type);
reg.Register(React.AssemblyRegistration.Container);
}
}
/// <summary>
/// Determines if the specified assembly is part of ReactJS.NET
/// </summary>
/// <param name="assembly">The assembly</param>
/// <returns>
/// <c>true</c> if this is a ReactJS.NET assembly; otherwise, <c>false</c>.
/// </returns>
private static bool IsReactAssembly(Assembly assembly)
{
var nameWithoutVersion = assembly.FullName.Split(',')[0];
return
(nameWithoutVersion == "React" ||
nameWithoutVersion.StartsWith("React.") ||
nameWithoutVersion.EndsWith(".React")) &&
!_obsoleteAssemblies.Contains(nameWithoutVersion);
}
/// <summary>
/// Determines whether the specified type is an assembly registration class
/// </summary>
/// <param name="type">The type to check</param>
/// <returns>
/// <c>true</c> if the type is a component registration class; otherwise, <c>false</c>.
/// </returns>
private static bool IsComponentRegistration(Type type)
{
return type.GetInterfaces().Contains(typeof(IAssemblyRegistration));
}
}
}