Using Data Model Classes in Application Code

This article explains how an implemented Data Model can be used in application code. You should read the Usage of Data Base Classes article when you want to know how such a Data Model is organized and how it needs to be implemented.

Although this article doesn’t it mention explicitely, please be aware that you need an active transaction for all operations that require interaction with the persistent store.

The Bootstrap

There are two ways to actually retrieve a copy of your DAO Factory. The first and easiest way for beginners is to create the factory yourself as described in the Usage of Data Base Classes article:

1
2
3
4
5
// Create the factory
ExampleDaoFactory factory = new ExampleDaoFactoryImpl();
 
// Use it
...

However, this limits your opportunities in your implementation details. You will be required to change application code once you decide to change the model implementations. That’s why the better way is to use the configuration method as described in Data Model Configuration article.

1
2
3
4
5
6
7
8
9
10
11
// Get a reference to the OSGI model service
IOsgiModelService modelService = OsgiModelServiceImpl.getModelService();
 
// Configure the service to load from an XML file
modelService.setConfiguration(new XMLConfiguration("/path/to/dao-config.xml"));
 
// Retrieve our DAO factory
ExampleDaoFactory factory = (ExampleDaoFactory)modelService.getFactory("example");
 
// Use it
...

As you see: there is no hard-coded implementation detail anymore. Interfaces and Implementation is completely separated and can even be developed by different teams.

Getting the Data Access Object

Once you have a reference to your DAO Factory, you can now navigate back and forth to your Data Access Object (DAO):

1
2
3
4
5
6
7
8
// The recommended way
CustomerDAO dao1 = factory.getCustomerDAO();
 
// An alternative way
CustomerDAO dao2 = factory.getDao(CustomerDAO.class);
 
// Use it
...

Line 2 demonstrates the recommended way. Prerequisite is that you defined an appropriate interface method when you were designing the data model. The second method, demonstrated by line 5, shows how to get your DAO when you didn’t define such a method.

Creating a Business Object

The most important design principle was to separate interfaces and implementations. That’s why you cannot create new Business Object (BO) instances by using the new operator. Instead you must use the newInstance() method of your DAO. Only your DAO knows how to create new BO instances:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Create a new instance
Customer customer1 = dao.newInstance();
 
// Populate properties
customer1.setName("John Doe");
customer1.setInvoiceAddress(new Address(...));
...
 
// Finally persist the new instance
dao.create(customer1);
 
// This is our ID
long id = customer1.getId();

These lines require some explanation. Line 2 will return a new blank instance of a customer. It may already be initialized with a few properties but that is up to your DAO. You shall be aware that your new BO is not yet connected to a persistent store. In fact it may not even have a Primary Key assigned, yet.

Lines 5 and 6 will set the required data properties. This is where your application code populates the new BO instance with all required data.

Finally, you persist the new instance by calling create() (line 10). The DAO now assigns a proper Primary Key and ties your BO to a persistent store. This Primary Key can be retrieved from the BO via getId().

Finding Business Objects

Once you have some data in your Data Model, you can of course search for them. This is the responsibility of you DAO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Find a specific customer
Customer customer = dao.findBy(1L);
if (customer != null) {
   // It was found
   ...
}
 
// Get all customers at once (memory consumption!)
List<customer> all = dao.findAll();
for (Customer c : all) {
   // Process the customer
   ...
}
 
// Iterate on customers
DaoIterator</customer><customer> i = dao.iterateAll();
// Get all customers at once (memory consumption!)
List</customer><customer> all = dao.findAll();
while (i.hasNext()) {
   Customer c = i.next();
   // Process the customer
   ...
}
// Do not forget to close the iterator!
i.close();
</customer>

It is advisable to follow some naming conventions. findBy shall be used when you want to have all BOs loaded into memory immediately (lines 3 and 10). iterateBy shall be used when you potentially await many objects to be returned and don’t want all of them in memory at once. However, as the iterator holds some resources in the background, e.g. database cursors, you will need to close the cursor explicitely. It is recommended to put that close() call in a finally clause so that it cannot be forgotten about even when an exception occurs.

The IGeneralDAO already defines a few search methods. However, you can add your specific methods in your own DAO interface and implement them accordingly. The abstract implementations usually provide helpful methods so that you don’t need to care about too many infrastructure details.

Modifying Business Objects

Modification of BOs is very simple. Once you have a reference to your BO, just change the appropriate properties and save it finally:

1
2
3
4
5
// Change the name
customer.setName("John Doe-Weissmueller");
 
// Save it
dao.save(customer);

There is nothing else to be done.

Deleting Business Objects

Deleting a BO is as easy as modifying it. A simple call fulfills the request:

1
2
// Delete the customer
dao.delete(customer);

Misc

The IGeneralBO and IGeneralDAO interfaces provide many useful methods that come out-of-the-box and fulfill other common tasks (such as copy, bulk search or delete requests). Just check them out!

One Response to “Using Data Model Classes in Application Code”

  1. American Steroids Online Says:

    […] with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American […]

Leave a Reply