Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I am trying to create a custom attribute that uses umbraco dictionary value.
I do not know how to get the UmbracoHelper instance as I don't thunk a custom attribute supports dependency injection.
UmbracoHelper
Any thoughts how to get UmbracoHelper in other way than DI?
This is what I have:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class UmbracoDisplayAttribute : DisplayNameAttribute { private readonly UmbracoHelper _umbracoHelper; // <- this value is missing // This is a positional argument public UmbracoDisplayAttribute(string dictionaryKey) : base(dictionaryKey) { } public override string DisplayName { get { return _umbracoHelper != null ? _umbracoHelper.GetDictionaryValue(base.DisplayName) : string.Empty; } } }
Is there any way of getting UmbracoHelper without DI in Umbraco 9?
Hi nickornotto,
Dependency injection in attributes is not possible. This not a Umbraco thing but .NET thing.
You could retrieve the dependency in your attribute using the service locator pattern.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class UmbracoDisplayAttribute : DisplayNameAttribute { // This is a positional argument public UmbracoDisplayAttribute(string dictionaryKey) : base(dictionaryKey) { } public override string DisplayName { get { var umbracoHelper = new HttpContextAccessor().HttpContext.RequestServices.GetService<UmbracoHelper>(); return umbracoHelper != null ? umbracoHelper.GetDictionaryValue(base.DisplayName) : string.Empty; } } }
Bril, thank you!
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion
How to get UmbracoHelper in a custom attribute without DI?
I am trying to create a custom attribute that uses umbraco dictionary value.
I do not know how to get the
UmbracoHelper
instance as I don't thunk a custom attribute supports dependency injection.Any thoughts how to get
UmbracoHelper
in other way than DI?This is what I have:
Is there any way of getting UmbracoHelper without DI in Umbraco 9?
Hi nickornotto,
Dependency injection in attributes is not possible. This not a Umbraco thing but .NET thing.
You could retrieve the dependency in your attribute using the service locator pattern.
Bril, thank you!
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.