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. General Programming
  3. C#
  4. Exposing a queue as a public property

Exposing a queue as a public property

Scheduled Pinned Locked Moved C#
data-structuresquestion
9 Posts 4 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.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    Is anybody aware of any guidelines, issued by Microsoft or any other parties, about exposing a Queue or Queue<T> as a public property on an object? I recall some old guidelines about exposing a generic collection, but I can't find any recent information about that, either.

    P A B 3 Replies Last reply
    0
    • J JoeRip

      Is anybody aware of any guidelines, issued by Microsoft or any other parties, about exposing a Queue or Queue<T> as a public property on an object? I recall some old guidelines about exposing a generic collection, but I can't find any recent information about that, either.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Yeah, it's best not to unless you really want other classes to have access. For one thing, it exposes implementation which makes altering the implementation difficult later. Another problem would be if one class is iterating (foreaching) the collection when another is trying to alter it. What are you trying to accomplish? Think in terms of interface. Will AddItem and/or GetItem methods suffice?

      1 Reply Last reply
      0
      • J JoeRip

        Is anybody aware of any guidelines, issued by Microsoft or any other parties, about exposing a Queue or Queue<T> as a public property on an object? I recall some old guidelines about exposing a generic collection, but I can't find any recent information about that, either.

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        I have not come across any such guidelines. Its best advised not to use public for any property, unless you really have to.

        Build your own survey - http://www.factile.net

        1 Reply Last reply
        0
        • J JoeRip

          Is anybody aware of any guidelines, issued by Microsoft or any other parties, about exposing a Queue or Queue<T> as a public property on an object? I recall some old guidelines about exposing a generic collection, but I can't find any recent information about that, either.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          Exposing any collection as a public property means that you lose control over the collection, because the calling class can push, pop and inspect items in the collection. It also potentially causes synchronisation issues if you use that collection within your own code and expect it to be unchanging (in particular, if you enumerate over it). I won't say 'don't do it', because it's often extremely convenient to do so, at least with a list where you want to provide access and you don't want to write a bunch of pointless IList wrapper methods to protect it. (With a queue, chances are all you want to allow the caller to do is add to it, so in that case I would write a wrapper method so I could use something other than a queue later.) But if you do expose a collection, make sure you put a note of caution about how a caller should use it in the documentation, and remember that you're relying on the end user being responsible. If you do choose to expose a collection, it's generally better to expose it as the relevant interface instead of the concrete class, since the Framework provides both. That allows you to change your underlying data structure in future, and expose a shim that supports the interface and maps to whatever you now use. None of this has anything to do with generics, by the way. It is exposing collections to the outside that you should be wary of.

          J 1 Reply Last reply
          0
          • B BobJanova

            Exposing any collection as a public property means that you lose control over the collection, because the calling class can push, pop and inspect items in the collection. It also potentially causes synchronisation issues if you use that collection within your own code and expect it to be unchanging (in particular, if you enumerate over it). I won't say 'don't do it', because it's often extremely convenient to do so, at least with a list where you want to provide access and you don't want to write a bunch of pointless IList wrapper methods to protect it. (With a queue, chances are all you want to allow the caller to do is add to it, so in that case I would write a wrapper method so I could use something other than a queue later.) But if you do expose a collection, make sure you put a note of caution about how a caller should use it in the documentation, and remember that you're relying on the end user being responsible. If you do choose to expose a collection, it's generally better to expose it as the relevant interface instead of the concrete class, since the Framework provides both. That allows you to change your underlying data structure in future, and expose a shim that supports the interface and maps to whatever you now use. None of this has anything to do with generics, by the way. It is exposing collections to the outside that you should be wary of.

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            I'm exposing a recorded history of events (changes to a database), in the order that they occured. I expect to update the queue periodically. Original order of events is important. I expect the user to periodically consume this information, in order, and then come back for more. So I was using a queue as it enables them to dequeue at will, while maintaining the original order, and keeping track of which events they have already noted/processed. The user really only needs to dequeue (ie, remove one item at a time, and be able to tell that they have consumed all the items currently available).

            B P 2 Replies Last reply
            0
            • J JoeRip

              I'm exposing a recorded history of events (changes to a database), in the order that they occured. I expect to update the queue periodically. Original order of events is important. I expect the user to periodically consume this information, in order, and then come back for more. So I was using a queue as it enables them to dequeue at will, while maintaining the original order, and keeping track of which events they have already noted/processed. The user really only needs to dequeue (ie, remove one item at a time, and be able to tell that they have consumed all the items currently available).

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              If the user tries to dequeue as you are trying to enqueue, I think odd things can happen. It may be better to instead expose an event for when a new event should be posted, and have the user catch that event and add the item to a local queue, which it can dequeue when convenient.

              1 Reply Last reply
              0
              • J JoeRip

                I'm exposing a recorded history of events (changes to a database), in the order that they occured. I expect to update the queue periodically. Original order of events is important. I expect the user to periodically consume this information, in order, and then come back for more. So I was using a queue as it enables them to dequeue at will, while maintaining the original order, and keeping track of which events they have already noted/processed. The user really only needs to dequeue (ie, remove one item at a time, and be able to tell that they have consumed all the items currently available).

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Consider an event for that. Among other things it allows multiple listeners.

                J 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Consider an event for that. Among other things it allows multiple listeners.

                  J Offline
                  J Offline
                  JoeRip
                  wrote on last edited by
                  #8

                  Heh. Actually, this code was written to prevent the user from having to deal with events. I'm wrapping a COM object which does raise events, but has complexities and issues that are problematic. This code maps an event raiser to something a user can poll. If those are the primary concerns, I'll stick with this model for now. My stress tests should tell me how much collision I'm seeing during my internal enqueue. Thanks everybody!

                  P 1 Reply Last reply
                  0
                  • J JoeRip

                    Heh. Actually, this code was written to prevent the user from having to deal with events. I'm wrapping a COM object which does raise events, but has complexities and issues that are problematic. This code maps an event raiser to something a user can poll. If those are the primary concerns, I'll stick with this model for now. My stress tests should tell me how much collision I'm seeing during my internal enqueue. Thanks everybody!

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Then maybe have Enqueue and Dequeue methods.

                    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