Firstly, you need to create a class member variable for the entity's DAO, and annotate it with @Inject:
public class MyFacade { // ... @Inject private SomeEntityDAO someEntityDAO;
(The @Inject annotation is provided by Guice, a dependency injector. At runtime, Guice will initialize the someEntityDAO variable to use the configured implementation of SomeEntityDAO. You don't really need to worry about any of this.)
The someEntityDAO object "knows" how to create instances of the entity interface. In this scenario, you'll use the DAO to retrieve the instance of the entity from the database.
Because instances of your class are created outside of Guice's control, you must code an explicit constructor which requests Guice to "inject" class variables (in particular the someEntityDAO variable you created in the previous step):
public MyFacade() { GuiceWrapper.getInjector().injectMembers(this); }
If you fail to do this step, then when your application runs you will likely see a NullPointerException when your application attempts to access the someEntityDAO variable.
In your façade method, code a variable to hold an instance of the entity interface, and set its value by calling.get() on the DAO, passing the key of the database row:
// retrieve the instance of the entity final SomeEntity someEntity = someEntityDAO.get(key.someEntityID);
Now code calls to the entity "getters" to map fields values to your return struct:
// map the details from the entity instance someEntityDetails.details.someEntityID = someEntity.getID(); someEntityDetails.details.name = someEntity.getName(); someEntityDetails.details.versionNo = someEntity.getVersionNo(); final DateRange dateRange = someEntity.getDateRange(); someEntityDetails.details.startDate = dateRange.start(); someEntityDetails.details.endDate = dateRange.end(); // ...more mappings
You must code a mapping for each field that you need to return to the client. Code-completion in IDEs like Eclipse will help!
Here's the complete code for this scenario solution:
public class MyFacade { // ... @Inject private SomeEntityDAO someEntityDAO; public MyFacade() { GuiceWrapper.getInjector().injectMembers(this); } public SomeEntityDetails viewSomeEntityDetails( final SomeEntityKey key) throws AppException, InformationalException { // create an instance of the return struct final SomeEntityDetails someEntityDetails = new SomeEntityDetails(); // retrieve the instance of the entity final SomeEntity someEntity = someEntityDAO.get(key.someEntityID); // map the details from the entity instance someEntityDetails.details.someEntityID = someEntity.getID(); someEntityDetails.details.name = someEntity.getName(); someEntityDetails.details.versionNo = someEntity.getVersionNo(); final DateRange dateRange = someEntity.getDateRange(); someEntityDetails.details.startDate = dateRange.start(); someEntityDetails.details.endDate = dateRange.end(); // ...more mappings // return to the client return someEntityDetails; } // ... }
For this first scenario only, here's a side-by-side look at the classic approach vs. the service-layer API approach: