Hi there - super contrived example here, but I have this problem where I have a Helpers file with some methods I've collected through years of Umbraco experience.
So in a Template or a Partial, I can do stuff like this:
@using Vintage;
<p>@Helpers.DoStuff()</p>
My problem is that now with .net Core I don't know how to do this thing that used to be fairly simple (using UmbracoHelper). I'm aware that I need to use Dependency Injection somehow, but I just can't wrap my head around how much ceremony is needed for it to work, so here's a very thin reduction of my problem:
namespace Vintage;
public class Helpers {
public static GetProductById(int id) {
return umbracoHelper.Content(id)
}
}
What would I need to do, to get a reference to an UmbracoHelper (or whatever its equivalent is in Umbraco 10) in the above example?
I may have misunderstood your question though, in a C# class the ceremony (also known as constructor injection) looks a little like:
using Umbraco.Cms.Web.Common;
namespace Vintage;
public class Helpers {
private readonly UmbracoHelper _umbracoHelper;
public Helpers(UmbracoHelper umbracoHelper) {
_umbracoHelper = umbracoHelper;
}
public static GetProductById(int id) {
return _umbracoHelper.Content(id)
}
}
This gets super "fun" when you eventually stumble upon a No parameterless constructor defined error. That's something we can talk about if and when that happens though! π
Or if you no able to use DI you can always use the static service provider to get a instance.
namespace Vintage;
public static class Helpers {
public static IPublishedContent? GetProductById(int id)
{
var accessor = StaticServiceProvider.Instance.GetRequiredService<IUmbracoHelperAccessor>();
if (accessor.TryGetUmbracoHelper(out var umbracoHelper))
{
return umbracoHelper.Content(id);
}
return null;
}
}
I know it is a last resort and that DI is preferred.
But we are in the middle of upgrading a V8 site to V10 that makes uses of Current a lot. So we are using this as well before we do a proper refactor.
Getting an UmbracoHelper in a custom class
Hi there - super contrived example here, but I have this problem where I have a Helpers file with some methods I've collected through years of Umbraco experience.
So in a Template or a Partial, I can do stuff like this:
My problem is that now with .net Core I don't know how to do this thing that used to be fairly simple (using
UmbracoHelper
). I'm aware that I need to use Dependency Injection somehow, but I just can't wrap my head around how much ceremony is needed for it to work, so here's a very thin reduction of my problem:What would I need to do, to get a reference to an
UmbracoHelper
(or whatever its equivalent is in Umbraco 10) in the above example?Thanks!
/Chriztian
You'll hopefully be very pleased at this tiny ceremony:
I may have misunderstood your question though, in a C# class the ceremony (also known as constructor injection) looks a little like:
This gets super "fun" when you eventually stumble upon a
No parameterless constructor defined
error. That's something we can talk about if and when that happens though! πThanks Sebastiaan;
Yeah, this puts me at βAn object reference is required for the non-static field, method, or property
Helpers._umbracoHelper
...β/Chriztian
Or if you no able to use DI you can always use the static service provider to get a instance.
Dave
Thanks Dave!
I got it working with this after some fiddling (namespaces...).
If I understand the (source)code comments correctly this is the absolute last resort method, but thanks again - it works!! :D
/Chriztian
Hi Chriztian,
I know it is a last resort and that DI is preferred. But we are in the middle of upgrading a V8 site to V10 that makes uses of Current a lot. So we are using this as well before we do a proper refactor.
Dave
is working on a reply...