0

I'm developing a multi-language web site. My solution for displaying multilanguage texts, based on following LocalizedDisplayAttribute class. I would like to inject some dependency to this class.But the class is instantiated by .Net Framework. How container inject dependency without using service locator?

Here is my code block

[AttributeUsage(AttributeTargets.All)]
public class LocalizedDisplayAttribute : DisplayNameAttribute
{
    public MultilanguageKeys Key { get; set; }
    public IMultilanguageManager MultilanguageManager { get; set; }              

    public LocalizedDisplayAttribute(IMultilanguageManager multilanguageManager)
    {
        this.MultilanguageManager = multilanguageManager;
    }

    public override string DisplayName
    {
        get
        {
         // Run some business   
         return GetCustomAttribute(Key);
        }
    }      

    private string GetCustomAttribute(MultilanguageKeys key)
    {
        int multiLanguageId = 3;            

        MultiLanguageDictionaryInfo multiLanguageDictionary = MultilanguageManager.DictionaryList()
                                                            .Where(t => t.Key == key.ToString())
                                                            .Where(t => t.MultilanguageId == multiLanguageId)
                                                            .FirstOrDefault();           

        return multiLanguageDictionary.Value;

    }

}
doganak
  • 798
  • 14
  • 31

2 Answers2

1

AS your mention, Attribute class is created by .NET framework, so I believe it's impossible to do dependency injection automatically, you need to inject this manually.

Use this solution to inject properties to constructed object:

Windsor castle Injecting properties of constructed object

Community
  • 1
  • 1
cuongle
  • 74,024
  • 28
  • 151
  • 206
0

You have to override the ControllerActionInvoker class and the InvokeActionMethodWithFilters method where you can use windsor to inject the dependencies on the instantiated objects

Edit: Realized this is for mvc, not sure how to go about it in a webforms environment

Daniel
  • 622
  • 4
  • 6