What are the functions of an ORM? [closed]

8

What actions should an ORM provide?

What should it or should not it provide the developer, or what role does it play within a system?

    
asked by anonymous 05.07.2016 / 18:56

3 answers

15

Introduction

Object-Relational Mapping (ORM) is a technique for querying and manipulating 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."

An 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 do not 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 class book, which you want to get all the books 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.

Pros and cons

Using ORM saves a lot of time because:

  • DRY: You write your data model in one place, and it's easier to maintain, and reuse the code.
  • A lot of things are done automatically, from bank manipulation of data for I18N.
  • It compels us to write the MVC code, which, in the end, makes its code a little cleaner.
  • You do not have to write malformed SQL (most Web programmers really spoil everything because SQL is handled as a "sub" language, when in reality it is a very powerful and complex).
  • Sanitizing; using prepared statements or transactions are so easy such as calling a method.

Using an ORM library is more flexible because:

  • It fits into its natural coding form (it's your language!).

  • It abstracts the DB system, so you can change it whenever you want.

  • The template is weakly linked 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 from head.

But ORM can be a pain:

  • You have to learn, and ORM libraries are not tools lightweight;

  • You have to configure it. Same problem.

  • Performance is OK for usual queries, but a 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 scenes, it's a trap for new programmers who can very greedy statements, such as a heavy hit on a loop for.

"Translated from: What is an ORM and where can I learn more about it?

    
05.07.2016 / 19:17
9

The tag description itself already gives enough information about what an ORM is.

What is it?

The acronym stands for Object-Relational Mapping , relational object mapping in free translation.

It is a technique used to map between object-oriented systems and relational databases, where the database tables are represented in classes and the records of the tables 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;
}

Use

Using an ORM, the programmer does not have to worry about writing the query's for queries, inserts, etc. This is done through the programming language, ORM exposes an API to handle these operations.

Example:

No ORM:

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 and illustrative example. Each ORM has a different way of working (this is a form I invented).

Mapping

There are several ways to map a class to a table, so it depends on the tool. For example, Hibernate (Java) allows mapping to be done using XML files, or annotations in the (model) class. The Entity Framework (.NET) allows this mapping directly into the class (the name of the class turns the table name and properties see the columns) or using attributes (the DataAnnotations).

    
05.07.2016 / 19:18
7

Strictly speaking the only function that a ORM should have is the mapping of the data model found in the (relational) database to the model found in the application (object oriented).

The data source does not necessarily have to be a database, it does not necessarily have to be relational, but this is the most common.

This means mapping database tables to application classes, not necessarily one-to-one (though this is the most common) translating data types, since there is usually no direct relationship. Do you know of any programming language that has a varchar type? Of course not? So this is a function of ORM.

In general what ORM does is to decrease the boilerplate of accessing data in different models through an abstraction of the model. This is different from direct relational access where you can see everything as tables. With ORM the application accesses data in a way that is less tied to the data source.

It is common for ORMs to implement a repository and have their own language for data access, but this is not mandatory . This usually creates an abstraction that allows adaptation to any type of data source (depending on the flexibility of the ORM). In general there are operations for performing a CRUD, but again, this is an extension of the basic functionality.

It is not easy to do this correctly because of the Object-relational_impedance_mismatch .

Of course the ORM can, and often, do more than that. There is controversy if it should. There are advantages and disadvantages in doing more. It is common to call these more "light" frameworks (which only do the least that an ORM should do) of micro ORM.

There is a question about it .

There are detractors of the standard . There's even a well-known article on the subject . Many of these criticisms are more in the way well-known ORMs work than in the standard itself. Mapping is necessary in some way, the problem is overdoing it. A Less Partial View . Note that I am only the messenger, do not shoot me if you do not like what is written here.

    
05.07.2016 / 20:53