generics collection question

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

    generics collection question

    Hello,

    I'm hoping to do something using Generics, but I'm not sure it's possible.
    Let's say I want to have a bunch of business objects and a data access class
    cooresponding to each business object. For each business object, I would
    have something like below:

    public class Apple
    {
    //apple stuff
    }

    public class AppleDAL
    {
    public void UpdateApple( int appleID, Apple apple );
    public Apple GetApple( int appleID );
    public AppleList ListAllApples() ;
    }

    Currently, the AppleList collection class is a subclass of CollectionBase.
    Everytime I add a new business object, I need to write a new DAL class and a
    new CollectionBase subclass to act as the collection for that business
    object:

    /// <summary>
    /// Type-safe collection for Apple objects
    /// </summary>
    [Serializable]
    public class AppleList : System.Collecti ons.CollectionB ase
    {
    public AppleList {}

    public Apple this[ int index ]
    {
    get{ return( Apple List[index] ); }
    set{ List[index] = value; }
    }

    public int Add( Apple item ) { return( List.Add( item ) ); }

    public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }

    public void Insert( int index, Apple item ) { List.Insert( index,
    item ); }

    public void Remove( Apple item ) { List.Remove( item ); }
    }


    I am wondering if there is a way to use Generics to avoid writing the
    CollectionBase subclass for every new business object. I'm not sure how I
    would create a concrete type-safe collection for something new. So for an
    new business object, orange:

    public class Orange
    {
    //orange stuff
    }

    public class OrangeDAL
    {
    public void UpdateOrange( int orangeID, Orange orange );
    public Orange GetOrange( int orangeID );
    public OrangeList ListAllOranges( );
    }

    Can I create an OrangeList class using Generics? Something like?

    //this doesn't work
    public class OrangeList <Orange>{}

    -Corey



    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  • Kevin Spencer

    #2
    Re: generics collection question

    Heck yes, Corey! It's quite simple:

    using System.Collecti ons.ObjectModel ;

    public class AppleList : Collection<Appl e{}

    That's all you need. It already has the methods that you had to wire up
    before, like Add, Remove, IndexOf, etc.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Chicken Salad Surgery

    Who is Mighty Abbott? A twin-turret scalawag.

    "cwineman" <me@home.comwro te in message
    news:1154707893 _14959@sp6iad.s uperfeed.net...
    Hello,
    >
    I'm hoping to do something using Generics, but I'm not sure it's possible.
    Let's say I want to have a bunch of business objects and a data access
    class cooresponding to each business object. For each business object, I
    would have something like below:
    >
    public class Apple
    {
    //apple stuff
    }
    >
    public class AppleDAL
    {
    public void UpdateApple( int appleID, Apple apple );
    public Apple GetApple( int appleID );
    public AppleList ListAllApples() ;
    }
    >
    Currently, the AppleList collection class is a subclass of CollectionBase.
    Everytime I add a new business object, I need to write a new DAL class and
    a new CollectionBase subclass to act as the collection for that business
    object:
    >
    /// <summary>
    /// Type-safe collection for Apple objects
    /// </summary>
    [Serializable]
    public class AppleList : System.Collecti ons.CollectionB ase
    {
    public AppleList {}
    >
    public Apple this[ int index ]
    {
    get{ return( Apple List[index] ); }
    set{ List[index] = value; }
    }
    >
    public int Add( Apple item ) { return( List.Add( item ) ); }
    >
    public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }
    >
    public void Insert( int index, Apple item ) { List.Insert( index,
    item ); }
    >
    public void Remove( Apple item ) { List.Remove( item ); }
    }
    >
    >
    I am wondering if there is a way to use Generics to avoid writing the
    CollectionBase subclass for every new business object. I'm not sure how I
    would create a concrete type-safe collection for something new. So for an
    new business object, orange:
    >
    public class Orange
    {
    //orange stuff
    }
    >
    public class OrangeDAL
    {
    public void UpdateOrange( int orangeID, Orange orange );
    public Orange GetOrange( int orangeID );
    public OrangeList ListAllOranges( );
    }
    >
    Can I create an OrangeList class using Generics? Something like?
    >
    //this doesn't work
    public class OrangeList <Orange>{}
    >
    -Corey
    >
    >
    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
    News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
    Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption
    =----

    Comment

    • wfairl@gmail.com

      #3
      Re: generics collection question

      Sure, define a base DataCollection and DataRecord object. Then create
      your base DAL entity class and collection and then you can create
      custom classes on top of those (I say this because I'm guessing your
      DAL is probably auto-generated).

      Something like this for the DAL:

      public abstract class OrangeCollectio nBase<T: DataCollection< Twhere
      T : OrangeBase, new()
      {
      }

      Then your derived collection class would look like this:

      public class Oranges : OrangeCollectio nBase<Orange>
      {
      }

      cwineman wrote:
      Hello,
      >
      I'm hoping to do something using Generics, but I'm not sure it's possible.
      Let's say I want to have a bunch of business objects and a data access class
      cooresponding to each business object. For each business object, I would
      have something like below:
      >
      public class Apple
      {
      //apple stuff
      }
      >
      public class AppleDAL
      {
      public void UpdateApple( int appleID, Apple apple );
      public Apple GetApple( int appleID );
      public AppleList ListAllApples() ;
      }
      >
      Currently, the AppleList collection class is a subclass of CollectionBase.
      Everytime I add a new business object, I need to write a new DAL class and a
      new CollectionBase subclass to act as the collection for that business
      object:
      >
      /// <summary>
      /// Type-safe collection for Apple objects
      /// </summary>
      [Serializable]
      public class AppleList : System.Collecti ons.CollectionB ase
      {
      public AppleList {}
      >
      public Apple this[ int index ]
      {
      get{ return( Apple List[index] ); }
      set{ List[index] = value; }
      }
      >
      public int Add( Apple item ) { return( List.Add( item ) ); }
      >
      public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }
      >
      public void Insert( int index, Apple item ) { List.Insert( index,
      item ); }
      >
      public void Remove( Apple item ) { List.Remove( item ); }
      }
      >
      >
      I am wondering if there is a way to use Generics to avoid writing the
      CollectionBase subclass for every new business object. I'm not sure how I
      would create a concrete type-safe collection for something new. So for an
      new business object, orange:
      >
      public class Orange
      {
      //orange stuff
      }
      >
      public class OrangeDAL
      {
      public void UpdateOrange( int orangeID, Orange orange );
      public Orange GetOrange( int orangeID );
      public OrangeList ListAllOranges( );
      }
      >
      Can I create an OrangeList class using Generics? Something like?
      >
      //this doesn't work
      public class OrangeList <Orange>{}
      >
      -Corey
      >
      >
      >
      ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

      Comment

      • cwineman

        #4
        Re: generics collection question

        Excellent.

        I was fairly certain that it could be easily done, but I couldn't figure out
        the syntax and I wasn't able to find an example showing what I wanted.

        Thanks, guys.

        "Kevin Spencer" <uce@ftc.govwro te in message
        news:eQ%23CSd%2 3tGHA.5032@TK2M SFTNGP04.phx.gb l...
        Heck yes, Corey! It's quite simple:
        >
        using System.Collecti ons.ObjectModel ;
        >
        public class AppleList : Collection<Appl e{}
        >
        That's all you need. It already has the methods that you had to wire up
        before, like Add, Remove, IndexOf, etc.
        >
        --
        HTH,
        >
        Kevin Spencer
        Microsoft MVP
        Chicken Salad Surgery
        >
        Who is Mighty Abbott? A twin-turret scalawag.


        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        • Chris Dunaway

          #5
          Re: generics collection question

          cwineman wrote:
          I was fairly certain that it could be easily done, but I couldn't figure out
          the syntax and I wasn't able to find an example showing what I wanted.
          If you don't want to write your own class, you can just use the built
          in List<class:

          public List<OrangeList AllOranges();

          Comment

          • Joanna Carter [TeamB]

            #6
            Re: generics collection question

            <wfairl@gmail.c oma écrit dans le message de news:
            1154711200.2036 02.160100@p79g2 00...legr oups.com...

            | Sure, define a base DataCollection and DataRecord object. Then create
            | your base DAL entity class and collection and then you can create
            | custom classes on top of those (I say this because I'm guessing your
            | DAL is probably auto-generated).

            This really isn't necessary, you can use List<Tor Collection<Tor any
            other generic class without any further derivation. A DAL should be quite
            capable of producing or iterating through such lists using reflection.

            public static class DAL
            {
            public static List<TRetrieveL ist<T>()
            {
            ...
            }

            ...
            }

            void test()
            {
            List<Customercu stList = DAL.RetrieveLis t<Customer>();

            ...
            }

            Joanna

            --
            Joanna Carter [TeamB]
            Consultant Software Engineer


            Comment

            • Chris Mullins

              #7
              Re: generics collection question

              "Joanna Carter [TeamB]" <joanna@not.for .spamwrote in message
              This really isn't necessary, you can use List<Tor Collection<Tor any
              other generic class without any further derivation. A DAL should be quite
              capable of producing or iterating through such lists using reflection.
              public static class DAL
              {
              public static List<TRetrieveL ist<T>()
              {
              ...
              }
              Just to be nitpicky, that code wouldn't pass FxCop validation (or Team
              System Code Analysis).

              For reason's I'm not quite clear on, you're not supposed to return a
              List<T>. They recommend using the Generic classes found in the
              System.Collecti ons.ObjectModel namespace instead.

              You can find more on this here:



              --
              Chris Mullins, MCSD.NET, MCPD:Enterprise



              Comment

              • Joanna Carter [TeamB]

                #8
                Re: generics collection question

                "Chris Mullins" <cmullins@yahoo .coma écrit dans le message de news:
                OBPgziL7GHA.200 @TK2MSFTNGP05.p hx.gbl...

                | Just to be nitpicky, that code wouldn't pass FxCop validation (or Team
                | System Code Analysis).
                |
                | For reason's I'm not quite clear on, you're not supposed to return a
                | List<T>. They recommend using the Generic classes found in the
                | System.Collecti ons.ObjectModel namespace instead.

                Hmm, Do you know if IList<Tis equally frowned upon ?

                | You can find more on this here:
                | http://msdn2.microsoft.com/en-us/library/ms182142.aspx

                Interesting but, as you say, the reasons really aren't all that clear. What
                If I want to return a type that cannot be inherited, is that really so wrong
                ?

                I personally use my own ReadOnlyList<Tc lass because ReadOnlyCollect ion<T>
                raises exceptions on all the IList<Timplemen ting methods. To my mind, that
                is a poor design choice; what is the point of supplying an interface that
                provides most of its functionality by raising exceptions ? You could hardly
                call that "implementation " :-)

                Joanna

                --
                Joanna Carter [TeamB]
                Consultant Software Engineer


                Comment

                • Chris Mullins

                  #9
                  Re: generics collection question

                  "Joanna Carter [TeamB]" wrote
                  "Chris Mullins" <cmullins@yahoo .coma écrit dans le message de news:
                  OBPgziL7GHA.200 @TK2MSFTNGP05.p hx.gbl...
                  >
                  | Just to be nitpicky, that code wouldn't pass FxCop validation (or Team
                  | System Code Analysis).
                  |
                  | For reason's I'm not quite clear on, you're not supposed to return a
                  | List<T>. They recommend using the Generic classes found in the
                  | System.Collecti ons.ObjectModel namespace instead.
                  >
                  Hmm, Do you know if IList<Tis equally frowned upon ?
                  Code Analysis (via Team System) is perfectly happy with IList<T>. Seems to
                  be if one fails, they both should fail.
                  >
                  | You can find more on this here:
                  | http://msdn2.microsoft.com/en-us/library/ms182142.aspx
                  >
                  Interesting but, as you say, the reasons really aren't all that clear.
                  What
                  If I want to return a type that cannot be inherited, is that really so
                  wrong
                  ?
                  I agree. I recognized the issue in your code because I do it so often
                  myself. We've been trying to be diligant about using FxCop more, and this is
                  one of the things that keeps biting me.
                  I personally use my own ReadOnlyList<Tc lass because
                  ReadOnlyCollect ion<T>
                  raises exceptions on all the IList<Timplemen ting methods. To my mind,
                  that
                  is a poor design choice; what is the point of supplying an interface that
                  provides most of its functionality by raising exceptions ? You could
                  hardly
                  call that "implementation " :-)
                  I agree here as well.

                  I would much rather have a true RealyOnly Interface. I want compile-time
                  errors, not run-time errors.

                  --
                  Chris Mullins, MCSD.NET, MCPD:Enterprise



                  Comment

                  • Arne Vajhøj

                    #10
                    Re: generics collection question

                    Chris Mullins wrote:
                    "Joanna Carter [TeamB]" wrote
                    >"Chris Mullins" <cmullins@yahoo .coma écrit dans le message de news:
                    >OBPgziL7GHA.200 @TK2MSFTNGP05.p hx.gbl...
                    >| For reason's I'm not quite clear on, you're not supposed to return a
                    >| List<T>. They recommend using the Generic classes found in the
                    >| System.Collecti ons.ObjectModel namespace instead.
                    >>
                    >Hmm, Do you know if IList<Tis equally frowned upon ?
                    >
                    Code Analysis (via Team System) is perfectly happy with IList<T>. Seems to
                    be if one fails, they both should fail.
                    Maybe it has nothing to do with generics and it just wants
                    to enforce the good habit of returning interfaces instead
                    of concrete classes.

                    Arne

                    Comment

                    • Chris Mullins

                      #11
                      Re: generics collection question

                      "Arne Vajhøj" <arne@vajhoej.d kwrote
                      Chris Mullins wrote:
                      >"Joanna Carter [TeamB]" wrote
                      >>"Chris Mullins" <cmullins@yahoo .comwrote:
                      >>OBPgziL7GHA.200 @TK2MSFTNGP05.p hx.gbl...
                      >>| For reason's I'm not quite clear on, you're not supposed to return a
                      >>| List<T>. They recommend using the Generic classes found in the
                      >>| System.Collecti ons.ObjectModel namespace instead.
                      >>>
                      >>Hmm, Do you know if IList<Tis equally frowned upon ?
                      >>
                      >Code Analysis (via Team System) is perfectly happy with IList<T>. Seems
                      >to be if one fails, they both should fail.
                      >
                      Maybe it has nothing to do with generics and it just wants
                      to enforce the good habit of returning interfaces instead
                      of concrete classes.
                      If only it were that simple.

                      The classes they recommend be returned, in the
                      System.Collecti ons.ObjectModel namespace, are not interfaces. They're
                      standard classes.

                      --
                      Chris Mullins, MCSD.NET, MCPD:Enterprise



                      Comment

                      • Arne Vajhøj

                        #12
                        Re: generics collection question

                        Chris Mullins wrote:
                        "Arne Vajhøj" <arne@vajhoej.d kwrote
                        >Chris Mullins wrote:
                        >>"Joanna Carter [TeamB]" wrote
                        >>>"Chris Mullins" <cmullins@yahoo .comwrote:
                        >>>OBPgziL7GHA.200 @TK2MSFTNGP05.p hx.gbl...
                        >>>| For reason's I'm not quite clear on, you're not supposed to return a
                        >>>| List<T>. They recommend using the Generic classes found in the
                        >>>| System.Collecti ons.ObjectModel namespace instead.
                        >>>>
                        >>>Hmm, Do you know if IList<Tis equally frowned upon ?
                        >>Code Analysis (via Team System) is perfectly happy with IList<T>. Seems
                        >>to be if one fails, they both should fail.
                        >Maybe it has nothing to do with generics and it just wants
                        >to enforce the good habit of returning interfaces instead
                        >of concrete classes.
                        >
                        If only it were that simple.
                        >
                        The classes they recommend be returned, in the
                        System.Collecti ons.ObjectModel namespace, are not interfaces. They're
                        standard classes.
                        Expert-led tech and creative training to master your craft. Unlimited and online.



                        indicates that it is because System.Collecti ons.ObjectModel .Collection
                        is intended to be extended while List is not.

                        No - it is not that obvious to me.

                        Arne

                        Comment

                        • Chris Mullins

                          #13
                          Re: generics collection question

                          "Arne Vajhøj" <arne@vajhoej.d kwrote:
                          Chris Mullins wrote:
                          >>
                          >The classes they recommend be returned, in the
                          >System.Collect ions.ObjectMode l namespace, are not interfaces. They're
                          >standard classes.
                          >
                          Expert-led tech and creative training to master your craft. Unlimited and online.


                          >
                          indicates that it is because System.Collecti ons.ObjectModel .Collection
                          is intended to be extended while List is not.
                          That's the answer I saw as well - but it sure seems like a pretty weak
                          answer to me. I still don't understand their motivation, as there are a
                          number of cases where an List<Tis EXACTLY what I would want to return.

                          --
                          Chris Mullins, MCSD.NET, MCPD:Enterprise



                          Comment

                          • Arne Vajhøj

                            #14
                            Re: generics collection question

                            Chris Mullins wrote:
                            "Arne Vajhøj" <arne@vajhoej.d kwrote:
                            >Chris Mullins wrote:
                            >>The classes they recommend be returned, in the
                            >>System.Collec tions.ObjectMod el namespace, are not interfaces. They're
                            >>standard classes.
                            >http://pluralsight.com/blogs/craig/a.../21/15770.aspx
                            >http://www.gotdotnet.com/team/fxcop/...ericLists.html
                            >>
                            >indicates that it is because System.Collecti ons.ObjectModel .Collection
                            >is intended to be extended while List is not.
                            >
                            That's the answer I saw as well - but it sure seems like a pretty weak
                            answer to me. I still don't understand their motivation, as there are a
                            number of cases where an List<Tis EXACTLY what I would want to return.
                            Me too.

                            But ...

                            Arne

                            Comment

                            • Joanna Carter [TeamB]

                              #15
                              Re: generics collection question

                              "Arne Vajhøj" <arne@vajhoej.d ka écrit dans le message de news:
                              XsXWg.20907$2g4 .9052@dukeread0 9...

                              | http://pluralsight.com/blogs/craig/a.../21/15770.aspx
                              |

                              |
                              | indicates that it is because System.Collecti ons.ObjectModel .Collection
                              | is intended to be extended while List is not.

                              To quote a comment on the first blog :
                              Tomas, probably because they want you to derive from Collection<T>
                              to expose your strongly-typed collection classes in public APIs rather
                              than using List<T>. A
                              I thought the purpose of generic types was that you didn't have to derive
                              from them to provide strongly typed collections ! ? Huh ?


                              To quote the example in the first blog :

                              // This class satisfies the rule.
                              public class GenericIntegerC ollection:
                              CollectionBase, IEnumerable<int >
                              {
                              public int Add(int value)
                              {
                              return InnerList.Add(v alue);
                              }

                              // Other method overrides using Int32.

                              public new IEnumerator<int GetEnumerator()
                              {
                              foreach (int data in InnerList)
                              {
                              yield return data;
                              }
                              }
                              }

                              If I were designing a generic collection class, why would I use
                              CollectionBase to derive from ? This forces me to hold all my items in the
                              InnerList, which is an ArrayList !!! Surely the point of a generic
                              collection is that the items are stored as their native type, not
                              cast/boxed/unboxed on the way into/out of the storage ?

                              Methinks this train of thought has the smell of a .NET 1.1 programmer who
                              doesn't understand generics, enforcing their ideas on fxCop.

                              Joanna

                              --
                              Joanna Carter [TeamB]
                              Consultant Software Engineer


                              Comment

                              Working...