views:

35

answers:

1

Hi

I'm trying to work out how to do the following:

[CustomAnnotation(thisVariableShouldBeInjected)]
public class MyClass
{
    // Normal class stuff
}

Now the data annotation is decorating a WCF service class, which itself has constructor injection going on. Ideally I'd like to inject the annotation with a value using the Ninject IOC container, however this question is container agnostic.

I just refactored the Annotation class from a static class and I'd hate to have to convert it back.

I'm happy to do property injection, (constructor injection isn't going to work with data annotations as they have to be constant expressions afaik). I just don't know how in this instance.

Any suggestions gladly welcomed!


In response to questions/comments:

In this particular instance, the WCF service is annotated with attributes, some of which perform functionality such as auditing service requests and statistics gathering. In these instances the Annotation classes are invoked directly by the WCF infrastructure and I cannot get access to them in the constructor phase.

I'm trying out reflection based property injection (as suggested). In this exact situation the fact that the annotation classes are global actually helps me, as I am attempting to inject repository instances - I only ever needed one anyway.

This may have to be the way I end up doing the injection. However it is much better in my book than the static classes with their static references to the repository.

+1  A: 

As attributes are compile time meta data on a class you cannot inject a value into them at runtime that would affect a particular instance.

You can use reflection to change a value (property) of an attribute at runtime but it would be global and not for any particular instance.

aqwert