简体   繁体   中英

Data access in GAE

I'm trying to write small application in GAE. I'm using Java and native DatastoreService to handle data (Objectify is a possibility but it does not change much).

I'm having hard time figure out how to keep object oriented design and use the data store efficiently and in transactional way.

I'm trying to build a game so I have rooms, monsters, players, etc each as a class. When the player do an action like moving or attacking I have to load some of the data, modify it and store it all in one tx so no anomalies will occur.

Should I pass a DatastoreService instance all the way down the call hierarchy or is it better to just get the DatastoreService on each class? Because the datastore ensure consistent view after I start a transaction and put an Entity when I do get I get null because i'm still in the tx, how should I handle that?

Thank you, Ido.

Firstly, I think you should look carefully at Objectify, it makes transactions and embedding objects (for example) quite a lot easier.

Secondly, I would seriously consider using a dependency injection framework such as Guice to obtain the Objectify object (or DatastoreService object) - it will make your code quite a bit cleaner in the end, as well as teaching you a useful skill for the future.

If you don't use dependency injection, or even if you do, it's probably best to put your code to control the data into a different class to the data itself. While this is not always the best strategy, I think it normally is. For example, if you find yourself wanting to update and save a monster and a player at the same time, where would you put the code - in monster or in player? I sometimes find it easiest to use a separate Data Access Object to do this kind of thing, or if it's just a little bit of work, do it in the Servlet.

The Objectify sample app might help with these bits - it uses Guice, but not a DAO as such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM