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.
Richard Blythe wrote:
I have rarely used attributes because they are obtained by reflection. (Performance hit) I have always design my apps not to depend on reflection.
The performance hit isn't as bad as it used to be (post .net 3.0). A lot of the new .net 4.0 stuff is reflected (and some of the 3.5), so they sorted the performance out before doing this. IMO code clarity wins out over performance, unless there is a specific need for for the performance to be good (e.g. bulk processing, real-time processing) etc. The other posters have given good reasons for attributes, WCF wouldn't work and it is good for the entity framework too.
ragnaroknrol The Internet is For Porn[^]
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners. -
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.
Sure, the performance isn't great, but proper use of attributes can make for some VERY clean and maintainable code. Improper use, well... There are plenty of ways to screw up a design, and you really don't need attributes if you want to make spaghetti code :) I minimize the performance hit by just reading once and caching... The reflection isn't a big deal unless you're doing it repeatedly. Want an example? Well, I have a set of data objects that act as a sort of bridge between multiple external data sources... Each one has different naming conventions, and there's a good deal of overlap... So instead of writing detailed converters for each source, I have stuff like this:
[Source(Sources.Database, "TABLE1.SHORT_NAME", NullIfEmpty=true)]
[Source(Sources.OtherThing, "ItemName")]
public string Name { get; internal set; }(The names have been changed to protect the innocent) I have a "SourcePathMap" class, which, given a "Sources" value, goes through all classes in a certain namespace that are a subset of "Record", and builds several hashes that translate field names to property setters. Then each source has a class or classes to use those maps to translate to and from the external API. So basically, instead of having separate files to translate each of these, I sacrifice a little performance to be able to look at a data object and say "Well, this field comes from X in source Y, or from A in source B" and so on. Easy to read, easy to maintain.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
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 assume you mean custom attributes. As with my enum utilities[^], I use them often, but I try to read them once and cache them.
-
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 prefer using C++. C++ doesn't have attributes or reflection. Problem solved. (For my C# code, I do use attributes and reflection very heavily in a test program, but rarely elsewhere. Due to nature of what I do, I rarely need to serialize anything so I don't need attributes for that. I endorse your approach. Even if reflection is faster, actually designing your code with performance in mind instead of using every damn feature of C# in a knee jerk fashion is a Very Good Thing and way to uncommon in my experience.)
-
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.
For web services or Win32 API calls from C# environment. In general the attributes confuses me.
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
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 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've only used attributes for three things: 1. To tell the .NET serializer what to do 2. To tell the .NET Form Designer what do to 3. To tell my Interacx application, at the server, what plugin methods are associated with what views and CRUD operations, which is all figured out at startup. [soapbox] I think attributes are pretty much evil. I've seen them used to couple class names and properties with database tables and fields, which in my opinion is mixing declarative information, better represented externally, in the imperative language code base. Yuck. So when using attributes, I always ask myself, am I applying an attribute that is actually completely decoupled from the imperative code and whose values may vary independently of the code, or vice versa? If so, then attributes are an incorrect pattern to apply. Yes, it may be simpler, but I prefer to do the extra work up front rather than be royally screwed later. And it really is amazing how a little defensive design up front can create very robust applications. [/soapbox] Marc
-
I've only used attributes for three things: 1. To tell the .NET serializer what to do 2. To tell the .NET Form Designer what do to 3. To tell my Interacx application, at the server, what plugin methods are associated with what views and CRUD operations, which is all figured out at startup. [soapbox] I think attributes are pretty much evil. I've seen them used to couple class names and properties with database tables and fields, which in my opinion is mixing declarative information, better represented externally, in the imperative language code base. Yuck. So when using attributes, I always ask myself, am I applying an attribute that is actually completely decoupled from the imperative code and whose values may vary independently of the code, or vice versa? If so, then attributes are an incorrect pattern to apply. Yes, it may be simpler, but I prefer to do the extra work up front rather than be royally screwed later. And it really is amazing how a little defensive design up front can create very robust applications. [/soapbox] Marc
Applause... Great speech Marc. :-D All joking aside, I completely agree with your assesment of attributes. I don't think my lack of attributes comes from a lack of knowlege. I just haven't had a lot of projects that required them.
The mind is like a parachute. It doesn’t work unless it’s open.
-
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.
The only attribute I tend to use is the
DescriptionAttribute
on an enum (like PIEBALDconsult's enum utils). In particular, I had to display a "meaningful" error message for about 30 different error codes, and it was convenient to define the codes as an enum with a human-readableDescriptionAttribute
I could display. I've also used this when I need to populate a ComboBox with the values of an enum. Other than that and some occasional designer attributes (Category
,Behavior
, etc. for the Property Page), I typically don't need to use attributes at all. DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
-
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 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 attribute a lot with monorail to create custom data binding method (for instance I want to create my object using a constructor with parameters). But I think it's not a good idea to use it like in ActiveRecord, your mapping code is supposed to be apart from your class code.
-
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.
-
We're using PostSharp[^] too. I find it nice to be able to add one line of (attribute) code on top of a method, and have performance counters captured for that method. Open up PerfMon and see how may times my method is being hit. Attributes are a good way to abstract out the "non-method specific" functionality of a method. (I.e. If your method is called GetUser(), it's nice to be able to put performance tracing, error handling, entry/exit validation, etc., all without having to "spaghetti up" the code!) As far as reflection/performance, the good thing about PostSharp[^] is that the overhead is at compile time, not runtime!
-------------- Henry Manager, Software Engineering and Professional Angry Monkey Slayer
-
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.
:) :) :) :) :) :)