0

Let's assume following basic Project-Setup:

- Core
-- Attributes
--- CustomDisplayNameAttribute : DisplayNameAttribute
- UI

UI represents the MVC Web interface, the core implements all the domain business objects, including self written attributes like CustomDisplayNameAttribute. This attribute contains additional dependencies like a language resolver, which e.g. deals with fallback orders. Hibernate sessions would be another possible dependency.

In earlier projects, these attributes did a global request in order to get the resolver. This is IMO ugly and should be handled differently. Furthermore, Core should stay without HttpContext: since the language-resolver is required per request, it might end up in HttpContext Items Collection.

Now I am quite a beginner with Ninject, and I am not sure if it is the right tool in order to get such dependencies into something like a CustomDisplayNameAttribute?

In words it would be something like this:

  • If the attribute is created, populate the additional language resolver property with the language resolver from HttpContext Items Collection
  • If there is no HttpContext (e.g. testing, quartz jobs etc.), get it from somewhere else.

Thx for any inputs

Edit: Sample-Code

namespace Core.Attributes
{
    public class CustomDisplayNameAttribute : DisplayNameAttribute
    {

        private string textCode;

        /// <param name="textCode">According to this Text-Code, we will load
        /// and resolve the text.</param>
        public DeimosDisplayNameAttribute(string textCode)
        {
            this.textCode = textCode;
        }

        /// <summary>
        /// Load and resolve Text according to Text-Code
        /// </summary>
        public override string DisplayName
        {
            get 
            {
                // Load - Ooops: First global access 
                // --> How can it be injected with IoC?
                TextbausteinRepository repo = Root.GetTextBausteinRepository();
                var textItem = repo.GetText(textCode);
                // Resolve - Ooops: Second global access
                // --> How can it be injected with IoC?
                TextResolver resolver = Root.GetTextResolver();
                return resolver.resolve(textItem);
            }
        }
    }

}

Edit 2: In that context, it seems that there is no way around a global access, like a registry pattern or similar. UI would register the needed data in there, and the attributes would access it from there. We started to think about storing it in ThreadLocal<T>, but this seems not really save due to the fact that there is a possibility of thread-swapping during the life-cycle. So there seems no way around storing HttpContext in the registry layer. For more info about this subject, see [Cup(Of T)][1].

Steven
  • 166,672
  • 24
  • 332
  • 435
sl3dg3
  • 5,026
  • 12
  • 50
  • 74

1 Answers1

0

I don't think this is possible, because data attributes are not run-time dispatched like filters are. As such, there's no place to intercept the creation and inject what you're looking for.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291