Home Internet Codepolitan

Codepolitan

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.

Bạn đang xem: Codepolitan

It”s good for abstracting the datastore (flat file / SQL / whatever) out in order to provide an interface that can be used in your code. For example, (in rails) instead of constructing SQL to find the first user in a users table, we could do this:

User.firstWhich would return us an instance of our user model, with the attributes of the first user in the users table.

Share
Follow
answered Jul 20 “09 at 8:27

*

CodebeefCodebeef
41.9k2020 gold badges8383 silver badges116116 bronze badges
Add a comment |
257
A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.

Xem thêm: Giao Thức Arp Là Gì – Nó Hoạt Động Ra Sao

In other words, instead of something like this:

String sql = “SELECT … FROM persons WHERE id = 10″DbCommand cmd = new DbCommand(connection, sql);Result res = cmd.Execute();String name = res;you do something like this:

Person p = repository.GetPerson(10);String name = p.FirstName;or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:

Person p = Person.Get(10);Some also implement complex query systems, so you could do this:

Person p = Person.Get(Person.Properties.Id == 10);The framework is what makes this code possible.

Now, benefits. First of all, you hide the SQL away from your logic code. This has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to “get me all persons edited the last 24 hours” might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.

Xem thêm: Nhà Cung Cấp Là Gì – Nhà Cung Cấp (Suppliers) Là Ai

Additionally, you can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn”t contain all the “plumbing” necessary to talk to the database.

Chuyên mục: Mẹo Hay

Rate this post