In one of my projects, I'm using multiple repositories, one of them Umbraco content cache. While I imagine this is a good way to keep the website loosely integrated with Umbraco, I'm stuck with one problem: injecting UmbracoContext.ContentCache down to the repository from the controller, because UmbracoContext.ContentCache is available only in the page request lifecycle. Has anybody tried to do this? Or it seems like a bad approach? I'm using Autofac for DI.
-
I am curious why you need the ContentCache in the repo layer. What are you trying to do? – Sam Sussman Mar 05 '16 at 13:47
-
The website is a much complex one than just a few (or more) pages to manage within Umbraco. It has functionalities outside Umbraco and integrating with other systems. So it kind of made sense to put Umbraco content cache behind the service abstraction. Umbraco is being used to cover the SEO interests of the stakeholders and provide limited control for the content developers to make on-the-fly changes, manage a blog micro-site, etc... – droidbot Mar 07 '16 at 10:07
2 Answers
Because the UmbracoContext.Current
is a singleton set during the page lifecycle, you should only need to add Umbraco.Core to your repository layer and use the singleton to access the cache. All of this is happening already anyways, no need to rely on autofac to do it again.
Also, while I'm not sure yet if this is the right way to go about it, I registered the UmbracoHelper using autofac which gives access to the typed and dynamic cache.
builder.Register(c => new UmbracoHelper(UmbracoContext.Current));

- 1,005
- 8
- 27
Managed to find a solution. I was wondering that the contentCache reference should be available outside the request's context. And looking at the code here https://github.com/umbraco/Umbraco-CMS/blob/4a101786972bb591bb5d22acd043cc9f9da267ed/src/Umbraco.Web/UmbracoContext.cs
I found this is the way to inject contentCache down to the repositories:
builder.Register(
ctx => new UmbracoContentCacheWrapper(global::Umbraco.Web.UmbracoContext.Current.ContentCache)).InstancePerHttpRequest()
.As<IContextualPublishedContentCache>();
Happy Friday all!

- 937
- 1
- 10
- 26