Active record (motif de conception)

Active record (motif de conception)

Active record (patron de conception)

En génie logiciel, le patron de conception (design pattern) active record (enregistrement actif en anglais) est une approche pour lire les données d'une base de données. Les attributs d'une table ou d'une vue sont encapsulés dans une classe. Ainsi l'objet, instance de la classe, est lié à un tuple de la base. Après l'instanciation d'un objet, un nouveau tuple est ajouté à la base au moment de l'enregistrement. Chaque objet récupère ses données depuis la base; quand un objet est mis à jour, le tuple auquel il est lié l'est aussi. La classe implémente des accesseurs pour chaque attribut.

Une des implémentations les plus utilisées est celle de la classe ActiveRecord de Ruby on Rails. Par exemple, s'il y a une table pièces définie par l'ensemble d'attributs {id, nom, prix}, et qu'il y a une classe Pièce, le code Ruby suivant :

a = Pièce.new
a.nom = "Pièce test"
a.prix = 123.45
a.save

va créer un nouveau tuple dans la base avec les valeurs correspondantes, et est exactement équivalent à la requête SQL suivante :

INSERT INTO pièces (nom, prix) VALUES ('Pièce test', 123.45);

Réciproquement, la classe peut être utilisée pour accéder à la base :

nompièce = "Pot catalytique"
b = Pièce.find(:first, :conditions ⇒ [ "nom = ?", nompièce ])

va créer un objet à partir du premier tuple de la base dont l'attribut nom est égal à la variable nompièce, et est exactement équivalent à la requête SQL suivante :

SELECT * FROM pièces WHERE nom = 'Pot catalytique' LIMIT 1;

Le code aurait même pu être réduit :

b = Pièce.find_by_name("Pot catalytique")

Liens externes

Ce document provient de « Active record (patron de conception) ».

Wikimedia Foundation. 2010.

Contenu soumis à la licence CC-BY-SA. Source : Article Active record (motif de conception) de Wikipédia en français (auteurs)

Игры ⚽ Поможем решить контрольную работу

Regardez d'autres dictionnaires:

  • CakePHP — Pour les articles homonymes, voir Cake. CakePHP …   Wikipédia en Français

  • CodeIgniter — Développeur EllisLab, Inc. Dernière version …   Wikipédia en Français

  • Europe, history of — Introduction       history of European peoples and cultures from prehistoric times to the present. Europe is a more ambiguous term than most geographic expressions. Its etymology is doubtful, as is the physical extent of the area it designates.… …   Universalium

  • Christianity — /kris chee an i tee/, n., pl. Christianities. 1. the Christian religion, including the Catholic, Protestant, and Eastern Orthodox churches. 2. Christian beliefs or practices; Christian quality or character: Christianity mixed with pagan elements; …   Universalium

  • literature — /lit euhr euh cheuhr, choor , li treuh /, n. 1. writings in which expression and form, in connection with ideas of permanent and universal interest, are characteristic or essential features, as poetry, novels, history, biography, and essays. 2.… …   Universalium

  • arts, East Asian — Introduction       music and visual and performing arts of China, Korea, and Japan. The literatures of these countries are covered in the articles Chinese literature, Korean literature, and Japanese literature.       Some studies of East Asia… …   Universalium

  • BIBLE — THE CANON, TEXT, AND EDITIONS canon general titles the canon the significance of the canon the process of canonization contents and titles of the books the tripartite canon …   Encyclopedia of Judaism

  • Judaism — /jooh dee iz euhm, day , deuh /, n. 1. the monotheistic religion of the Jews, having its ethical, ceremonial, and legal foundation in the precepts of the Old Testament and in the teachings and commentaries of the rabbis as found chiefly in the… …   Universalium

  • JERUSALEM — The entry is arranged according to the following outline: history name protohistory the bronze age david and first temple period second temple period the roman period byzantine jerusalem arab period crusader period mamluk period …   Encyclopedia of Judaism

  • biblical literature — Introduction       four bodies of written works: the Old Testament writings according to the Hebrew canon; intertestamental works, including the Old Testament Apocrypha; the New Testament writings; and the New Testament Apocrypha.       The Old… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”