Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Using Attributes

Using Attributes

Scheduled Pinned Locked Moved The Lounge
questioncsharpdesignperformancetutorial
24 Posts 23 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Richard Blythe

    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.

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #14

    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

    R 1 Reply Last reply
    0
    • M Marc Clifton

      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

      R Offline
      R Offline
      Richard Blythe
      wrote on last edited by
      #15

      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.

      1 Reply Last reply
      0
      • R Richard Blythe

        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.

        D Offline
        D Offline
        dybs
        wrote on last edited by
        #16

        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-readable DescriptionAttribute 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. Dybs

        The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

        1 Reply Last reply
        0
        • R Richard Blythe

          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.

          D Offline
          D Offline
          Donsw
          wrote on last edited by
          #17

          Depends on the location. If I need the speed no, but there are many places where speed of the application is not a concern.

          cheers, Donsw My Recent Article : CDC - Change Data Capture

          1 Reply Last reply
          0
          • R Richard Blythe

            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.

            R Offline
            R Offline
            Remi BOURGAREL
            wrote on last edited by
            #18

            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.

            1 Reply Last reply
            0
            • R Richard Blythe

              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.

              J Offline
              J Offline
              Jacquers
              wrote on last edited by
              #19

              I've used them with PostSharp[^] which I found very useful.

              H 1 Reply Last reply
              0
              • J Jacquers

                I've used them with PostSharp[^] which I found very useful.

                H Offline
                H Offline
                HenryChilvers
                wrote on last edited by
                #20

                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

                1 Reply Last reply
                0
                • R Richard Blythe

                  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.

                  P Offline
                  P Offline
                  Patrick Fox
                  wrote on last edited by
                  #21

                  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.

                  1 Reply Last reply
                  0
                  • R Richard Blythe

                    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.

                    J Offline
                    J Offline
                    Jason Christian
                    wrote on last edited by
                    #22

                    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.

                    1 Reply Last reply
                    0
                    • R Richard Blythe

                      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.

                      F Offline
                      F Offline
                      frattaro
                      wrote on last edited by
                      #23

                      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.

                      1 Reply Last reply
                      0
                      • R Richard Blythe

                        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.

                        S Offline
                        S Offline
                        Sandesh M Patil
                        wrote on last edited by
                        #24

                        :) :) :) :) :) :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups