/*========================================================================================================================== | CONSTRUCTOR \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Establishes a new instance of the <see cref="SampleActivator"/>, including any shared dependencies to be used across /// instances of controllers. /// </summary> /// <remarks> /// The constructor is responsible for establishing dependencies with the singleton lifestyle so that they are available /// to all requests. /// </remarks> public SampleActivator(string connectionString, IWebHostEnvironment webHostEnvironment) { /*------------------------------------------------------------------------------------------------------------------------ | Verify dependencies \-----------------------------------------------------------------------------------------------------------------------*/ Contract.Requires(connectionString, nameof(connectionString)); Contract.Requires(webHostEnvironment, nameof(webHostEnvironment)); /*------------------------------------------------------------------------------------------------------------------------ | Initialize Topic Repository \-----------------------------------------------------------------------------------------------------------------------*/ var sqlTopicRepository = new SqlTopicRepository(connectionString); var cachedTopicRepository = new CachedTopicRepository(sqlTopicRepository); /*------------------------------------------------------------------------------------------------------------------------ | Preload repository \-----------------------------------------------------------------------------------------------------------------------*/ _topicRepository = cachedTopicRepository; _typeLookupService = new EditorViewModelLookupService(); _topicMappingService = new TopicMappingService(_topicRepository, _typeLookupService); _ = _topicRepository.Load(); /*------------------------------------------------------------------------------------------------------------------------ | Establish standard editor composer \-----------------------------------------------------------------------------------------------------------------------*/ _webHostEnvironment = webHostEnvironment; _standardEditorComposer = new StandardEditorComposer(_topicRepository, _webHostEnvironment); }
/*========================================================================================================================== | CONSTRUCTOR \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Establishes a new instance of the <see cref="GoldSimActivator"/>, including any shared dependencies to be used /// across instances of controllers. /// </summary> /// <remarks> /// The constructor is responsible for establishing dependencies with the singleton lifestyle so that they are available /// to all requests. /// </remarks> public GoldSimActivator(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { /*------------------------------------------------------------------------------------------------------------------------ | Verify dependencies \-----------------------------------------------------------------------------------------------------------------------*/ Contract.Requires(configuration, nameof(configuration)); Contract.Requires(webHostEnvironment, nameof(webHostEnvironment)); /*------------------------------------------------------------------------------------------------------------------------ | SAVE STANDARD DEPENDENCIES \-----------------------------------------------------------------------------------------------------------------------*/ _configuration = configuration; _webHostEnvironment = webHostEnvironment; var connectionString = configuration.GetConnectionString("OnTopic"); var sqlTopicRepository = new SqlTopicRepository(connectionString); var cachedTopicRepository = new CachedTopicRepository(sqlTopicRepository); /*------------------------------------------------------------------------------------------------------------------------ | PRELOAD REPOSITORY \-----------------------------------------------------------------------------------------------------------------------*/ _topicRepository = cachedTopicRepository; _typeLookupService = new CompositeTypeLookupService( new GoldSimTopicViewModelLookupService(), new TopicViewModelLookupService(), new EditorViewModelLookupService() ); _topicMappingService = new TopicMappingService(_topicRepository, _typeLookupService); _topicRepository.Load(); /*------------------------------------------------------------------------------------------------------------------------ | INITIALIZE EDITOR COMPOSER \-----------------------------------------------------------------------------------------------------------------------*/ _standardEditorComposer = new(_topicRepository, _webHostEnvironment); /*------------------------------------------------------------------------------------------------------------------------ | CONSTRUCT SMTP CLIENT \-----------------------------------------------------------------------------------------------------------------------*/ var postmarkApiKey = _configuration.GetValue<string>("Postmark:ApiKey"); var postmarkClient = new PostmarkClient(postmarkApiKey); _smtpService = new PostmarkSmtpService(postmarkClient); /*------------------------------------------------------------------------------------------------------------------------ | CONSTRUCT HIERARCHICAL TOPIC MAPPING SERVICES \-----------------------------------------------------------------------------------------------------------------------*/ _hierarchicalTopicMappingService = new CachedHierarchicalTopicMappingService<Models.NavigationTopicViewModel>( new HierarchicalTopicMappingService<Models.NavigationTopicViewModel>( _topicRepository, _topicMappingService ) ); _coursewareTopicMappingService = new CachedHierarchicalTopicMappingService<TrackedNavigationTopicViewModel>( new HierarchicalTopicMappingService<TrackedNavigationTopicViewModel>( _topicRepository, _topicMappingService ) ); }
/// <summary> /// Registers dependencies, and injects them into new instances of view components in response to each request. /// </summary> /// <returns>A concrete instance of an <see cref="Controller"/>.</returns> public object Create(ViewComponentContext context) { /*------------------------------------------------------------------------------------------------------------------------ | Validate parameters \-----------------------------------------------------------------------------------------------------------------------*/ Contract.Requires(context, nameof(context)); /*------------------------------------------------------------------------------------------------------------------------ | Determine view component type \-----------------------------------------------------------------------------------------------------------------------*/ var type = context.ViewComponentDescriptor.TypeInfo.AsType(); /*------------------------------------------------------------------------------------------------------------------------ | Configure and return appropriate view component \-----------------------------------------------------------------------------------------------------------------------*/ if (StandardEditorComposer.IsEditorComponent(type)) { return _standardEditorComposer.ActivateEditorComponent(type, _topicRepository); } throw new InvalidOperationException($"Unknown view component {type.Name}"); }
/// <summary> /// Registers dependencies, and injects them into new instances of view components in response to each request. /// </summary> /// <returns>A concrete instance of an <see cref="IController"/>.</returns> public object Create(ViewComponentContext context) { /*------------------------------------------------------------------------------------------------------------------------ | Determine view component type \-----------------------------------------------------------------------------------------------------------------------*/ var viewComponentType = context.ViewComponentDescriptor.TypeInfo.AsType(); /*------------------------------------------------------------------------------------------------------------------------ | Establish dependencies >------------------------------------------------------------------------------------------------------------------------- | ### HACK JJC20200725: Typically, we use a singleton life cycle for the hierarchical navigation—and, indeed, we have one | defined. During development, however, we'll be using a transient scoped dependency in order to avoid the caching | implemented in the singleton version. Prior to deployment, we'll switch back to the singleton. \-----------------------------------------------------------------------------------------------------------------------*/ var coursewareTopicMappingService = (IHierarchicalTopicMappingService<TrackedNavigationTopicViewModel>)null; if (viewComponentType.Namespace.Contains("Courses", StringComparison.OrdinalIgnoreCase)) { coursewareTopicMappingService = new HierarchicalTopicMappingService<TrackedNavigationTopicViewModel>( _topicRepository, _topicMappingService ); } /*------------------------------------------------------------------------------------------------------------------------ | Resolve \-----------------------------------------------------------------------------------------------------------------------*/ //Handle standard topic editor view components if (StandardEditorComposer.IsEditorComponent(viewComponentType)) { return _standardEditorComposer.ActivateEditorComponent( viewComponentType, _topicRepository ); } //Handle GoldSim-specific view components return viewComponentType.Name switch { nameof(CookiesNoticeViewComponent) => new CookiesNoticeViewComponent(), nameof(MetadataLookupViewComponent) => new MetadataLookupViewComponent(_topicRepository), nameof(MenuViewComponent) => new MenuViewComponent(_topicRepository, _hierarchicalTopicMappingService), nameof(PageLevelNavigationViewComponent) => new PageLevelNavigationViewComponent(_topicRepository, _hierarchicalTopicMappingService), nameof(CallsToActionViewComponent) => new CallsToActionViewComponent(_topicRepository, _hierarchicalTopicMappingService), nameof(FooterViewComponent) => new FooterViewComponent(_topicRepository, _hierarchicalTopicMappingService), nameof(CourseListViewComponent) => new CourseListViewComponent(_topicRepository, coursewareTopicMappingService), nameof(UnitListViewComponent) => new UnitListViewComponent(_topicRepository, coursewareTopicMappingService), nameof(LessonListViewComponent) => new LessonListViewComponent(_topicRepository, coursewareTopicMappingService), nameof(LessonPagingViewComponent) => new LessonPagingViewComponent(_topicRepository, _topicMappingService), _ => throw new Exception($"Unknown view component {viewComponentType.Name}") }; }