I have 4 tables in the DB, each one has Id and Name, but they represent different things.
for each “thing” i have a different class, they all inherit from “thing”.
i have 4 functions :
List<thing1> getAllThings1();
List<thing2> getAllThings2();
List<thing3> getAllThings3();
List<thing4> getAllThings4();
each function reads from different table and creates the needed list.
Because i want to avoid code duplication i wanted to make a utility function that receives the table name (as string) and the type (thing1, thing2 … etc), and returns List<t>.
unfortunately this is impossible (without reflection) :
Create list of variable type
My current solution is that i have a function that returns a List, i call her in each “getAllThings#” and then manually convert each “thing” in the list to the proper thing by using ConvertAll and passing to him some converter.
I don’t like this solution, it feels wrong because i create a list and create a new one. very inefficient.
is there a better way to do it ?
thanks
Why not use generics?
You will be able to call it
you could also have a Dictionary for storing the table name for each type, so you don’t have to provide the table name to the method.