Skip to content

Commit a43193e

Browse files
committed
Introduced basic configuration for **OnTopic**
Note that I encountered some unexpected challenges with DLL versioning. Specifically, `System.ComponentModel.Annotations` and `System.Data.SqlClient` required a very specific version. This merits further investigation, as this should be far more forgiving and flexible here.
1 parent e6446b6 commit a43193e

7 files changed

Lines changed: 258 additions & 34 deletions

File tree

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,60 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Web;
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client GoldSim
4+
| Project Website
5+
\=============================================================================================================================*/
56
using System.Web.Mvc;
67
using System.Web.Routing;
78

8-
namespace Ignia.Topics.Web.Mvc.Host
9-
{
10-
public class RouteConfig
11-
{
12-
public static void RegisterRoutes(RouteCollection routes)
13-
{
14-
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
15-
16-
routes.MapRoute(
17-
name: "Default",
18-
url: "{controller}/{action}/{id}",
19-
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
20-
);
21-
}
9+
namespace Ignia.Topics.Web.Mvc.Host {
10+
11+
/*============================================================================================================================
12+
| CLASS: ROUTE CONFIGURATION
13+
\---------------------------------------------------------------------------------------------------------------------------*/
14+
/// <summary>
15+
/// Provides default routing configuration for MVC.
16+
/// </summary>
17+
public static class RouteConfig {
18+
19+
/*==========================================================================================================================
20+
| METHOD: REGISTER ROUTES
21+
\-------------------------------------------------------------------------------------------------------------------------*/
22+
/// <summary>
23+
/// Provided a <see cref="RouteCollection"/>, registers all routes associated with the application.
24+
/// </summary>
25+
/// <param name="routes">
26+
/// The route collection for the server, typically passed from the <see cref="System.Web.HttpApplication"/> class.
27+
/// </param>
28+
public static void RegisterRoutes(RouteCollection routes) {
29+
30+
/*------------------------------------------------------------------------------------------------------------------------
31+
| Handle OnTopic redirects
32+
\-----------------------------------------------------------------------------------------------------------------------*/
33+
routes.MapRoute(
34+
name: "TopicRedirect",
35+
url: "Topic/{topicId}",
36+
defaults: new { controller = "Redirect", action = "Redirect" }
37+
);
38+
39+
/*------------------------------------------------------------------------------------------------------------------------
40+
| Handle OnTopic Web namespace
41+
\-----------------------------------------------------------------------------------------------------------------------*/
42+
routes.MapRoute(
43+
name: "WebTopics",
44+
url: "Web/{*path}",
45+
defaults: new { controller = "Topic", action = "Index", id = UrlParameter.Optional, rootTopic = "Web" }
46+
);
47+
48+
/*------------------------------------------------------------------------------------------------------------------------
49+
| Handle default route convention
50+
\-----------------------------------------------------------------------------------------------------------------------*/
51+
routes.MapRoute(
52+
name: "Default",
53+
url: "{controller}/{action}/{id}",
54+
defaults: new { controller = "Fallback", action = "Index", id = UrlParameter.Optional }
55+
);
56+
2257
}
23-
}
58+
59+
} //Class
60+
} //Namespace
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%@ Application Codebehind="Global.asax.cs" Inherits="Ignia.Topics.Web.Mvc.Host.MvcApplication" Language="C#" %>
1+
<%@ Application Codebehind="Global.asax.cs" Inherits="Ignia.Topics.Web.Mvc.Host.Global" Language="C#" %>
Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,56 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client GoldSim
4+
| Project Website
5+
\=============================================================================================================================*/
16
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
47
using System.Web;
58
using System.Web.Mvc;
69
using System.Web.Routing;
710

8-
namespace Ignia.Topics.Web.Mvc.Host
9-
{
10-
public class MvcApplication : System.Web.HttpApplication
11-
{
12-
protected void Application_Start()
13-
{
14-
AreaRegistration.RegisterAllAreas();
15-
RouteConfig.RegisterRoutes(RouteTable.Routes);
16-
}
11+
namespace Ignia.Topics.Web.Mvc.Host {
12+
13+
/*============================================================================================================================
14+
| CLASS: GLOBAL
15+
\---------------------------------------------------------------------------------------------------------------------------*/
16+
/// <summary>
17+
/// Provides default configuration for the application, including any special processing that needs to happen relative to
18+
/// application events (such as <see cref="Application_Start"/> or <see cref="System.Web.HttpApplication.Error"/>.
19+
/// </summary>
20+
public class Global: HttpApplication {
21+
22+
/*==========================================================================================================================
23+
| METHOD: APPLICATION START (EVENT HANDLER)
24+
\-------------------------------------------------------------------------------------------------------------------------*/
25+
/// <summary>
26+
/// Provides initial configuration for the application, including registration of MVC routes via the
27+
/// <see cref="RouteConfig"/> class.
28+
/// </summary>
29+
void Application_Start(object sender, EventArgs e) {
30+
31+
/*------------------------------------------------------------------------------------------------------------------------
32+
| Register controller factory
33+
\-----------------------------------------------------------------------------------------------------------------------*/
34+
ControllerBuilder.Current.SetControllerFactory(
35+
new SampleControllerFactory()
36+
);
37+
38+
/*------------------------------------------------------------------------------------------------------------------------
39+
| Register view engine
40+
\-----------------------------------------------------------------------------------------------------------------------*/
41+
ViewEngines.Engines.Insert(0, new TopicViewEngine());
42+
43+
/*------------------------------------------------------------------------------------------------------------------------
44+
| Register routes
45+
\-----------------------------------------------------------------------------------------------------------------------*/
46+
RouteConfig.RegisterRoutes(RouteTable.Routes);
47+
48+
/*------------------------------------------------------------------------------------------------------------------------
49+
| Require HTTPS
50+
\-----------------------------------------------------------------------------------------------------------------------*/
51+
GlobalFilters.Filters.Add(new RequireHttpsAttribute());
52+
1753
}
18-
}
54+
55+
} //Class
56+
} //Namespace

Ignia.Topics.Web.Mvc.Host/Ignia.Topics.Web.Mvc.Host.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<HintPath>..\packages\Microsoft.Configuration.ConfigurationBuilders.UserSecrets.1.0.2\lib\Net471\Microsoft.Configuration.ConfigurationBuilders.UserSecrets.dll</HintPath>
5151
</Reference>
5252
<Reference Include="Microsoft.CSharp" />
53+
<Reference Include="System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
54+
<HintPath>..\packages\System.ComponentModel.Annotations.4.7.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System.Data.SqlClient, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
57+
<HintPath>..\packages\System.Data.SqlClient.4.8.0\lib\net461\System.Data.SqlClient.dll</HintPath>
58+
</Reference>
5359
<Reference Include="System.Web.DynamicData" />
5460
<Reference Include="System.Web.Entity" />
5561
<Reference Include="System.Web.ApplicationServices" />
@@ -102,6 +108,7 @@
102108
<Compile Include="Global.asax.cs">
103109
<DependentUpon>Global.asax</DependentUpon>
104110
</Compile>
111+
<Compile Include="SampleControllerFactory.cs" />
105112
<Compile Include="Properties\AssemblyInfo.cs" />
106113
</ItemGroup>
107114
<ItemGroup>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client GoldSim
4+
| Project Website
5+
\=============================================================================================================================*/
6+
using System;
7+
using System.Configuration;
8+
using System.Web.Mvc;
9+
using System.Web.Routing;
10+
using Ignia.Topics.Data.Caching;
11+
using Ignia.Topics.Data.Sql;
12+
using Ignia.Topics.Mapping;
13+
using Ignia.Topics.Mapping.Hierarchical;
14+
using Ignia.Topics.Repositories;
15+
using Ignia.Topics.ViewModels;
16+
using Ignia.Topics.Web.Mvc.Controllers;
17+
18+
namespace Ignia.Topics.Web.Mvc.Host {
19+
20+
/*============================================================================================================================
21+
| CLASS: CONTROLLER FACTORY
22+
\---------------------------------------------------------------------------------------------------------------------------*/
23+
/// <summary>
24+
/// Responsible for creating instances of factories in response to web requests. Represents the Composition Root for
25+
/// Dependency Injection.
26+
/// </summary>
27+
class SampleControllerFactory : DefaultControllerFactory {
28+
29+
/*==========================================================================================================================
30+
| PRIVATE INSTANCES
31+
\-------------------------------------------------------------------------------------------------------------------------*/
32+
private readonly ITypeLookupService _typeLookupService = null;
33+
private readonly ITopicMappingService _topicMappingService = null;
34+
private readonly ITopicRepository _topicRepository = null;
35+
private readonly Topic _rootTopic = null;
36+
37+
private readonly IHierarchicalTopicMappingService<NavigationTopicViewModel> _hierarchicalTopicMappingService = null;
38+
39+
/*==========================================================================================================================
40+
| CONSTRUCTOR
41+
\-------------------------------------------------------------------------------------------------------------------------*/
42+
/// <summary>
43+
/// Establishes a new instance of the <see cref="SampleControllerFactory"/>, including any shared dependencies to be used
44+
/// across instances of controllers.
45+
/// </summary>
46+
public SampleControllerFactory() : base() {
47+
48+
/*------------------------------------------------------------------------------------------------------------------------
49+
| ESTABLISH DATABASE CONNECTION
50+
\-----------------------------------------------------------------------------------------------------------------------*/
51+
var connectionString = ConfigurationManager.ConnectionStrings["OnTopic"].ConnectionString;
52+
var sqlTopicRepository = new SqlTopicRepository(connectionString);
53+
54+
/*------------------------------------------------------------------------------------------------------------------------
55+
| SAVE STANDARD DEPENDENCIES
56+
\-----------------------------------------------------------------------------------------------------------------------*/
57+
_topicRepository = new CachedTopicRepository(sqlTopicRepository);
58+
_typeLookupService = new TopicViewModelLookupService();
59+
_topicMappingService = new TopicMappingService(_topicRepository, _typeLookupService);
60+
_rootTopic = _topicRepository.Load();
61+
62+
/*------------------------------------------------------------------------------------------------------------------------
63+
| CONSTRUCT HIERARCHICAL TOPIC MAPPING SERVICE
64+
\-----------------------------------------------------------------------------------------------------------------------*/
65+
var service = new HierarchicalTopicMappingService<NavigationTopicViewModel>(
66+
_topicRepository,
67+
_topicMappingService
68+
);
69+
70+
_hierarchicalTopicMappingService = new CachedHierarchicalTopicMappingService<NavigationTopicViewModel>(
71+
service
72+
);
73+
74+
}
75+
76+
/*==========================================================================================================================
77+
| GET CONTROLLER INSTANCE
78+
\-------------------------------------------------------------------------------------------------------------------------*/
79+
/// <summary>
80+
/// Overrides the factory method for creating new instances of controllers.
81+
/// </summary>
82+
/// <returns>A concrete instance of an <see cref="IController"/>.</returns>
83+
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
84+
85+
/*------------------------------------------------------------------------------------------------------------------------
86+
| Register
87+
\-----------------------------------------------------------------------------------------------------------------------*/
88+
var mvcTopicRoutingService = new MvcTopicRoutingService(
89+
_topicRepository,
90+
requestContext.HttpContext.Request.Url,
91+
requestContext.RouteData
92+
);
93+
94+
/*------------------------------------------------------------------------------------------------------------------------
95+
| Resolve
96+
\-----------------------------------------------------------------------------------------------------------------------*/
97+
switch (controllerType.Name) {
98+
99+
case nameof(RedirectController):
100+
return new RedirectController(_topicRepository);
101+
102+
case nameof(SitemapController):
103+
return new SitemapController(_topicRepository);
104+
105+
/*
106+
case nameof(ErrorController):
107+
return new ErrorController();
108+
109+
case nameof(LayoutController):
110+
return new LayoutController(mvcTopicRoutingService, _hierarchicalTopicMappingService, _topicRepository);
111+
*/
112+
113+
case nameof(TopicController):
114+
return new TopicController(_topicRepository, mvcTopicRoutingService, _topicMappingService);
115+
116+
default:
117+
return base.GetControllerInstance(requestContext, controllerType);
118+
119+
}
120+
121+
}
122+
123+
} //Class
124+
} //Namespace

Ignia.Topics.Web.Mvc.Host/Web.config

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<!--
33
For more information on how to configure your ASP.NET application, please visit
44
https://go.microsoft.com/fwlink/?LinkId=301880
@@ -20,11 +20,20 @@
2020
</appSettings>
2121
<connectionStrings configBuilders="ConnectionStrings"></connectionStrings>
2222
<system.web>
23-
<compilation debug="true" targetFramework="4.8" />
23+
<compilation debug="true" targetFramework="4.8">
24+
<assemblies>
25+
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
26+
</assemblies>
27+
</compilation>
2428
<httpRuntime targetFramework="4.8" />
2529
</system.web>
30+
2631
<runtime>
2732
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
33+
<dependentAssembly>
34+
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
35+
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
36+
</dependentAssembly>
2837
<dependentAssembly>
2938
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
3039
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
@@ -37,12 +46,18 @@
3746
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
3847
<bindingRedirect oldVersion="1.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
3948
</dependentAssembly>
49+
<dependentAssembly>
50+
<assemblyIdentity name="System.Data.SqlClient" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
51+
<bindingRedirect oldVersion="0.0.0.0-4.6.1.0" newVersion="4.6.1.0" />
52+
</dependentAssembly>
4053
</assemblyBinding>
4154
</runtime>
55+
4256
<system.codedom>
4357
<compilers>
4458
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
4559
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
4660
</compilers>
4761
</system.codedom>
62+
4863
</configuration>

Ignia.Topics.Web.Mvc.Host/packages.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
<package id="Microsoft.Configuration.ConfigurationBuilders.Base" version="1.0.1" targetFramework="net48" />
88
<package id="Microsoft.Configuration.ConfigurationBuilders.UserSecrets" version="1.0.2" targetFramework="net48" />
99
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net48" />
10+
<package id="System.ComponentModel.Annotations" version="4.2.1" targetFramework="net48" />
11+
<package id="System.Data.SqlClient" version="4.6.1" targetFramework="net48" />
12+
<package id="System.Linq" version="4.3.0" targetFramework="net47" />
1013
</packages>

0 commit comments

Comments
 (0)