Advice on injecting a localisation service for a custom class
Hi All,
I am creating a very simple custom required attribute which I have got working but it currently involves an anti-patter that I am trying to replace with an injected localisation service.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Current = Umbraco.Web.Composing.Current;
namespace MyUmbraco.DataAnnotations.Attributes
{
public class UmbracoRequiredAttribute : RequiredAttribute, IClientValidatable
{
public string DictionaryKey { get; set; } = "Validation.Required";
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var ErrorMessage = Current.UmbracoHelper.GetDictionaryValue(DictionaryKey);
yield return new ModelClientValidationRequiredRule(ErrorMessage);
}
}
}
As you can see I am using the Current.UmbracoHelper.GetDictionary() method which I know is a big "No No!". I would like to know if anyone has had experience with injecting the localisation service into a custom class?
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Umbraco.Core.Composing;
using Umbraco.Core.Services;
namespace CleanBlog.Core
{
public class UmbracoRequiredAttribute : RequiredAttribute, IClientValidatable
{
public UmbracoRequiredAttribute(ILocalizationService localizationService)
{
_localizationService = localizationService;
}
private readonly ILocalizationService _localizationService;
public string DictionaryKey { get; set; } = "Validation.Required";
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
//you would need a way to know the current language, but for now we have the default one
var defaultLanguageId = _localizationService.GetDefaultLanguageId();
//this gets the item from the dictionary by using the key and then returns the translation for the default language.
var ErrorMessage = _localizationService.GetDictionaryItemByKey(DictionaryKey).Translations.Where(x => x.LanguageId == defaultLanguageId).FirstOrDefault().Value;
yield return new ModelClientValidationRequiredRule(ErrorMessage);
}
}
}
Thanks for the reply, but it's a path I've been down already and I get the following issue:
Because we are injecting the ILocalizationService into the constructor it expects one to be passed in when I decorate Model properties 😅 Does anyone know a way around this? I've seen mentions of using Action Filters...but really hoping for another solution!
BTW loving the Umbraco 8 videos you've been creating, keep up the good work!
Advice on injecting a localisation service for a custom class
Hi All,
I am creating a very simple custom required attribute which I have got working but it currently involves an anti-patter that I am trying to replace with an injected localisation service.
As you can see I am using the Current.UmbracoHelper.GetDictionary() method which I know is a big "No No!". I would like to know if anyone has had experience with injecting the localisation service into a custom class?
Thanks,
Rob
Try something like this.
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; using Umbraco.Core.Composing; using Umbraco.Core.Services;
Cheers
Paul
Hi Paul!
Thanks for the reply, but it's a path I've been down already and I get the following issue:
Because we are injecting the ILocalizationService into the constructor it expects one to be passed in when I decorate Model properties 😅 Does anyone know a way around this? I've seen mentions of using Action Filters...but really hoping for another solution!
BTW loving the Umbraco 8 videos you've been creating, keep up the good work!
Cheers, Rob
is working on a reply...