Locked. This question và its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Someone suggested I use an ORM for a project that I"m designing, but I"m having trouble finding information on what it is or how it works.

Can anyone give me a brief explanation of what an ORM is và how it works và how I should get started using one?


*

*

Introduction

Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring lớn a library that implements the Object-Relational Mapping technique, hence the phrase "an ORM".

Bạn đang xem: Object relational mapping là gì

Bạn đã xem: Object relational mapping là gì

An ORM library is a completely ordinary library written in your language of 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"re using.

For example, here is a completely imaginary case with a pseudo language:

You have a book class, you want to retrieve all the books of which the tác giả is "Linus". Manually, you would vì chưng something lượt thích that:

book_list = new List();sql = "SELECT book FROM library WHERE tác giả = "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 taken care of automatically via the ORM library.

Pros & Cons

Using ORM saves a lot of time because:

You don"t have khổng lồ write poorly-formed SQL (most website programmers really suck at it, because SQL is treated like a "sub" language, when in reality it"s a very powerful và complex one).Sanitizing; using prepared statements or transactions are as easy as calling a method.

Xem thêm: Hướng Dẫn Móc Giầy Len Cho Bé Sơ Sinh P1, Hướng Dẫn Móc Giày Cho Bé Sơ Sinh P1

Using an ORM library is more flexible because:

It fits in your natural way of coding (it"s your language!).It abstracts the DB system, so you can change it whenever you want.The mã sản phẩm is weakly bound to the rest of the application, so you can change it or use it anywhere else.It lets you use OOP goodness lượt thích data inheritance without a headache.

But ORM can be a pain:

You have to learn it, and ORM libraries are not lightweight tools;You have lớn set it up. Same problem.Performance is OK for usual queries, but a SQL master will always vày better with his own SQL for big projects.It abstracts the DB. While it"s OK if you know what"s happening behind the scene, it"s a trap for new programmers that can write very greedy statements, like a heavy hit in a for loop.

How to learn about ORM?

Well, use one. Whichever ORM library you choose, they all use the same principles. There are a lot of ORM libraries around here:

If you want to lớn try an ORM library in web programming, you"d be better off using an entire framework stack like:

Do not try lớn write your own ORM, unless you are trying to learn something. This is a gigantic piece of work, và the old ones took a lot of time and work before they became reliable.

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *