9
What actions a ORM should supply?
What it should or should not provide to the developer, or what its role within a system?
9
What actions a ORM should supply?
What it should or should not provide to the developer, or what its role within a system?
18
Object-Relational Mapping (ORM) is a technique that allows you to query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the object-relational mapping technique, hence the phrase "an ORM".
A ORM library is a completely normal library written in the language of your choice that encapsulates the code needed to manipulate the data, so you don’t use SQL anymore; you interact directly with an object in the same language you are using.
For example, here is a completely imaginary case with a pseudo language:
You have a book class, which aims to get all the books of which the author is "Linus".
Manually, you would do something like this:
book_list = new List();
sql = "SELECT book FROM library WHERE author = 'Linus'";
data = query(sql); // I over simplify ...
while (row = data.next()){
book = new Book();
book.setAuthor(row.get('author');
book_list.add(book);
}
With an ORM library, it would look like this:
book_list = BookTable.query(author="Linus");
The mechanical part is performed automatically through the ORM library.
Using ORM saves a lot of time because:
Using a ORM library is more flexible because:
It fits into your natural coding form (it’s your language!).
It abstracts the DB system, so you can change it whenever you want.
The model is weakly attached to the rest of the application, so you can change it or use it anywhere else.
It allows you to use OOP as data inheritance without pain of head.
But ORM can be a pain:
You have to learn, and ORM libraries are not tools light;
You have to configure it. Same problem.
Performance is OK for usual queries, but an SQL master always do better with your own SQL for large projects.
It abstracts the DB. While it’s OK if you know what’s going on behind the scene, it’s a trap for new programmers who can record very greedy statements, like a heavy hit on a loop for.
"Translated from: What is an ORM and Where can I Learn more about it?
12
The tag description itself already gives enough information about what a ORM is.
The acronym means Object-Relational Mapping, mapping relational object in free translation.
It is a technique used to map between object-oriented systems and relational databases, where database tables are represented in classes and table records would be instances of these classes.
Example:
Table:
Pessoa
-----------------------------
Id Integer(10)
Nome Varchar(100)
DataNascimento DateTime
Class (pseudo-language):
class Pessoa
{
int Id;
string Nome;
DateTime DataNascimento;
}
Using a ORM, the programmer doesn’t have to bother writing the query’s for queries, inserts, etc. This is done through the programming language, the ORM exposes an API to handle these operations.
Example:
Ormless:
string sql = "SELECT * FROM PESSOAS";
SqlCommand command = new SqlCommand(sql, connection);
DataReader reader = command.Execute();
List<Pessoas> pessoas = new List<Pessoas>();
while(row = reader.Next())
{
pessoas.Add(new Pessoa
{
Id = Convert.ToInt(row["Id"]),
Nome = row["Nome"].ToString(),
DataNascimento = Convert.ToDate(row["DataNascimento"]);
};
}
With ORM:
List<Pessoas> pessoas = database.Pessoas.SelectAll().ToList();
Note that this is a simple, illustrative example. Each ORM has a different way of working (this is a way that I invented).
There are several ways to map a class with a table, it depends on the tool. For example, Hibernate (Java) allows mapping to be done using XML files, or annotations in the class (model). The Entity Framework (.NET) allows this mapping directly into the class (the class name turns the table name and the properties turn to the columns) or using attributes (the Dataannotations).
8
Strictly speaking the only function that a ORM must ter is the mapping of the data model found in the database (relational) to the model found in the application (object oriented).
The data source does not necessarily need to be a database, mainly it does not need to be relational, but this is the most common.
This means mapping the database tables with the application classes, not necessarily one to one (although it is the most common) translating the data types, since there is usually no direct relation. Know some programming language that has a type varchar
? No, right? So this is a function of the ORM.
In general what the ORM does is decrease the Boilerplate access to data in different models through an abstraction of the model. This is different from direct relational access where you see everything as tables. With the ORM the application accesses the data in a way less linked to the data source.
It is common for Orms to implement a repository and have their own language for access to data, but that is not mandatory. This often creates an abstraction that allows adaptation to any type of data source (depending on the flexibility of the ORM). In general there are operations to perform a CRUD, but again, this is extension of basic functionality.
It is not simple to do this correctly because of Object-relational_impedance_mismatch.
Of course the ORM can, and often does more than that. There are controversies one should. There are advantages and disadvantages in doing more. It is common to call these frameworks more "lightweight" (which only do the least a ORM should do) of micro ORM.
There’s a question on the subject.
There are detractors from the pattern. There’s even a well-known article on the subject. Many of these criticisms are more in the way more known Orms function than in the pattern itself. The mapping is necessary somehow, the problem is to exaggerate. A less partial view. Note that I am only the messenger, do not shoot me if you do not like what is written here.
Browser other questions tagged orm
You are not signed in. Login or sign up in order to post.
When you talk about implementing an ORM, you say develop one or utilize one?
– Randrade
With that, I think it’s very broad. The answer would have to teach you how to develop a whole Orm, and that can encompass a lot. I advise you to open a question and post what you already have, and ask the question upon the real doubt, in your case the hasMany.
– Randrade
If the doubt is different from what was asked, I would suggest to elaborate a separate question, or to supplement it with more details, but without changing the focus. Already have two extensive answers that run the risk of being invalidated because of it. As you already have a long site time, already know how it works.
– Bacco
There’s no way to answer that. Every ORM has its particularities. Some fit into Micro ORM, as is the case with Dapper. He for example does several things, but not the hasMany. However, many consider it as ORM, and it is created by SE and used on all websites of the network.
– Randrade