Martin Taal's Blog

About EMF, Teneo, CDO, Texo, Hibernate, Open-Source ERP and other topics

EMFT Texo: generating a DAO layer from ecore/xsd models

leave a comment »

The Dao pattern has been with us a for a while now. However, since the arrival of JPA/ORM tools there has been a lot of discussion if the DAO concept still makes sense. For me it always depends on the case, I have used the DAO pattern in some specific cases and have not needed it in other cases.

In any case, Texo offers DAO generation with some special functions which make sense to have when developing real-life applications (something I do for a living):

  • cross-referencing functionality
  • remove with cascade delete for single associations
  • findBy property
  • findBy using EStructuralFeatures (the ecore runtime model)
  • get and getAll

Okay, let’s see how to generate the DAO classes and how to use them at runtime. First ofcourse install Texo from the update site. Then after restarting, right click on a model file (ecore or xsd) and select the ‘Generate the Model + Dao code’ option. As a default the dao classes are generated in a sub package of the model code.

/**
 * The Dao implementation for the model object '<em><strong>Forum</strong></em>'.
 *
 * @generated
 */
public class ForumDao extends BaseDao {

  /**
   * @generated
   */
  @Override
  public Class getEntityClass() {
    return Forum.class;
  }

  /**
   * @generated
   */
  @Override
  public EClass getEClass() {
    return ForumModelPackage.INSTANCE.getForumEClass();
  }
}

Now to get to the DAO at runtime you can use the Texo DaoRegistry class, the DAO classes are registered when the model get’s initialized in memory:


final BaseDao bookDao = DaoRegistry.getInstance().getDaoForEntity(Book.class);

Or you can use the Dao class directly:


final BookDao bookDao = DaoRegistry.getInstance().getDao(BookDao.class);

and you can for example do cross-reference checks using the DAO:


// is the book referenced, only check non-containment references

bookDao.isReferenced(book, false));

// get maximum 10 referers and include containment references

List referers = bookDao.getReferingObjects(book, 10, true);

The DAO instance gets access to an EntityManager using the Texo EntityManagerProvider, it is a light-weight concept which makes it easy to pass your EntityManager instance to the Texo runtime code.

My plan is to extend the DAO generation to automatically generate typed findBy* methods based on annotations in the model.

For more information on the DAO support by Texo visit this wiki page. And as before feel free to give feedback or ask questions on the EMFT newsgroup or forum.

gr. Martin

Advertisements

Written by martintaal

November 10, 2011 at 3:52 pm

Posted in Texo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: