Alternative Ways To Implement "Events"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smithers

    #1

    Alternative Ways To Implement "Events"

    Just looking to compile a list of "all the ways to implement events".

    I'm NOT looking to get into the merits or mechanics of each in this
    thread... just want to identify them all - good, bad, and ugly.

    Here's what I have so far
    1. Implement via the 'event' keyword (with associated delegate, etc)

    2. Expose a private delegate via a public property (really a "roll yer own"
    version of the 'event' keyword method).

    3. Old-school callbacks in which one object (objA) calls another (objB),
    passing a reference of itself to the called object (objB then contains a
    reference to objA). The called object can then call methods on the calling
    object (i.e., objB calls methods of objA).

    4 ... ???

    I'd appreciate any additional ways to implement "events" - including total
    hacks.

    I'm looking to grasp finally grasp the entire landscape of events and
    callback mechanisms that are available in .NET (even if they are independent
    of .NET, like plain ole' callbacks).

    Thanks.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Alternative Ways To Implement "Events&qu ot;

    Smithers,

    Technically, you have to use the "event" keyword when you want to have
    an event on your type. This is baked into the metadata (and shown through
    Reflection) as an event. All the methods, besides #1, are not events,
    technically.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Smithers" <A@B.comwrote in message
    news:uhi2AOs2HH A.4584@TK2MSFTN GP03.phx.gbl...
    Just looking to compile a list of "all the ways to implement events".
    >
    I'm NOT looking to get into the merits or mechanics of each in this
    thread... just want to identify them all - good, bad, and ugly.
    >
    Here's what I have so far
    1. Implement via the 'event' keyword (with associated delegate, etc)
    >
    2. Expose a private delegate via a public property (really a "roll yer
    own" version of the 'event' keyword method).
    >
    3. Old-school callbacks in which one object (objA) calls another (objB),
    passing a reference of itself to the called object (objB then contains a
    reference to objA). The called object can then call methods on the calling
    object (i.e., objB calls methods of objA).
    >
    4 ... ???
    >
    I'd appreciate any additional ways to implement "events" - including total
    hacks.
    >
    I'm looking to grasp finally grasp the entire landscape of events and
    callback mechanisms that are available in .NET (even if they are
    independent of .NET, like plain ole' callbacks).
    >
    Thanks.
    >

    Comment

    • Samuel R. Neff

      #3
      Re: Alternative Ways To Implement &quot;Events&qu ot;


      In some places .NET uses attributes to subscribe event handlers. Look
      at OnSerializingAt tribute and related attributes (there's 4 of them).

      Java (at least as of 1.4, not sure if it changed), implements events
      using interfaces. Instead of declaring an event like in C# each event
      type has an interface and there are methods for subsribing to an
      "event" by providing an interface implementation. Then inner classes,
      especially anonymous inner classes, are heavily used as "event"
      handlers. I probably have some details wrong but I'm pretty sure on
      the basic principle.

      DOM (JavaScript, ActionScript) implements events using name events and
      function references (delegates). I've actually seen people implement
      custom events in .NET this way, but in that situation we turned down
      the contract because of that odd requirement.


      And to be perfectly clear, I'm not advocating or condoning the use of
      any of these methods over the standard way to create events in .NET..
      just you asked for everything. :-)

      HTH,

      Sam


      ------------------------------------------------------------
      We're hiring! B-Line Medical is seeking .NET
      Developers for exciting positions in medical product
      development in MD/DC. Work with a variety of technologies
      in a relaxed team environment. See ads on Dice.com.



      On Thu, 9 Aug 2007 13:26:09 -0700, "Smithers" <A@B.comwrote :
      >Just looking to compile a list of "all the ways to implement events".
      >
      >I'm NOT looking to get into the merits or mechanics of each in this
      >thread... just want to identify them all - good, bad, and ugly.
      >
      >Here's what I have so far
      >1. Implement via the 'event' keyword (with associated delegate, etc)
      >
      >2. Expose a private delegate via a public property (really a "roll yer own"
      >version of the 'event' keyword method).
      >
      >3. Old-school callbacks in which one object (objA) calls another (objB),
      >passing a reference of itself to the called object (objB then contains a
      >reference to objA). The called object can then call methods on the calling
      >object (i.e., objB calls methods of objA).
      >
      >4 ... ???
      >
      >I'd appreciate any additional ways to implement "events" - including total
      >hacks.
      >
      >I'm looking to grasp finally grasp the entire landscape of events and
      >callback mechanisms that are available in .NET (even if they are independent
      >of .NET, like plain ole' callbacks).
      >
      >Thanks.
      >

      Comment

      • Smithers

        #4
        Re: Alternative Ways To Implement &quot;Events&qu ot;

        <snip>

        << While loosely, it is an event, the serialization engine uses reflection
        to call the method >>>

        <snip>

        Nicholas,

        Please note that the answer that Samuel provided is *exactly* the sort of
        response I was looking for in my OP.

        Please note that the OP subject has the word, events, in quotes ("events")
        which indicates that I'm looking not only for events, but also anything that
        can be roughly construed as an event. I went on in the body of the OP to
        continue the use of quotes, and even explicitly requiested "total hacks"
        (meaning not prim and proper events).

        Please do not discourage folks from responding simply because they aren't
        providing some way to implement events according to some narrow technically
        specific definition. If thats' what I wanted, the OP wouldn't have described
        my intent as going after the "entire landscape" of events and callback
        mechanisms.... even those that are independent of .NET."

        -S


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Alternative Ways To Implement &quot;Events&qu ot;

          Samuel,

          The OnSerializingAt tribute does not subscribe the method to an event.
          While loosely, it is an event, the serialization engine uses reflection to
          call the method.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Samuel R. Neff" <samuelneff@nom ail.comwrote in message
          news:spvmb3t8gn cj7hds3517l5a43 uc6aecd47@4ax.c om...
          >
          In some places .NET uses attributes to subscribe event handlers. Look
          at OnSerializingAt tribute and related attributes (there's 4 of them).
          >
          Java (at least as of 1.4, not sure if it changed), implements events
          using interfaces. Instead of declaring an event like in C# each event
          type has an interface and there are methods for subsribing to an
          "event" by providing an interface implementation. Then inner classes,
          especially anonymous inner classes, are heavily used as "event"
          handlers. I probably have some details wrong but I'm pretty sure on
          the basic principle.
          >
          DOM (JavaScript, ActionScript) implements events using name events and
          function references (delegates). I've actually seen people implement
          custom events in .NET this way, but in that situation we turned down
          the contract because of that odd requirement.
          >
          >
          And to be perfectly clear, I'm not advocating or condoning the use of
          any of these methods over the standard way to create events in .NET..
          just you asked for everything. :-)
          >
          HTH,
          >
          Sam
          >
          >
          ------------------------------------------------------------
          We're hiring! B-Line Medical is seeking .NET
          Developers for exciting positions in medical product
          development in MD/DC. Work with a variety of technologies
          in a relaxed team environment. See ads on Dice.com.
          >
          >
          >
          On Thu, 9 Aug 2007 13:26:09 -0700, "Smithers" <A@B.comwrote :
          >
          >>Just looking to compile a list of "all the ways to implement events".
          >>
          >>I'm NOT looking to get into the merits or mechanics of each in this
          >>thread... just want to identify them all - good, bad, and ugly.
          >>
          >>Here's what I have so far
          >>1. Implement via the 'event' keyword (with associated delegate, etc)
          >>
          >>2. Expose a private delegate via a public property (really a "roll yer
          >>own"
          >>version of the 'event' keyword method).
          >>
          >>3. Old-school callbacks in which one object (objA) calls another (objB),
          >>passing a reference of itself to the called object (objB then contains a
          >>reference to objA). The called object can then call methods on the calling
          >>object (i.e., objB calls methods of objA).
          >>
          >>4 ... ???
          >>
          >>I'd appreciate any additional ways to implement "events" - including total
          >>hacks.
          >>
          >>I'm looking to grasp finally grasp the entire landscape of events and
          >>callback mechanisms that are available in .NET (even if they are
          >>independent
          >>of .NET, like plain ole' callbacks).
          >>
          >>Thanks.
          >>
          >

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Alternative Ways To Implement &quot;Events&qu ot;

            I'm not trying to discourage people from answering, but I don't want to
            spread the wrong impression about what an event is in .NET. It has a very
            concrete definition (reflected by the metadata reflected on the type, and
            reflected through the EventInfo type in the System.Reflecti on namespace).

            To be frank, the question that you asked has a vast number of answers.
            You are basically looking for anything that can signal your code in response
            to an action. The number of mechanisms for this is pretty huge.

            Some are more common than others, granted, and I feel you have hit on
            all of the common ones, I believe, but there are many more to be had
            (depending on the scenario and context).

            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m


            "Smithers" <A@B.comwrote in message
            news:ORcUv$s2HH A.5316@TK2MSFTN GP04.phx.gbl...
            <snip>
            >
            << While loosely, it is an event, the serialization engine uses reflection
            to call the method >>>
            >
            <snip>
            >
            Nicholas,
            >
            Please note that the answer that Samuel provided is *exactly* the sort of
            response I was looking for in my OP.
            >
            Please note that the OP subject has the word, events, in quotes ("events")
            which indicates that I'm looking not only for events, but also anything
            that can be roughly construed as an event. I went on in the body of the OP
            to continue the use of quotes, and even explicitly requiested "total
            hacks" (meaning not prim and proper events).
            >
            Please do not discourage folks from responding simply because they aren't
            providing some way to implement events according to some narrow
            technically specific definition. If thats' what I wanted, the OP wouldn't
            have described my intent as going after the "entire landscape" of events
            and callback mechanisms.... even those that are independent of .NET."
            >
            -S
            >

            Comment

            • Samuel R. Neff

              #7
              Re: Alternative Ways To Implement &quot;Events&qu ot;


              And since I guess you missed this part of my original post, I'll
              repeat..

              "
              And to be perfectly clear, I'm not advocating or condoning the use of
              any of these methods over the standard way to create events in .NET..
              just you asked for everything. :-)
              "

              While this may seem like a silly question, silly questions come up a
              lot (especially in consulting contracts, which I'm glad we no longer
              have to deal with) and it's always good to be knowledgeable about the
              subject and know lots of ways to do things, even if you'd never
              actually use them 'cause there is an accepted best practice (and of
              course best practices do change over time, for example events are
              different in plain .NET and WPF--although WPF events are based and a
              wrapper of .NET events).

              btw, Smithers, that's another one to add to your list. WPF supports
              tunneling and bubbling of events. I'm not the most qualified to
              explain it, but it's a very nice extension to .NET events.

              Sam

              ------------------------------------------------------------
              We're hiring! B-Line Medical is seeking .NET
              Developers for exciting positions in medical product
              development in MD/DC. Work with a variety of technologies
              in a relaxed team environment. See ads on Dice.com.



              On Thu, 9 Aug 2007 21:40:58 -0400, "Nicholas Paldino [.NET/C# MVP]"
              <mvp@spam.guard .caspershouse.c omwrote:
              I'm not trying to discourage people from answering, but I don't want to
              >spread the wrong impression about what an event is in .NET. It has a very
              >concrete definition (reflected by the metadata reflected on the type, and
              >reflected through the EventInfo type in the System.Reflecti on namespace).
              >
              To be frank, the question that you asked has a vast number of answers.
              >You are basically looking for anything that can signal your code in response
              >to an action. The number of mechanisms for this is pretty huge.
              >
              Some are more common than others, granted, and I feel you have hit on
              >all of the common ones, I believe, but there are many more to be had
              >(depending on the scenario and context).

              Comment

              Working...