How would you transfer data between your data structures and databases?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.