Problems with generic list data structure
-
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
-
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
-
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
You can add your object into hash table. Then you can use has code value in the switch statement.
-
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)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
-
You can add your object into hash table. Then you can use has code value in the switch statement.
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) -
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
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) -
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)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
-
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
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
-
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
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)