views:

268

answers:

2

I need to know if there's any way (or another distinct approach) to an attribute knows something about what is being decorated for him. For example:

class Cat
{
    public Cat() { }

    [MyAttribute]
    public House House { get; set; }
}

Inside MyAttribute I must do some preprocessing with the house object...

class MyAttribute : Attribute
{
    public MyAttribute() 
    {
        var ob = // Discover the decorated property, do some changes and set it again
    }
}

I don't know if it's the better way, neither if it actually can be done,

+2  A: 

This is not how attributes work. They are just compile time metadata added to something. They don't accomplish anything by themselves. At runtime, code can use that metadata to do things.

UPDATE: Basically, as I understand, you are trying to accomplish two things. The first is to tell the repository not to load some properties. Attributes can be used for this purpose but the repository code should use reflection on the entity type and see what it shouldn't load in the first place. The second thing is that you want to have the property loaded as it's called for the first time. You need to check if it's already loaded or not on each call and load it the first time it's called. This can be achieved by manually inserting such a code or using something like PostSharp which post-processes code and can inject method calls automatically by looking at the attributes. Probably, this is what you asked for in the first place.

Mehrdad Afshari
So what would be the correct way here?
Alaor
Probably, you should mention your actual problem to get the best solution. More clearly, *what* are you trying to accomplish in the first place?
Mehrdad Afshari
Somekind of lazy loading with the database. I have a complex field in my objects and I don't wan't them to load together with the object itself, I can make a method but my goal is to do something extensible in the future, part of the repository code in my app. My repo actually auto load end set the objects properties that I ask for, I must grant a way to tell him not to do this to that big prop at first and wrap a call to that prop when it's needed and load it on demand. I'm losing my time?
Alaor
The first part is easy and is working already. I have a attribute that say to my code to not load'em. But Its a repo that abstract almost everything, I just do something like Repository<Cat> repo = new Repository<Cat>(); Cat c = repo.Load<Cat>("id", 50); It uses reflection to see the database fields, compare with the object and populate it, that's why I need somekind of wrap, I don't want to code every prop in my domain, neither the ones to come, I'd like to set a attribute - or something like - and my repo using reflection load it for me when time come. I'll get a look in PostSharp, thankz!
Alaor
+1  A: 

What you want is the Policy Injection application block in EntLib.

Developers can use the Policy Injection Application Block to specify crosscutting behavior of objects in terms of a set of policies. A policy is the combination of a series of handlers that execute when client code calls methods of the class and—with the exception of attribute-based policies—a series of matching rules that select the classes and class members (methods and properties) to which the application block attaches the handlers.

JP Alioto
I'm gonna ready the link! Thankz for the light!
Alaor