samedi 25 avril 2015

Combine Class/Table with middletable value

I'm trying to learn to structure my (datamodel)classes to reflect the database structure.

Coming from web-development I haven't bothered much in the past with creating objects that reflected my database content. I've always been used to working with it as raw array data.

The structure of the data in the system I'm trying to develop is based around translations. After a bit of research I decided to split my tables into "static" and "translation" data storage. Below is an example of the database structure using sqlite:

Table: Tutorial
(PK)Id : Integer
PositionX : Integer
PositionY: Integer

Table: Tutorial_Translation
(FK)Tutorial_Id : Integer
(FK)Language_Id : Integer
Title : Text
Text : Text

Table: Language
(PK)Id : Integer
Title : Text
Country : Text

Now the idea, as you may have guessed, is that I store the text for the tutorials in the middletable "Tutorial_Translation". When I wish to display a given tutorial, I'd join the "Tutorial_Translation" values "Title" and "Text" to the "Tutorial" table based on the "Tutorial ID" and the "Language ID" that has been set beforehand.

So far so good.. Now the "problem" I occour here, Is that I have to structure my system classes around these database tables, so that I work with it flawlessly using the SQLite framework.

I would really like to get some feedback of the idea to the class structure that I'm thinking about using:

Class: Tutorial
Id : Int
Position : Point
_positionX : Int
_positionY : Int
TitleText : TutorialTranslation

Class: TutorialTranslation
Tutorial_Id : Int
Language_Id : Int
Title : String
Text : String

If I were to use a structure like this, I could easily instantiate an object of Tutorial and call it's title or text, through the TutorialTranslation object.

My questions are:

  1. Is this a good (enough) structure of the system classes to reflect the database design?
  2. If so, would the best way to create an object of TutorialTranslation be in the constructor of Tutorial? -> Is there a way to get Sqlite to do this for me?

Aucun commentaire:

Enregistrer un commentaire