How would you transfer data between your data structures and databases?
-
I'm learning programming in school and I have this question that's bugging me about data structures and transferring the information stored inside them to databases. We're doing just small systems - registration for an event, point of sale for a local restaurant, sales and inventory, etc Let's say someone registers himself, should it be passed into a data structure and then into the database, or should it just be transferred directly to the database?
-
Answer:
At its most basic, what you need to develop to transfer data between data structures in memory and persistent data stores (files, DB, whatever) is a Data Access Layer or DAL. This is one of your "application tiers" in a properly-constructed N-tier application. Objects in this tier know how to create data structures from DB data, and conversely to convert a properly-populated data structure into DB data, so that objects generally lying outside this tier don't have to have this knowledge. There are several models for building a DAL tier. The most common in my experience is a centralized "front door" that can accept any request to read or write data and produce any needed data structure. This pattern is called the "Repository". You would make a call to this object along the lines of: int myObjectId = 1243; MyObject myObjectInstance = myRepository.RetrieveById<MyObject>(myObjectId); ... and then myObjectInstance would be populated with the data from the table representing MyObject records, with the identifying key that we specified. How that happens exactly, nobody outside the Repository really has to know; you could dynamically construct a SQL statement, use a Data Access Object that knows specifically how to retrieve MyObject instances, or you could do as Oded states and use a third-party library called an Object-Relational Mapper or ORM which abstracts away all the details of how the retrieval happens.
Daryll Santos at Programmers Visit the source
Other answers
The aim is to store data into the database. If the data structure isn't needed for your use case, then don't use it. In most apps I have worked on, data structure would be used only because I am working with code that expects (or needs) a data structure to be passed around between different business logic methods before the data can be saved to the database.
rk2010
Related Q & A:
- How To Get Online Data Mining Work?Best solution by theatlantic.com
- how to extract video and audio meta data?Best solution by Stack Overflow
- How to restore windows 7 without losing data?Best solution by Super User
- How to Write Form Data to XML?Best solution by Stack Overflow
- Is it possible to transfer data wirelessly between your PS3 and your computer?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.