Skip to content

Commit ae3a523

Browse files
committed
removing dependency on structuremap
1 parent 52688d9 commit ae3a523

16 files changed

Lines changed: 170 additions & 704 deletions

‎Core/AssemblyLoader.cs‎

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Reflection;
33
using System.Web.Mvc;
4-
using StructureMap;
4+
using System;
55

66
namespace 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
}

‎Core/ControllerReader.cs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Web.Mvc;
5-
using StructureMap;
65
using System.Reflection;
76
using System.Web.Script.Serialization;
87

@@ -31,22 +30,31 @@ public class ControllerReader
3130
/// <summary>
3231
/// Initializes a new instance of the <see cref="ControllerReader"/> class.
3332
/// </summary>
34-
/// <param name="container">The dependency container.</param>
35-
/// <param name="interceptors">Controller action interceptors</param>
36-
public ControllerReader(IContainer container, IEnumerable<IControllerActionInterceptor> interceptors)
33+
/// <param name="container">Route container</param>
34+
public ControllerReader(RouteConstructionContainer container)
35+
:this (container.ControllerTypes, container.ControllerActionInterceptors)
3736
{
38-
if (container == null) throw new ArgumentNullException("container");
37+
}
38+
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="ControllerReader"/> class.
41+
/// </summary>
42+
/// <param name="controllerTypes">Controller types</param>
43+
/// <param name="interceptors">Controller interceptors</param>
44+
public ControllerReader(IEnumerable<Type> controllerTypes, IEnumerable<IControllerActionInterceptor> interceptors)
45+
{
46+
if (controllerTypes == null) throw new ArgumentNullException("controllerTypes");
3947
if (interceptors == null) throw new ArgumentNullException("interceptors");
4048

4149
var paramInterceptor = new ParameterInterceptor(this.FindReader);
4250
var descritionInterceptor = new SummaryInterceptor(this.FindReader);
4351

44-
this.Definitions = from inst in container.Model.InstancesOf<ControllerBase>()
45-
from action in inst.ConcreteType.GetMethods()
52+
this.Definitions = from inst in controllerTypes
53+
from action in inst.GetMethods()
4654
where action.IsPublic
4755
where actionResult.IsAssignableFrom(action.ReturnType)
4856
let def = new ControllerActionDefinition(action)
49-
where descritionInterceptor.Intercept(def) && paramInterceptor.Intercept(def) &&
57+
where descritionInterceptor.Intercept(def) && paramInterceptor.Intercept(def) &&
5058
interceptors.AsParallel().All(interceptor => interceptor.Intercept(def))
5159
select def;
5260
}

‎Core/Core.csproj‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="StructureMap">
36-
<HintPath>..\lib\StructureMap.dll</HintPath>
37-
</Reference>
3835
<Reference Include="System" />
3936
<Reference Include="System.Core" />
4037
<Reference Include="Microsoft.CSharp" />

‎Core/JavaScript/RouteManager.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*!
22
* ASP.NET JavaScript Router
33
*
4-
* Copyright 2010, Zack Owens
4+
* Copyright 2011, Zack Owens
55
* http://weblogs.asp.net/zowens/
66
* http://wwww.eagleenvision.net/
77
*/
88

99
// ===============================================================================================
1010
//
11-
// WARNING: This file was generated by JsRouting.Output.exe and should NOT be overridden.
11+
// WARNING: This file was generated by JsRouting and should NOT be overridden.
1212
// When this file is compromised, the file will be overridden and changes will be lost.
1313
//
1414
// ===============================================================================================
1515

16-
// The following code is the jQuery pluggin that handles most of the routing logic. Note that jQuery is not required,
17-
// but is used as the root namespace to prevent clutter of the global vars.
16+
// The following code is the jQuery plugin that handles most of the routing logic.
17+
// JQUERY IS REQUIRED!
1818

