|
| 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 |
0 commit comments