Using Attributes
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
I use attributes a lot, I think they are amazing. They allow you to differentiate objects with metadata. This allows me to write reflection heavy code that checks properties for their attributes to decide what to do with them. I can often get away with decorating some properties with attributes as opposed to going in and writing code for certain object's properties. I can introduce a new object and it will behave the way I want without having to change any business logic. Performance should always be a concern, but only when it becomes obvious. Avoiding attributes because their performance is slower than just writing code is premature optimization. You should only reconsider use of attributes when it becomes apparent that the performance is not adequate, this applies to all situations where performance is a concern. In my experience, use of attributes leads to increased productivity. To give an example, imagine you are writing a plugin based application where certain plugin objects depend on others to be loaded first. The host application is dumb, it just loads plugins, it doesn't know anything about them. You can create a dependency attribute so that the plugin objects could tell the host that it depends on this other plugin being loaded first. The host could then sort the plugins based on dependencies before initializing them. If the host doesn't find a plugin, it could show a message that plugin X requires plugin Y, which wasn't found. Or you could blindly load plugin X and get some confusing error message like a null reference exception.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
If you avoid them only because there is a "performance hit" then you are missing a very useful tool. To avoid them because of a performance hit that may or may not matter in the context of application is a mistake - use your brain and evaluate the situation to determine if the performance matters - and as other suggested you can cache the results of the attribute lookup or engage other techniques to mitigate the performance hit. If you are using attributes to do things in the UI for instance, the performance hit can be less than a human's ability to respond to stimulus anyway - so will never be noticed by a user.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
Richard Blythe wrote:
reflection. (Performance hit)
I'm writing the third version of my own CMS. I don't use attributes, but I do use reflection to load third-party dll's on every page load, in case they want to run some analytics code, or run extra authentication/authorization stuff. I have yet to complete it, but I don't expect the performance hit to be bad. We'll see I guess.
-
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection. Here's the question: Are attributes an important part your applications? If so, give us a (good) example of your attribute implementation. (.Net or custom)
The mind is like a parachute. It doesn’t work unless it’s open.
:) :) :) :) :) :)