11using System . Collections . Generic ;
22using System . Reflection ;
33using System . Web . Mvc ;
4- using StructureMap ;
4+ using System ;
55
66namespace JsRouting . Core
77{
@@ -15,27 +15,108 @@ public static class AssemblyLoader
1515 /// </summary>
1616 /// <param name="assemblies">Assemblies to scan</param>
1717 /// <returns>Container containing various dependencies to form the JavaScript routing definitions</returns>
18- public static IContainer Load ( IEnumerable < string > assemblies )
18+ public static RouteConstructionContainer Load ( IEnumerable < string > assemblies )
1919 {
20- return new Container ( config =>
20+ var container = new RouteConstructionContainer ( ) ;
21+
22+ foreach ( var assembly in assemblies )
23+ {
24+ container . AddAssembly ( Assembly . LoadFrom ( assembly ) ) ;
25+ }
26+
27+ return container ;
28+ }
29+ }
30+
31+ public sealed class RouteConstructionContainer
32+ {
33+ private readonly IList < IRouteSource > sources = new List < IRouteSource > ( ) ;
34+ private readonly IList < IRouteInterceptor > interceptors = new List < IRouteInterceptor > ( ) ;
35+ private readonly IList < Type > controllerTypes = new List < Type > ( ) ;
36+ private readonly IList < IControllerActionInterceptor > controllerActionInterceptors = new List < IControllerActionInterceptor > ( ) ;
37+ private readonly IList < IJavaScriptAddition > additions = new List < IJavaScriptAddition > ( ) ;
38+
39+ public IEnumerable < IJavaScriptAddition > Additions
40+ {
41+ get
42+ {
43+ return additions ;
44+ }
45+ }
46+
47+ public IEnumerable < IControllerActionInterceptor > ControllerActionInterceptors
48+ {
49+ get
50+ {
51+ return controllerActionInterceptors ;
52+ }
53+ }
54+
55+ public IEnumerable < Type > ControllerTypes
56+ {
57+ get
58+ {
59+ return controllerTypes ;
60+ }
61+ }
62+
63+ public IEnumerable < IRouteInterceptor > Interceptors
64+ {
65+ get
66+ {
67+ return interceptors ;
68+ }
69+ }
70+
71+ public IEnumerable < IRouteSource > Sources
72+ {
73+ get
2174 {
22- config . Scan ( a =>
75+ return sources ;
76+ }
77+ }
78+
79+ internal void AddAssembly ( Assembly assembly )
80+ {
81+ foreach ( var type in assembly . GetTypes ( ) )
82+ {
83+ object value = null ;
84+
85+ bool publicCtr = type . GetConstructor ( new Type [ 0 ] ) != null ;
86+
87+ if ( type . IsA < IRouteSource > ( ) && publicCtr )
88+ {
89+ sources . Add ( ( IRouteSource ) ( value = Activator . CreateInstance ( type ) ) ) ;
90+ }
91+
92+ if ( type . IsA < IRouteInterceptor > ( ) && publicCtr )
2393 {
24- // add the main JsRouting assembly to the scanner
25- a . Assembly ( typeof ( Output ) . Assembly ) ;
26-
27- // add the assemblies to scan
28- foreach ( string assembly in assemblies )
29- a . Assembly ( Assembly . LoadFrom ( assembly ) ) ;
30-
31- // types to scan..
32- a . AddAllTypesOf < IRouteSource > ( ) ;
33- a . AddAllTypesOf < IRouteInterceptor > ( ) ;
34- a . AddAllTypesOf < ControllerBase > ( ) ;
35- a . AddAllTypesOf < IControllerActionInterceptor > ( ) ;
36- a . AddAllTypesOf < IJavaScriptAddition > ( ) ;
37- } ) ;
38- } ) ;
94+ interceptors . Add ( ( IRouteInterceptor ) ( value ?? ( value = Activator . CreateInstance ( type ) ) ) ) ;
95+ }
96+
97+ if ( type . IsA < ControllerBase > ( ) )
98+ {
99+ controllerTypes . Add ( type ) ;
100+ }
101+
102+ if ( type . IsA < IControllerActionInterceptor > ( ) && publicCtr )
103+ {
104+ controllerActionInterceptors . Add ( ( IControllerActionInterceptor ) ( value ?? ( value = Activator . CreateInstance ( type ) ) ) ) ;
105+ }
106+
107+ if ( type . IsA < IJavaScriptAddition > ( ) && publicCtr )
108+ {
109+ additions . Add ( ( IJavaScriptAddition ) ( value ?? ( value = Activator . CreateInstance ( type ) ) ) ) ;
110+ }
111+ }
112+ }
113+ }
114+
115+ internal static class TypeExt
116+ {
117+ public static bool IsA < T > ( this Type sub )
118+ {
119+ return typeof ( T ) . IsAssignableFrom ( sub ) ;
39120 }
40121 }
41122}
0 commit comments