While setting up an Umbraco site you'll probably end up with some "site settings" that will eather be on a top level node or on a child settings node. Think things like setting up the nav, the footer...
These are items you only set up once a site and want to manage in a global way.
You can of course write a query each time to traverse the tree... but something that v8 makes easy and possible is to extend the UmbracoHelper that is available in your Razor Views.
Imagine that you can simply do @Umbraco.WebSiteSettings.Footer and that you get the data you want (strongly typed). THis is nice and readable and folks without a lot of razor knowledge can also see what is happening...(maybe if a frontend dev needs to do some changes).
Ok let's get started. First of all, to do the stronlgy typed stuff, use Modelbuilders (duh).
I tend to use this config:
Once generated you'll need to include the files in the vs project...
With that in place I now have strongly typed models of my document types...
Next is to get into the DI magic of V8, and build a custom Service
Define an interface (I called mine ISiteService)
It has 2 methods, 1 that fetches a Website doc based on id and one that will fetch the SearchPage doc based on id (both returning strongly typed models generated by ModelsBuilder)
The implementation looks like this
So using the alias of the type. And then looks for the ancestor of the current fed in node id of that alias (since the website will always be at the top of the tree)
For the Searchpage, it first fetched the Website and then looks for the child of the correct alias (since it will be located directly under the website node)
Of course for all of this to work we need to register the custom service
And now we can inject it where we want...
But I'll also create an extension for the UmbracoHelper
Since it is static I can't use the standard constructor approach for injecting my custom service but I can still get to it with
And with all that in place I can now access my service from the Umbraco Helper in views: