Avoid loading unnecessary data from db into objects (web pages)

A really newbie question comes up. Is there a standard (or good) way to cope with the need for not all the information contained in the database table loaded into each related object. I think in the context of web pages where you are going to use objects only to create one page, and not for an application with more lively objects.

For example, let's say that you have an article table containing the fields id, title, author, date, summary and fullContents. You do not need fullContents to load into related objects if you simply display a page containing a list of articles with their resumes. On the other hand, if you are displaying a specific article, you may want each field to be loaded for one article, and possibly only headings for other articles (for example, to display the latest articles in the sidebar).

Some methods that I can think of:

  • Don't worry about it, just download everything from the database every time.
  • You have several different, possibly inherited classes for each table, and create one suitable for this situation (e.g. SummaryArticle, FullArticle).
  • Use a single class, but set unused properties to null when creating if this field is not needed and be careful.
  • Give objects access to the database so that they can load some fields on demand.
  • Something else?

All of the above seems to have quite serious flaws.

I am new to programming, very new to OOP and absolutely new to databases, so I might not have seen the obvious answer here at all. :)

+3
source share
3 answers

(1) , , ORM . SQL . , . ( SQL/HQL ). ORM, , , , .

2) (), . . , (, AJAX). , . , . CustomerBase → . CustomerBase ID, . , , , . ( ) CustomerBase , CRUD (Create/Retrieve/Update/Delete) Customer. , . , . .

3) , .

4) . , ( BLOB).

+1

.

  • , , . , .
  • ORM. .NET Entity Framework, NHibernate Subsonic. .NET ORM. Ruby Rails. Java Hibernate.
  • . , . - SQL . , .
0

, , 1, 2 4.

1. , : , , - .

2. , , (, SummaryArticle, FullArticle): " " - , , . , Linq-2-Sql, data.Articles.Select(a => new { a .Title, a.Author }) Title Author. SQL select Title, Author from Article.

4. , : , , "-" / , "". , - . . NHibernate, , lazy=true .

, , - ORM , NHibernate Entity Framework - .

0
source

Source: https://habr.com/ru/post/1739370/


All Articles