Skip to content

Instantly share code, notes, and snippets.

@JeremyCaney
Last active February 19, 2019 03:10
Show Gist options
  • Save JeremyCaney/6ba4bb0465b7dd1992a7ffdaa1ebf813 to your computer and use it in GitHub Desktop.
Save JeremyCaney/6ba4bb0465b7dd1992a7ffdaa1ebf813 to your computer and use it in GitHub Desktop.
Reference version of a ControllerFactory used to configure OnTopic.
/*==============================================================================================================================
| Author Ignia, LLC
| Client OrganizationName
| Project Website
\=============================================================================================================================*/
using System;
using System.Web.Mvc;
using System.Web.Routing;
using Ignia.Topics;
using Ignia.Topics.Mapping;
using Ignia.Topics.Repositories;
using Ignia.Topics.Web;
using Ignia.Topics.Web.Mvc;
namespace OrganizationName.Web {
/*============================================================================================================================
| CLASS: CONTROLLER FACTORY
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Responsible for creating instances of factories in response to web requests. Represents the Composition Root for
/// Dependency Injection.
/// </summary>
class OrganizationNameControllerFactory : DefaultControllerFactory {
/*==========================================================================================================================
| PRIVATE INSTANCES
\-------------------------------------------------------------------------------------------------------------------------*/
private readonly ITypeLookupService _viewModelLookupService = null;
private readonly ITopicMappingService _topicMappingService = null;
private readonly ITopicRepository _topicRepository = null;
private readonly Topic _rootTopic = null;
private readonly IHierarchicalTopicMappingService<NavigationTopicViewModel> _hierarchicalTopicMappingService = null;
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Establishes a new instance of the <see cref="OrganizationNameControllerFactory"/>, including any shared dependencies
/// to be used across instances of controllers.
/// </summary>
public OrganizationNameControllerFactory() : base() {
/*------------------------------------------------------------------------------------------------------------------------
| ESTABLISH DATABASE CONNECTION
\-----------------------------------------------------------------------------------------------------------------------*/
var connectionString = ConfigurationManager.ConnectionStrings["OnTopic"].ConnectionString;
var sqlTopicRepository = new SqlTopicRepository(connectionString);
/*------------------------------------------------------------------------------------------------------------------------
| SAVE STANDARD DEPENDENCIES
\-----------------------------------------------------------------------------------------------------------------------*/
_viewModelLookupService = new TopicViewModelLookupService();
_topicRepository = new CachedTopicRepository(sqlTopicRepository);
_topicMappingService = new TopicMappingService(_topicRepository, _viewModelLookupService);
_rootTopic = cachedTopicRepository.Load();
/*------------------------------------------------------------------------------------------------------------------------
| CONSTRUCT HIERARCHICAL TOPIC MAPPING SERVICE
\-----------------------------------------------------------------------------------------------------------------------*/
var service = new HierarchicalTopicMappingService<NavigationTopicViewModel>(
_topicRepository,
_topicMappingService
);
_hierarchicalTopicMappingService = new CachedHierarchicalTopicMappingService<NavigationTopicViewModel>(
service
);
}
/*==========================================================================================================================
| GET CONTROLLER INSTANCE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Overrides the factory method for creating new instances of controllers.
/// </summary>
/// <returns>A concrete instance of an <see cref="IController"/>.</returns>
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
/*------------------------------------------------------------------------------------------------------------------------
| Register
\-----------------------------------------------------------------------------------------------------------------------*/
var mvcTopicRoutingService = new MvcTopicRoutingService(
_topicRepository,
requestContext.HttpContext.Request.Url,
requestContext.RouteData
);
// Set default controller
if (controllerType == null) {
controllerType = typeof(FallbackController);
}
/*------------------------------------------------------------------------------------------------------------------------
| Resolve
\-----------------------------------------------------------------------------------------------------------------------*/
switch (controllerType.Name) {
case nameof(TopicController):
return new TopicController(_topicRepository, mvcTopicRoutingService, _topicMappingService);
case nameof(LayoutController):
return new LayoutController(mvcTopicRoutingService, _hierarchicalTopicMappingService, _topicRepository);
case (nameof(RedirectController)):
return new RedirectController(_topicRepository);
case (nameof(SitemapController)):
return new SitemapController(_topicRepository);
case (nameof(ErrorController)):
return new ErrorController();
case default:
return base.GetControllerInstance(requestContext, controllerType);
}
}
} //Class
} //Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment