1

I am trying to develop a DisplayName Attribute which has an interface for localization service, which is already registered at startup and working if injected in a constructor.

How can I get the localization service interface to be instantiated since I cant use a construction injection?

This is my code

public class MyDisplayNameAttribute : System.ComponentModel.DisplayNameAttribute
{
    private string _resourceValue = string.Empty;
    private ILocalizationService _localizationService;
    public MyDisplayNameAttribute(string resourceKey)
       : base(resourceKey)
    {
        ResourceKey = resourceKey;
    }

    public string ResourceKey { get; set; }

    public override string DisplayName
    {
        get
        {
            _resourceValue = _localizationService.GetLocaleString(ResourceKey);

            return _resourceValue;
        }
    }

    public string Name
    {
        get { return nameof(MyDisplayNameAttribute); }
    }
}

Thanks

5
  • Is there any reason you implement your own MyDisplayNameAttribute with localization? Why not use DataAnnotations localization? Commented Mar 6, 2019 at 7:26
  • @ArunPratap, I am implementing my own, because I want the resources coming from a database and users can easily change the display titles based on selected language. Commented Mar 6, 2019 at 8:18
  • You can't, at least not with the built in .net core DI container. I'd suggest that you roll your own IStringLocalizationFactory loading resources from SQL server instead, and use the built-in localization support of .net core such as DisplayAttribute. Commented Mar 6, 2019 at 8:42
  • @Michael: You don't need to implement your own attribute to get localization from a database. You need to implement IStringLocalizer/IStringLocalizerFactory. See this blog post: damienbod.com/2016/01/29/…, just use the proper mechanisms that ASP.NET Core offers you Commented Mar 6, 2019 at 10:57
  • Dependency Injection and attributes don't mix well. Write passive attributes instead. Commented May 18, 2019 at 7:10

2 Answers 2

2

I hope you could solve the problem, this is a very simple solution but as you knew it's an anti-pattern :

   public class LocalizedDisplayNameAttribute : DisplayNameAttribute
   {
        public LocalizedDisplayNameAttribute(string Name) : base(Name)
        {
        }
        public override string DisplayName
        {
            get
            {
                var _localizationService= new HttpContextAccessor().HttpContext.RequestServices.GetService<ILocalizationService>();
                return _localizationService.Get(base.DisplayNameValue).Result;
            }
        }
    }

I Hope it helps others at least ;)

Sign up to request clarification or add additional context in comments.

Comments

0

Dependency injection is working with invertion of control (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2). So framework controls your app and when instantiating your clsses injects dependencies requested via constructor.

Refer also here How to create a class without constructor parameter which has dependency injection

So I suspect that it is not possible to inject dependency without using constructor.

May be if you describe you intention there may be another good solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.