Skip to content

Commit 0bddfb0

Browse files
committed
Introduced new GetRootTopic() extension method
Originally, the `OnTopic.Querying` class was focused on querying the current `Topic` and its descendants. Increasingly, however, we're identifying needs to work with the entire in-memory topic graph starting from an arbitrary position within the tree. This is particularly useful when it's difficult or inappropriate to inject an `ITopicRepository` such that we have access from the root. To start, the `GetRootTopic()` extension now allows us to easily access the root topic of any graph starting from any arbitrary point. It's a really simple recrusive function. This includes a unit test for validating the functionality.
1 parent cfa8fd3 commit 0bddfb0

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

OnTopic.Tests/TopicQueryingTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,25 @@ public void FindAllByAttribute_ReturnsCorrectTopics() {
4444

4545
}
4646

47+
/*==========================================================================================================================
48+
| TEST: GET ROOT TOPIC: RETURNS ROOT TOPIC
49+
\-------------------------------------------------------------------------------------------------------------------------*/
50+
/// <summary>
51+
/// Given a deeply nested <see cref="Topic"/>, returns the root <see cref="Topic"/>.
52+
/// </summary>
53+
[TestMethod]
54+
public void GetRootTopic_ReturnsRootTopic() {
55+
56+
var parentTopic = TopicFactory.Create("ParentTopic", "Page", 1);
57+
var childTopic = TopicFactory.Create("ChildTopic", "Page", 5, parentTopic);
58+
var grandChildTopic = TopicFactory.Create("GrandChildTopic", "Page", 20, childTopic);
59+
var greatGrandChildTopic = TopicFactory.Create("GreatGrandChildTopic", "Page", 7, grandChildTopic);
60+
61+
var rootTopic = greatGrandChildTopic.GetRootTopic();
62+
63+
Assert.ReferenceEquals(parentTopic, rootTopic);
64+
65+
}
66+
4767
} //Class
4868
} //Namespace

OnTopic/Querying/TopicExtensions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,34 @@ public static ReadOnlyTopicCollection<Topic> FindAllByAttribute(this Topic topic
142142

143143
}
144144

145+
/*==========================================================================================================================
146+
| METHOD: GET ROOT TOPIC
147+
\-------------------------------------------------------------------------------------------------------------------------*/
148+
/// <summary>
149+
/// Retrieves the root <see cref="Topic"/> in the current topic graph.
150+
/// </summary>
151+
/// <param name="topic">The instance of the <see cref="Topic"/> to operate against; populated automatically by .NET.</param>
152+
/// <returns>The <see cref="Topic"/> at the root o the current topic graph.</returns>
153+
public static Topic GetRootTopic(this Topic topic) {
154+
155+
/*------------------------------------------------------------------------------------------------------------------------
156+
| Validate contracts
157+
\-----------------------------------------------------------------------------------------------------------------------*/
158+
Contract.Requires(topic, "The topic parameter must be specified.");
159+
160+
/*------------------------------------------------------------------------------------------------------------------------
161+
| Find lowest common root
162+
\-----------------------------------------------------------------------------------------------------------------------*/
163+
while (topic.Parent != null) {
164+
topic = topic.Parent;
165+
}
166+
167+
/*------------------------------------------------------------------------------------------------------------------------
168+
| Return root
169+
\-----------------------------------------------------------------------------------------------------------------------*/
170+
return topic;
171+
172+
}
173+
145174
} //Class
146175
} //Namespace

0 commit comments

Comments
 (0)