Reputation: 4618
I'm looking at improving performance on my Umbraco site (7.6.4) site and this line appears a lot:
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
I can add the UmbracoHelper to IOC (via Autofac)
builder.Register(c => new UmbracoHelper(UmbracoContext.Current));
So i can do this, but should i be doing this, or could i get unexpected results?
Upvotes: 1
Views: 576
Reputation: 105
This is what I use for umbraco + AutoFaq
builder.Register(c => ApplicationContext.Current).InstancePerRequest();
builder.Register(c => UmbracoContext.Current).InstancePerRequest();
builder.Register(c => new UmbracoHelper(c.Resolve<UmbracoContext>())).InstancePerRequest();
Upvotes: 1
Reputation: 3271
I don't think UmbracoHelper uses an interface, so if you want to use IOC to mock out umbraco dependencies, you'll probably run into trouble if you're not using Microsoft Fakes.
I don't know if it's best practice, but the way I've added the umbraco dependencies is by registering the different components of UmbracoHelper that do have an interface (The following code is Unity, not Autofac):
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
if (!Container.IsRegistered<IUmbracoComponentRenderer>())
Container.RegisterType<IUmbracoComponentRenderer>(new InjectionFactory(o => umbracoHelper.UmbracoComponentRenderer));
if (!Container.IsRegistered<ITypedPublishedContentQuery>())
Container.RegisterType<ITypedPublishedContentQuery>(new InjectionFactory(o => umbracoHelper.ContentQuery));
This way I can easily mock the interfaces without having to use Microsoft Fakes.
Upvotes: 0