Concepts - O/R Mapping

Preface

O/R Mapping, or Object Relational Mapping, is the general term for the concept of creating mappings between tables / views and their fields, and OO classes and their fields to be able to represent a table or view row in an OO program via a class. O/R Mapping is done using entities and their attributes (which are physically available through table or view definitions) and by creating a class for each entity, mapping each field in that entity class onto a field in a table or view. The management logic necessary to read an entity's data from the database into an instance of its entity class and back, together with the entity definitions, is called an O/R Mapper framework. Via this framework a developer can manipulate data in the database, using classes and their methods.

LLBLGen Pro's O/R Mapping

O/R Mapping is not new, for example in the Java world it is a common technique to work with data in a database. For developers using Microsoft platforms and .NET in particular, O/R Mapping is rather new and often mis-understood. Most Microsoft oriented developers think in tiers, tables and raw SQL statements and look for code generators for these tiers, completely neglecting the term O/R Mapping and the tool utilizing this technique. As with all techniques used by developers on a variety of platforms, O/R Mapping has a list of definitions which are not always the same. To be clear how O/R Mapping is utilized in the LLBLGen Pro system, this section defines O/R Mapping for LLBLGen Pro. It by no means claims to be the 'correct' version of the definition of an O/R Mapping framework, but it is a usable definition for LLBLGen Pro users.
Mapping with entities
Because LLBLGen Pro uses the relational model oriented definition of an entity, mapping of classes on tables / views is simply done by creating an entity definition for every table / view definition selected by the user, and an entity field definition for every table / view field in these selected tables / views. This concept is extended by offering the ability to specify that an entity is a subtype and/or a supertype, which results in an entity being mapped onto more than one table / view (its own table / view and all the tables / views its supertype (and that entity's supertype etc.) are mapped on). For more information about this concept which is called 'Inheritance', please see Entity inheritance and relational models. LLBLGen Pro allows you to map more than one entity definition onto the same table / view, and / or partly map an entity definition onto its table / view.

Entity classes generated using these entity definitions contain all logic necessary to read an entity's data from the database (persistent storage) or send back modified data to the persistent storage. You can see the instance of an entity class as the in-memory representation of a table / view row's data, and the methods exposed by the instance of the class as the tools to work with these data. Derived entities, or subtypes, are generated as derived classes and simply inherit the supertype's fields, logic and methods, overriding properties/methods where appropriate. A derived entity type therefore contains the supertype's fields together with its own fields. See for more details Entity inheritance and relational models.

LLBLGen Pro doesn't use an in-memory object cache, which means that the generated code and the runtime library targeted by the generated code, are designed to work with entities directly in the persistent storage (database). In-memory equivalents of data in the persistent storage (i.e. the entity objects you instantiate when you instantiate an entity class and fill it with its data from the persistent storage), will be kept in sync, if possible. However, code using the entity classes shouldn't rely on that, because .NET doesn't have a way to know when a database row has changed and thus when an entity object in-memory should be re-synced with the database. LLBLGen Pro defines a context which can be used to create unique objects per entity row in the database. Often it is not necessary to create unique objects per entity data however in the situations you need it, the context class is a way to make sure an entity is loaded in just one entity object for that particular context. See Using the context for more detailed information about how to use this in your code.

The entity classes generated give you the ability to work with data in the persistent storage in a typed, friendly way, however they don't contain logic often seen in Java O/R mappers, like an application wide object cache. The reason for this is that .NET doesn't have a system like Enterprise Java Beans (EJB) which does the persisting for you and keeps track of any changes, even in a distributed application spanning more than one machine.

This is not a limitation however, since working with entities as they are defined by LLBLGen Pro is a matter of data manipulation in the persistent storage and to do that with close ties to that persistent storage, multiple users, using multiple application or threads, targeting the same database, can work with the same data without problems, because the data manipulations are designed to be targeted directly to the persistent storage. Read more about this topic in Stateless persistence.
An entity's lifecycle
To get more familiar with the terms, the way LLBLGen Pro handles things and the way terms relate to each other, the following list is defined to illustrate when what is done and why. As an example, the entity definition 'Customer' is used which targets the table 'Customer'. A new 'customer' is added, with the primary key 'SOLDES', which is the CustomerID. For simplicity, SelfServicing is used to illustrate the lifecycle. For Adapter, it is similar, the developer uses different methods/classes to achieve the same goal(s).

LLBLGen Pro v2.6 documentation. ©2002-2008 Solutions Design