I have an complex object on my web server – lets say the object, for instance, represents a survey.
So this survey object has many questions and each question can have many possible answer options – basically it is a hierachical object.
Let say further, that in the database, they tables are roughly of a similar structure (ie. a survey table which link to a question table which links to a answer options table).
If we want to populate the object from SQL is the only way to do this to:
- Select the survey details from the database
- Select all the questions that appear in that survey from the database
- Select all the answer options that are available to the questions in the survey from the database
- Iterate over the results on the web server and populate the Survey object accordingly.
(This is bespoke code so the various entity frameworks are not an option).
Is there are better way to do this? It is correct that I am iterating through all these results and populating my object accordingly or is there an easier way?
It is not entirely clear on what abstraction level do you access the database. ADO.NET SqlCommands?
Are all OR/M frameworks out of question? Then you won’t get complex “objects” back from the data tier automatically. This is one of the major jobs of an OR/M tool that it can load the data into an object or object-hierarchy based on a mapping. If you don’t use a framework for this, you have to iterate the “raw SQL rows” yourself and copy them into an object structure.