0

Based on the tutorial for JAVA on the page : http://tutorials.jenkov.com/java-persistence/dao-manager.html,

I trying to implement the same concept with xCode for iOS.

The goal is to provide a DAOManager : a DAOCommand (defined as a protocol) + an implementation of his execute method. I could create a specific interface implementing the protocol, but as the mentioned example, I would like to implement the method when calling it. If I am right, it's possible to achieve that in objective-c using BLOCK.

Example of the java code provided on the mentioned Page :

 public interface DaoCommand {
   public Object execute(DaoManager daoManager);
 }

Sample of code requesting a CRUD service :

  DaoManager daoManager = daoFactory.createDaoManager();
  Person person = (Person)
  daoManager.executeAndClose(new DaoCommand(){

    public Object execute(DaoManager manager){
      return manager.getPersonDao().readPerson(666);
    }

  });

I tried to implement it in the following way : 1/ The DAOCommand :

@protocol IDaoCommand

- (id) executeUsingManager:(DAOManager*)pDAOManager;

@end

2/ My PoiCRUDService interface with a method getListOfPoi calling the DAOManager :

- (id) getListOfPoi {

DAOFactory* daoFactory = [[DAOFactory alloc] initWithOfflineMode:YES];
DAOManager* daoManager = [daoFactory createManager];

[daoManager executeAndCloseDaoCmdBlock:^(id<POIDAO> pPoiDAO) {
    [pPoiDAO getListPoi];
}];
 }

3/ My DAOManager with a method executeAndClose :

- (id) executeAndCloseDaoCmdBlock:(id(^)(id<IDaoCommand>))pDaoCmdBlock {

// Execute the query
//id returnObject = [[self getPoiDAO] getListPoi];
id returnObject = pDaoCmdBlock(self);

// Close the connection
[self.dataSource closeConnection];

return returnObject;
}

When I see my code, I don't see an creation of the DAOEntity (POIDAO). Actually, I have difficulties to see how to transpose the sample Java Code to xCode. Any idea on step and way to do ?

Thanks for any ideas or advices. St.

1 Answers1

0

Here's my suggestion from a glance. It seems that DaoCommand is just used as a function object interface due the the lack of function types in Java. In Objective-C, you can just a block type. You can typedef that to DaoCommand for convenience.

typedef id (^DaoCommand)(DaoManager *);

And then an exact translation of your sample would be something like:

DaoManager *daoManager = [daoFactory createDaoManager];
Person *person =
[daoManager executeAndClose:^(DaoManager *manager){
    return [[manager getPersonDao] readPerson:666];
 }];

I can't give you any help with the other functions, since I don't see their Java code, but executeAndClose: should now take an argument of type DaoCommand (which is a block type). Make sure to follow normal coding rules for blocks and you should be set.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Thank you for your help newacct, I am going to give a try with the sample of code you gave me. But a last question before closigng this subject, I do I implement the executeAndClose Method of the daoManager. Let's say this implementation has to : 1 - Open a connection, 2 - Execute the code of the block, 3 - close the connection open at step 1. Would it be : (id)executeAndClose:(DaoCommand)pDao { /*1- Open connection;*/... /*2-execute bloc*/ pDao(self); /*3-Close Connection*/ ...} ,is the syntax : pDao(self) the right way to execute the block passing the daoManager itself as a param – xiaolong97427 Nov 30 '12 at 12:13