1919
(function ($, undefined) {
2020

‎Core/Output.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public Output(RouteReader routeReader, ControllerReader controllerReader, IEnume
4949
this.additions = additions ?? Enumerable.Empty<IJavaScriptAddition>();
5050
}
5151

52+
public Output(RouteConstructionContainer container)
53+
: this(new RouteReader(container.Sources, container.Interceptors),
54+
new ControllerReader(container), container.Additions)
55+
{
56+
57+
}
58+
5259
/// <summary>
5360
/// Gets the output for the manager JS embedded resource
5461
/// </summary>

‎NuGet/JsRouting.nuspec‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>JsRouting</id>
5-
<version>1.0</version>
5+
<version>0.1</version>
66
<title>Javascript Routing</title>
77
<authors>Zack Owens,Richard Grundy</authors>
88
<owners>Zack Owens</owners>
99
<licenseUrl>https://github.com/zowens/ASP.NET-MVC-JavaScript-Routing</licenseUrl>
1010
<projectUrl>https://github.com/zowens/ASP.NET-MVC-JavaScript-Routing</projectUrl>
1111
<requireLicenseAcceptance>true</requireLicenseAcceptance>
1212
<description>Allows ASP.NET MVC routes to be used within JavaScript in a jQuery-like DSL.</description>
13-
<summary>Copyright (c) 2011, Zack Owens.
14-
All rights reserved.
15-
16-
Redistribution and use in source and binary forms, with or without
17-
modification, are permitted provided that the following conditions are met</summary>
13+
<summary>Allows ASP.NET MVC routes to be used within JavaScript in a jQuery-like DSL.</summary>
1814
<tags>Javascript, Routing, ASP.NET MVC</tags>
1915
<dependencies>
20-
<dependency id="structuremap" version="2.6.3" />
16+
<dependency id="jQuery" version="1.7" />
2117
</dependencies>
2218
<frameworkAssemblies>
2319
<frameworkAssembly assemblyName="Microsoft.CSharp" targetFramework="" />

‎NuGet/JsRouting.tt‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<#@ assembly name="EnvDTE80" #>
88
<#@ assembly name="VSLangProj" #>
99
<#@ assembly name="$(ProjectDir)bin\JsRouting.Core.Dll" #>
10-
<#@ assembly name="$(ProjectDir)bin\StructureMap.Dll" #>
1110

1211
<#@ import namespace="System.IO" #>
1312
<#@ import namespace="System.Linq" #>
@@ -61,18 +60,16 @@ static Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.C
6160

6261
string GetOutput(){
6362

64-
string[] DoNotScan = { "JsRouting.", "StructureMap.dll" };
63+
string[] DoNotScan = { "JsRouting." };
6564

6665
// get a stream of assemblies that could contain routes, constraints, etc
6766
var assemblies = from fileLocation in Directory.EnumerateFiles(binLocation)
6867
where fileLocation.EndsWith(".dll")
6968
let fileName = Path.GetFileName(fileLocation)
70-
where !DoNotScan.Any(fileName.Contains) // HACK to not scan StrucuteMap and the JsRouting DLLs
69+
where !DoNotScan.Any(fileName.Contains) // HACK to not scan JsRouting DLLs
7170
select fileLocation;
7271

73-
var outputJs = JsRouting.Core.Output.ManagerJs + JsRouting.Core.AssemblyLoader.Load(assemblies).GetInstance<JsRouting.Core.Output>().ToJavaScript();
74-
75-
return outputJs;
72+
return JsRouting.Core.Output.ManagerJs + new JsRouting.Core.Output(JsRouting.Core.AssemblyLoader.Load(assemblies)).ToJavaScript();
7673
}
7774

7875
#>

‎Output/Program.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Output
77
{
88
public static class Program
99
{
10-
private static readonly string[] DoNotScan = { "JsRouting.", "StructureMap.dll" };
10+
private static readonly string[] DoNotScan = { "JsRouting." };
1111

1212
public static void Main(string[] args)
1313
{
@@ -21,13 +21,11 @@ public static void Main(string[] args)
2121
from fileLocation in Directory.EnumerateFiles(assemblyDirectory.Without("\"").Trim())
2222
where fileLocation.EndsWith(".dll")
2323
let fileName = Path.GetFileName(fileLocation)
24-
where !DoNotScan.Any(fileName.Contains) // HACK to not scan StrucuteMap and the JsRouting DLLs
24+
where !DoNotScan.Any(fileName.Contains) // HACK to not scan the JsRouting DLLs
2525
select fileLocation;
2626

27-
var outputJs = JsRouting.Core.Output.ManagerJs +
28-
AssemblyLoader.Load(assemblies)
29-
.GetInstance<JsRouting.Core.Output>()
30-
.ToJavaScript();
27+
var outputJs = JsRouting.Core.Output.ManagerJs +
28+
new JsRouting.Core.Output(AssemblyLoader.Load(assemblies)).ToJavaScript();
3129

3230
// delete file if it already exists
3331
if (File.Exists(outputLocation))

0 commit comments

Comments
 (0)