Skip to content

Commit 180526e

Browse files
committed
Merge branch 'feature/ITopicRepository.Load-parentTopic' into develop
According to the `ITopicRepository.Load()` signature, a topic can be loaded from anywhere within the topic graph. And, indeed, that works just fine assuming one is querying e.g. the `CachedTopicRepository`, which has access to the entire tree. If one queries a data store—e.g., via the `SqlTopicRepository`—however, that can result in an incomplete topic being returned, since it won't be able to find topic references and relationships which occur outside the `Load()` scope. That can then cause problems if the topic is then saved, as those orphaned references may get overwritten. (Note: In practice, this usually only affects `Load(topicId, version)` since most applications use the `CachedTopicRepository`. And since relationships and references aren't currently versioned, this doesn't have an impact on `Rollback()` either. Nevertheless, it remains a hole in the design that could yield unexpected bugs, if not data loss.) In order to resolve this, the `ITopicRepository.Load()` signature has been extended to accept an optional `referenceTopic`. When present, this prepopulates the list of topics with those already in memory. This ensures that a topic being loaded has access to the rest of the graph, at least insofar as the current application is aware of it. Further, this optionally allows `ITopicRepository` instances to update existing instances of topics, instead of loading new object references referring to the same entity, which can cause additional problems. As part of that, the `SqlTopicRepository`'s `SqlDataReader` extensions now accept a `markDirty` parameter to control whether or not topics, collections, and values are marked dirty when loaded. This allows `Rollback()` to use the `Load(referenceTopic, version)` directly since, unlike `Load()`, it _should_ mark modified attributes as `IsDirty`. Finally, a number of bugs were resolved as part of this update. For example, the `GetTopicVersion` stored procedure wasn't correctly returning either the core attributes (since they were moved to the `Topics` table), nor the `Version` column for the `Attributes` or `ExtendedAttributes` table.
2 parents 826b93c + 1c2b25b commit 180526e

File tree

18 files changed

+239
-186
lines changed

18 files changed

+239
-186
lines changed

OnTopic.AspNetCore.Mvc/Components/MenuViewComponentBase{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ IHierarchicalTopicMappingService<T> hierarchicalTopicMappingService
7979
var configuredRoot = CurrentTopic.Attributes.GetValue("NavigationRoot", true);
8080

8181
if (!String.IsNullOrEmpty(configuredRoot)) {
82-
navigationRootTopic = TopicRepository.Load("Root:" + configuredRoot);
82+
navigationRootTopic = TopicRepository.Load("Root:" + configuredRoot, CurrentTopic);
8383
}
8484
if (navigationRootTopic is null) {
8585
navigationRootTopic = HierarchicalTopicMappingService.GetHierarchicalRoot(CurrentTopic, 2, "Web");

OnTopic.Data.Caching/CachedTopicRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public CachedTopicRepository(ITopicRepository dataProvider) : base() {
7979
| METHOD: LOAD
8080
\-------------------------------------------------------------------------------------------------------------------------*/
8181
/// <inheritdoc />
82-
public override Topic? Load(int topicId, bool isRecursive = true) {
82+
public override Topic? Load(int topicId, Topic? referenceTopic = null, bool isRecursive = true) {
8383

8484
/*------------------------------------------------------------------------------------------------------------------------
8585
| Handle request for entire tree
@@ -96,7 +96,7 @@ public CachedTopicRepository(ITopicRepository dataProvider) : base() {
9696
}
9797

9898
/// <inheritdoc />
99-
public override Topic? Load(string? uniqueKey = null, bool isRecursive = true) {
99+
public override Topic? Load(string? uniqueKey = null, Topic? referenceTopic = null, bool isRecursive = true) {
100100

101101
/*------------------------------------------------------------------------------------------------------------------------
102102
| Lookup by TopicKey
@@ -113,7 +113,7 @@ public CachedTopicRepository(ITopicRepository dataProvider) : base() {
113113
}
114114

115115
/// <inheritdoc />
116-
public override Topic? Load(int topicId, DateTime version) {
116+
public override Topic? Load(int topicId, DateTime version, Topic? referenceTopic = null) {
117117

118118
/*------------------------------------------------------------------------------------------------------------------------
119119
| Validate parameters
@@ -127,7 +127,7 @@ public CachedTopicRepository(ITopicRepository dataProvider) : base() {
127127
/*------------------------------------------------------------------------------------------------------------------------
128128
| Return appropriate topic
129129
\-----------------------------------------------------------------------------------------------------------------------*/
130-
return _dataProvider.Load(topicId, version);
130+
return _dataProvider.Load(topicId, version, referenceTopic?? _cache);
131131

132132
}
133133

OnTopic.Data.Sql.Database/Stored Procedures/GetTopicVersion.sql

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,13 @@ AS
1616
--------------------------------------------------------------------------------------------------------------------------------
1717
-- SELECT KEY ATTRIBUTES
1818
--------------------------------------------------------------------------------------------------------------------------------
19-
;WITH KeyAttributes
20-
AS (
21-
SELECT TopicID,
22-
AttributeKey,
23-
AttributeValue,
24-
RowNumber = ROW_NUMBER() OVER (
25-
PARTITION BY TopicID,
26-
AttributeKey
27-
ORDER BY Version DESC
28-
)
29-
FROM Attributes
30-
WHERE TopicID = @TopicID
31-
AND Version <= @Version
32-
AND AttributeKey
33-
IN ( 'Key',
34-
'ParentID',
35-
'ContentType'
36-
)
37-
)
3819
SELECT TopicID,
39-
ContentType,
40-
ParentID,
41-
[Key] AS 'TopicKey',
42-
1 AS 'SortOrder'
43-
FROM KeyAttributes
44-
PIVOT ( MIN(AttributeValue)
45-
FOR AttributeKey
46-
IN ( [ContentType],
47-
[ParentID],
48-
[Key]
49-
)
50-
) AS Pvt
51-
WHERE RowNumber = 1
20+
ContentType,
21+
ParentID,
22+
TopicKey,
23+
0 AS SortOrder
24+
FROM Topics
25+
WHERE TopicID = @TopicID
5226

5327
--------------------------------------------------------------------------------------------------------------------------------
5428
-- SELECT TOPIC ATTRIBUTES
@@ -58,6 +32,7 @@ AS (
5832
SELECT TopicID,
5933
AttributeKey,
6034
AttributeValue,
35+
Version,
6136
RowNumber = ROW_NUMBER() OVER (
6237
PARTITION BY TopicID,
6338
AttributeKey
@@ -69,7 +44,8 @@ AS (
6944
)
7045
SELECT TopicID,
7146
AttributeKey,
72-
AttributeValue
47+
AttributeValue,
48+
Version
7349
FROM TopicAttributes
7450
WHERE RowNumber = 1
7551

@@ -80,6 +56,7 @@ WHERE RowNumber = 1
8056
AS (
8157
SELECT TopicID,
8258
AttributesXml,
59+
Version,
8360
RowNumber = ROW_NUMBER() OVER (
8461
PARTITION BY TopicID
8562
ORDER BY Version DESC
@@ -89,7 +66,8 @@ AS (
8966
AND Version <= @Version
9067
)
9168
SELECT TopicID,
92-
AttributesXml
69+
AttributesXml,
70+
Version
9371
FROM TopicExtendedAttributes
9472
WHERE RowNumber = 1
9573

0 commit comments

Comments
 (0)