-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleActivator.cs
152 lines (129 loc) · 8.63 KB
/
SampleActivator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Sample OnTopic Site
\=============================================================================================================================*/
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using OnTopic.Data.Caching;
using OnTopic.Data.Sql;
using OnTopic.Editor.AspNetCore.Attributes;
using OnTopic.Editor.AspNetCore.Controllers;
using OnTopic.Editor.AspNetCore.Infrastructure;
using OnTopic.Internal.Diagnostics;
using OnTopic.Lookup;
using OnTopic.Mapping;
using OnTopic.Repositories;
namespace OnTopicTest {
/*============================================================================================================================
| CLASS: SAMPLE ACTIVATOR
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Responsible for creating instances of factories in response to web requests. Represents the Composition Root for
/// Dependency Injection.
/// </summary>
public class SampleActivator : IControllerActivator, IViewComponentActivator {
/*==========================================================================================================================
| PRIVATE INSTANCES
\-------------------------------------------------------------------------------------------------------------------------*/
private readonly ITypeLookupService _typeLookupService;
private readonly ITopicMappingService _topicMappingService;
private readonly ITopicRepository _topicRepository;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly StandardEditorComposer _standardEditorComposer;
/*==========================================================================================================================
| 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);
}
/*==========================================================================================================================
| METHOD: CREATE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Registers dependencies, and injects them into new instances of controllers in response to each request.
/// </summary>
/// <returns>A concrete instance of an <see cref="Controller"/>.</returns>
public object Create(ControllerContext context) {
/*------------------------------------------------------------------------------------------------------------------------
| Validate parameters
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Requires(context, nameof(context));
/*------------------------------------------------------------------------------------------------------------------------
| Determine controller type
\-----------------------------------------------------------------------------------------------------------------------*/
var type = context.ActionDescriptor.ControllerTypeInfo.AsType();
/*------------------------------------------------------------------------------------------------------------------------
| Configure and return appropriate controller
\-----------------------------------------------------------------------------------------------------------------------*/
if (type == typeof(EditorController)) {
return new EditorController(_topicRepository, _topicMappingService);
}
else {
throw new InvalidOperationException($"Unknown controller {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="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}");
}
/*==========================================================================================================================
| METHOD: RELEASE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Responds to a request to release resources associated with a particular controller.
/// </summary>
public void Release(ControllerContext context, object controller) { }
/// <summary>
/// Responds to a request to release resources associated with a particular view component.
/// </summary>
public void Release(ViewComponentContext context, object viewComponent) { }
} //Class
} //Namespace