Created
July 20, 2011 22:39
-
-
Save kdmukai/1096108 to your computer and use it in GitHub Desktop.
Extending David Chandler's ObjectifyGenericDao with interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.essaytagger.model.BasicUser; | |
import com.essaytagger.model.Essay; | |
import com.essaytagger.server.impl.ObjectifyGenericDAO; | |
// ... | |
ObjectifyGenericDAO<BasicUser> basicUserDao = new ObjectifyGenericDAO<BasicUser>(); | |
BasicUser user = basicUserDao.get(userId); | |
ObjectifyGenericDAO<Essay> essayDao = new ObjectifyGenericDAO<Essay>(); | |
Essay essay = essayDao.get(essayId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.essaytagger.model.BasicUser; | |
import com.essaytagger.model.Essay; | |
import com.essaytagger.server.IGenericDAO; | |
import com.essaytagger.server.impl.ObjectifyGenericDAO; | |
// ... | |
IGenericDAO<BasicUser> basicUserDao = new ObjectifyGenericDAO<BasicUser>(); | |
IGenericDAO<Essay> essayDao = new ObjectifyGenericDAO<Essay>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.essaytagger.model.BasicUser; | |
import com.essaytagger.model.Essay; | |
import com.essaytagger.server.EssayTaggerDAOFactory; | |
import com.essaytagger.server.IGenericDAO; | |
// ... | |
IGenericDAO<BasicUser> basicUserDao = EssayTaggerDAOFactory.getBasicUserDAO(); | |
IGenericDAO<Essay> essayDao = EssayTaggerDAOFactory.getEssayDAO(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.essaytagger.model.BasicUser; | |
import com.essaytagger.model.Essay; | |
import com.essaytagger.server.EssayTaggerDAOFactory; | |
import com.essaytagger.server.EssayTaggerDAOFactory.IBasicUserDAO; | |
import com.essaytagger.server.EssayTaggerDAOFactory.IEssayDAO; | |
// ... | |
IBasicUserDAO basicUserDAO = EssayTaggerDAOFactory.getBasicUserDAO(); | |
IEssayDAO essayDAO = EssayTaggerDAOFactory.getEssayDAO(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.essaytagger.model.BasicUser; | |
import com.essaytagger.model.Comment; | |
import com.essaytagger.model.Essay; | |
import com.essaytagger.server.EssayTaggerManagerFactory; | |
import com.essaytagger.server.EssayTaggerManagerFactory.IBasicUserManager; | |
import com.essaytagger.server.EssayTaggerManagerFactory.IEssayManager; | |
import com.google.appengine.api.datastore.EntityNotFoundException; | |
import com.googlecode.objectify.Key; | |
// ... | |
// Retrieve the user | |
IBasicUserManager basicUserManager = EssayTaggerManagerFactory.getBasicUserManager(); | |
BasicUser basicUser = basicUserManager.get(userId); | |
// Retrieve the essays that belong to this user | |
IEssayManager essayManager = EssayTaggerManagerFactory.getEssayManager(); | |
List<Key<Essay>> essayKeys = essayManager.listKeysByProperty("owner", basicUserManager.getKey(userId)); | |
// Iterate through and call one of the Extension methods | |
for (Key<Essay> key : essayKeys) { | |
Essay essay = essayManager.get(key); | |
Comment comment = new Comment("This is my comment"); | |
essayManager.addCommentToEssay(essay, comment); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EssayManager { | |
IEssayDAO dao; | |
public EssayManager() { | |
dao = EssayTaggerDAOFactory.getEssayDAO(); | |
} | |
public delete(Essay essay) { | |
// Do something important first | |
// ... | |
// Then make the pass-through call | |
dao.delete(essay) | |
} | |
public update(Essay essay) { | |
// Nothing special to do | |
dao.update(essay); | |
} | |
// And on and on for all the other basic operations | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.essaytagger.server; | |
import com.essaytagger.model.BasicUser; | |
import com.essaytagger.model.Essay; | |
import com.essaytagger.server.impl.ObjectifyGenericDAO; | |
import com.googlecode.objectify.ObjectifyService; | |
public class EssayTaggerDAOFactory { | |
static { | |
ObjectifyService.register(BasicUser.class); | |
ObjectifyService.register(Essay.class); | |
} | |
// Static-only usage pattern | |
protected EssayTaggerDAOFactory() {} | |
public static IGenericDAO<BasicUser> getBasicUserDAO() { | |
return new ObjectifyGenericDAO<BasicUser>; | |
} | |
public static IGenericDAO<Essay> getEssayDAO() { | |
return new ObjectifyGenericDAO<Essay>; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EssayTaggerDAOFactory { | |
// Declare entity-specific versions of each Generic interface | |
public interface IBasicUserDAO extends IGenericDAO<BasicUser> {} | |
public interface IEssayDAO extends IGenericDAO<Essay> {} | |
// Then use those interfaces as we declare entity-specific DAOs | |
private static class BasicUserDAO extends ObjectifyGenericDAO<BasicUser> implements IBasicUserDAO {} | |
private static class EssayDAO extends ObjectifyGenericDAO<Essay> implements IEssayDAO {} | |
static { | |
ObjectifyService.register(BasicUser.class); | |
ObjectifyService.register(Essay.class); | |
} | |
// Static-only usage pattern | |
protected EssayTaggerManagerFactory() {} | |
public static IBasicUserDAO getBasicUserDAO() { | |
return new BasicUserDAO(); | |
} | |
public static IEssayDAO getEssayDAO() { | |
return new EssayDAO(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment