Long answer: the services are accessed via dependency injection. Either you just have it as a parameter on your class’ constructor, or you use Current.Factory.GetInstance
When I try to do this (Umbraco.Web.Composing.Current.UmbracoHelper) from a class library 'Current' is null. How do you get the umbracoHelper in a class library in v8?
I would not recommend this. You should read up on dependency injection.
When you want to use UmbracoContext, you Almost always are in a context where you already have it. If you are building some static method to do something reusable, just give the context as an argument.
I’m very aware of DI and IOC so I don’t think I need to “read up” on that. Please don’t assume that someone that posts something that you (without context) don’t agree with don’t know what they are doing.
Service Locator is an anti pattern but it’s not illegal to use when it makes more sense.
I agree with you on a general level, injecting dependencies is always best and should always be preferred.
In my case the creation of the instance is out of my (and the IOC Contains) control and it only accepts a parameterless constructor, the method calls is also specified in external code so Service Locator is the least bad solution.
For anyone with this problem in the future and using a view with UmbracoContext.Current from v7.
I went from this v7 code:
@model Umbraco.Forms.Mvc.Models.FieldViewModel
@{
var memberShipHelper = new MembershipHelper(UmbracoContext.Current);
var member = memberShipHelper.GetCurrentMember();
// Code using member
}
To this for v8:
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Forms.Mvc.Models.FieldViewModel>
@{
var member = (myMemberModel)Members.GetCurrentMember();
// Code using member
}
Services are also available in this partial where I am unable to use DI.
Getting the current UmbracoContext in v8?
So i have the following code which does not work any more:
How do i create a new instance of the UmbracoHelper in v8?
Short answer: you don’t
Long answer: the services are accessed via dependency injection. Either you just have it as a parameter on your class’ constructor, or you use Current.Factory.GetInstance
Is the current a static class somewhere? I'm trying to access it from outside any umbraco view or controller.
Do you know if there is any documentation or topics on this?
Yup, static class. It's in Umbraco.Web.Composing. A version also exists in Umbraco.Core.Compsing, but exposes different stuff.
More than likely you'll be chasing the Umbraco.Web.Composing one.
UmbracoHelper is still available though - it's a property on Current. From a quick look though, I don't think it has all the same methods as in v7.
Depending on what you're trying to do, Current.UmbracoContext.ContentCache has some useful bits too.
Definitely have a poke around in Current.
Thank you! Umbraco.Web.Composing.Current.UmbracoHelper is the new way.
Umbraco.Web.Composing.Current.UmbracoContext
When I try to do this (Umbraco.Web.Composing.Current.UmbracoHelper) from a class library 'Current' is null. How do you get the umbracoHelper in a class library in v8?
I ended up here after looking for a way to get the UmbracoContext,
The old way would be UmbracoContext.Current,
After some looking around I found this that seems to work for me
Just wanted to share this is anyone else have the same needs.
// m
I would not recommend this. You should read up on dependency injection.
When you want to use UmbracoContext, you Almost always are in a context where you already have it. If you are building some static method to do something reusable, just give the context as an argument.
Hi Søren!
I’m very aware of DI and IOC so I don’t think I need to “read up” on that. Please don’t assume that someone that posts something that you (without context) don’t agree with don’t know what they are doing.
Service Locator is an anti pattern but it’s not illegal to use when it makes more sense.
I agree with you on a general level, injecting dependencies is always best and should always be preferred.
In my case the creation of the instance is out of my (and the IOC Contains) control and it only accepts a parameterless constructor, the method calls is also specified in external code so Service Locator is the least bad solution.
All the best.
For anyone with this problem in the future and using a view with UmbracoContext.Current from v7.
I went from this v7 code:
To this for v8:
Services are also available in this partial where I am unable to use DI.
Umbraco.Web.Composing.UmbracoContext
is working on a reply...