DataContextFactory needs the ability to decorate an existing
DataContext (like setting user properties or delegate) without needing
to create a DataContext from scratch.

Right now it takes some really ugly code like this because the
lowest-level createDataContext() method checks for a
DataContextFactory.  See last section for an example.

I think it would be better to refactor this to provide a method
signature like below, and if dataContextFactory is null, then use the
default cayenne data context.

   public DataContext createDataContext(DataContextFactory
dataContextFactory, boolean useSharedCache)

That would allow a DataContextFactory to simply call:

       DataDomain dataDomain = (DataDomain)dataChannel;
       DataContext dataContext = dataDomain.createDataContext(null,
dataDomain.isSharedCacheEnabled());

It'd be nice to make this more transparent, but I don't know if that's
reasonable.

DataDomain dataDomain = (DataDomain)dataChannel;
       DataContext dataContext = dataDomain.createDefaultDataContext();

=================================================================
Example DataContextFactory:

   public DataContext createDataContext(DataDomain dataDomain) {
       // for new dataRowStores use the same name for all stores
       // it makes it easier to track the event subject
       DataRowStore snapshotCache = (dataDomain.isSharedCacheEnabled())
               ? dataDomain.getSharedSnapshotCache()
               : new DataRowStore(dataDomain.getName(),
dataDomain.getProperties(), dataDomain.getEventManager());

       DataContext context;
       context = new DataContext((DataChannel) this, new
ObjectStore(snapshotCache));
       
context.setValidatingObjectsOnCommit(dataDomain.isValidatingObjectsOnCommit());
       return context;
   }

   public DataContext createDataContext(DataChannel dataChannel,
ObjectStore objectStore)
   {
       DataContext dataContext = createDataContext((DataDomain)dataChannel);
       dataContext.setDelegate(new AuditLoggingDataContextDelegate());
       return dataContext;
   }

Reply via email to