A simple OODesign question: how to request data from multiple objects?

I was assigned the task of reorganizing an obsolete database application, and I'm trying to create objects that encapsulate old code. This is the first time I'm using real OODesign, now I just used OO to encapsulate some legacy logins and refactor a subset of the functions from the application. (Now this is a classic client server application with all the business logic in the user interface, the idea is to make it layered, to use clients against the application server and ultimately write a web interface.)

I will make a simplified example to explain what I want to do:

I have many instances of this class:

type TCustomer = class(TObject) private FOrders: TList<TOrder>; // or how do implement this? // the idea is that the TCustomer "owns" the TOrder, anyway in the // legacy DB I just have an orders table with FK to the customer table // not the contrary. // But if I do that the Customer object "loses" the TOrders??? [...] // Many other fields public [...] end; 

How can I request orders from all customers? Imagine that I want to request all orders from September 2007.

If I have 10,000 TCustomer objects, I don’t want everyone to create them (this means that they extract everything from the database: I would get too much information that I don’t need).

In current software, of course, this is done with a simple query on the order table.

But how is this done in the OO world?

What approach do you offer me?

In addition: when refactoring such database applications, do you suggest using ORM (this means creating a new database and transferring all the data) or just using an existing database (which is bad in my case)?

+4
source share
4 answers

The solution is not trivial, and I would not call it simple. What you are looking for is called lazy loading .

The main idea is to create a proxy object that will load objects from the database on demand and preferably do some smart caching. In your case, this object will be your own implementation of TList - simply derive from it and override the appropriate methods for loading and building objects from the database.

There are mature structures that perform object-relational matching, including lazy loading. One such example is Hibernate . If you are allowed to use .NET in a Delphi application, you can check its .NET port NHibernate . Another option would be to use iBatis or any of the frameworks listed on Wikipedia .

+4
source

I agree with dark_charlie that doing this kind of work is not trivial or simple, and I would recommend using it with a framework.

Besides the two commercial for Delphi listed on the Wikipedia page, he mentions that there are several Open Source for Delphi.

A couple of others are mentioned in the answers to this question: ORM for DELPHI win32

Most, if not all, will support their use with an existing database, although you may need some encoding / work on your part to tell the infrastructure how to map your classes to existing tables and columns.

+2
source

Take a look at DORM at https://code.google.com/p/delphi-orm/ .

Other answers correctly state that what you want (at least sometimes) is lazy loading.

However, it seems to me that in your specific example you need a query (not lazy loading) that selects some instances (TOrder) related to another (TCustomer) according to certain selection criteria (month and year). In this case, you will not use the TCustomer.Orders property (lazy loading), because it loads all orders.

Regardless of which structure you will use (and I agree with others, you need to choose or create it), it should allow you to create and run a query on the saved objects and their relationships.

+1
source

Try TMS Aurelius, which is a very complete base for object-oriented persistence, search and database serialization.

0
source

All Articles