Skip Headers

# Using Unit of Work Properties Instead of Conforming

Previous

Next

Sometimes, you need to provide other code modules with access to new objects created in a unit of work. Conforming can be used to provide this access. However, the following alternative is significantly more efficient.

Somewhere a unit of work is acquired from a session and is passed to multiple modules for portions of the requisite processing:

    UnitOfWork uow = session.acquireUnitOfWork();

In the module that creates the new employee, note the following:

    Pet newPet = new Pet();
    Pet newPetClone = (Pet)uow.registerObject(newPet);
    uow.setProperty("NEW PET", newPet);

In other modules where `newPet` needs to be accessed for further modification, it can simply be extracted from the unit of work's properties:

    Pet newPet = (Pet) uow.getProperty("NEW PET");
    newPet.setType("Dog");

Conforming queries are ideal if you are not sure if an object has been created yet or the criteria is dynamic.

However, for situations where the quantity of objects is finite and well known, using unit of work properties is a simple and more efficient solution.