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. Problems with generic list data structure

Problems with generic list data structure

Scheduled Pinned Locked Moved C#
databaseoraclehelp
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.
  • S Offline
    S Offline
    sam L 0
    wrote on last edited by
    #1

    I have two factory classes that both return a generic list of a data object class. Currently the code is using a switch statement to call a particular factory depending on some input. Code below the switch statement below depends on getting the list of data objects from different sources, but processing them the same. List<Product> products = null; switch (i) { case 1: products = ProductFactory.GetProducts(); break; case 2: products = ItemFactory.GetItems(); break; default: break; } The problem is that if I want to add more factories from different sources... I would have to rewrite the code for the current builder class, i.e. add more statements to the switch. Since an unknown number of factory calls could be made to get the data objects from different sources (one factory returns from Oracle, one from SQL one from SAP, etc) the code needs to accommodate any new factory sources without having to recode. Is there an easy way to accomplish this, please show me some sample code if there's a way to do so. Thanks

    ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

    L D N 3 Replies Last reply
    0
    • S sam L 0

      I have two factory classes that both return a generic list of a data object class. Currently the code is using a switch statement to call a particular factory depending on some input. Code below the switch statement below depends on getting the list of data objects from different sources, but processing them the same. List<Product> products = null; switch (i) { case 1: products = ProductFactory.GetProducts(); break; case 2: products = ItemFactory.GetItems(); break; default: break; } The problem is that if I want to add more factories from different sources... I would have to rewrite the code for the current builder class, i.e. add more statements to the switch. Since an unknown number of factory calls could be made to get the data objects from different sources (one factory returns from Oracle, one from SQL one from SAP, etc) the code needs to accommodate any new factory sources without having to recode. Is there an easy way to accomplish this, please show me some sample code if there's a way to do so. Thanks

      ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      I guess you need a FactoryFactory. :) Like you 'lookup' them by index now, just place it in a list (array), and lookup the index.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      S 1 Reply Last reply
      0
      • S sam L 0

        I have two factory classes that both return a generic list of a data object class. Currently the code is using a switch statement to call a particular factory depending on some input. Code below the switch statement below depends on getting the list of data objects from different sources, but processing them the same. List<Product> products = null; switch (i) { case 1: products = ProductFactory.GetProducts(); break; case 2: products = ItemFactory.GetItems(); break; default: break; } The problem is that if I want to add more factories from different sources... I would have to rewrite the code for the current builder class, i.e. add more statements to the switch. Since an unknown number of factory calls could be made to get the data objects from different sources (one factory returns from Oracle, one from SQL one from SAP, etc) the code needs to accommodate any new factory sources without having to recode. Is there an easy way to accomplish this, please show me some sample code if there's a way to do so. Thanks

        ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

        D Offline
        D Offline
        dibya_2003
        wrote on last edited by
        #3

        You can add your object into hash table. Then you can use has code value in the switch statement.

        L 1 Reply Last reply
        0
        • L leppie

          I guess you need a FactoryFactory. :) Like you 'lookup' them by index now, just place it in a list (array), and lookup the index.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          S Offline
          S Offline
          sam L 0
          wrote on last edited by
          #4

          Hi there, I'm not sure if I followed what you meant here? Could you perhaps show me some sample code? I'd really appreciate it!

          ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

          L 1 Reply Last reply
          0
          • D dibya_2003

            You can add your object into hash table. Then you can use has code value in the switch statement.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            dibya_2003 wrote:

            Then you can use has code value in the switch statement.

            That is exactly what you should not do! You simply look it up by the key, without the need for a switch.

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            1 Reply Last reply
            0
            • S sam L 0

              Hi there, I'm not sure if I followed what you meant here? Could you perhaps show me some sample code? I'd really appreciate it!

              ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              sam L wrote:

              Hi there, I'm not sure if I followed what you meant here? Could you perhaps show me some sample code? I'd really appreciate it!

              Something like:

              Factory[] factories = { new AFactory(), new BFactory(), etc };

              int index = ??;

              Factory f = factories[index];

              List l = f.GetList();

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              S 1 Reply Last reply
              0
              • L leppie

                sam L wrote:

                Hi there, I'm not sure if I followed what you meant here? Could you perhaps show me some sample code? I'd really appreciate it!

                Something like:

                Factory[] factories = { new AFactory(), new BFactory(), etc };

                int index = ??;

                Factory f = factories[index];

                List l = f.GetList();

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                S Offline
                S Offline
                sam L 0
                wrote on last edited by
                #7

                Thanks a lot for the sample code...I guess the indexer here would be numbered based, so I can initialize the int index = 0??? But how is it going to get incremented for the subsequent factory values (AFactory, Bfactory, etc)????? Should there be a loop??? Can you please clarify?

                ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

                L 1 Reply Last reply
                0
                • S sam L 0

                  I have two factory classes that both return a generic list of a data object class. Currently the code is using a switch statement to call a particular factory depending on some input. Code below the switch statement below depends on getting the list of data objects from different sources, but processing them the same. List<Product> products = null; switch (i) { case 1: products = ProductFactory.GetProducts(); break; case 2: products = ItemFactory.GetItems(); break; default: break; } The problem is that if I want to add more factories from different sources... I would have to rewrite the code for the current builder class, i.e. add more statements to the switch. Since an unknown number of factory calls could be made to get the data objects from different sources (one factory returns from Oracle, one from SQL one from SAP, etc) the code needs to accommodate any new factory sources without having to recode. Is there an easy way to accomplish this, please show me some sample code if there's a way to do so. Thanks

                  ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  I am not sure I got your question. How about something like this ?

                  class Program
                  {
                      static void Main(string\[\] args) {
                          List<Product> sapProducts = ProductFactory.GetProducts(new SAPFactory());
                          List<Product> oracleProducts = ProductFactory.GetProducts(new OracleFactory());
                      }
                  }
                  abstract class BaseFactory
                  {
                      public abstract List<Product> GetProduct();
                  }
                  
                  class SAPFactory : BaseFactory
                  {
                      public override List<Product> GetProduct() {
                          return new List<Product>();
                      }
                  }
                  
                  class OracleFactory : BaseFactory
                  {
                      public override List<Product> GetProduct() {
                          return new List<Product>();
                      }
                  }
                  
                  static class ProductFactory
                  {
                      public static List<Product> GetProducts(BaseFactory factory) {
                          return factory.GetProduct();
                      }
                  }
                  

                  This allows you to add new factories easily without modifying any code. You can keep the factory names in a XML file and load it at runtime using reflection which gives you full flexibility on adding new factories. Is this what you are looking for ? Correct me if I got you wrong.

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                  1 Reply Last reply
                  0
                  • S sam L 0

                    Thanks a lot for the sample code...I guess the indexer here would be numbered based, so I can initialize the int index = 0??? But how is it going to get incremented for the subsequent factory values (AFactory, Bfactory, etc)????? Should there be a loop??? Can you please clarify?

                    ----------------------------- If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place. -Nora Roberts

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    It's your code, you should know what it does. If you are not familiar with the pattern you are using, please read up on it. Your 'indexer' (key really) can be based on anything if you use hashtables instead. Lastly, why do you want to loop? Do you want to combine the data maybe? Then do that after you got the data. Solve one problem at a time.

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    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