Pixel Core Tutorial – Persistence Layer
Now, when we created domain model, we need to add persistence layer (data managers). Data managers will provide interaction between application layer and database. They enable application to save, retrieve and delete object. In general, each domain object should have its own data manager. Also it is a good practice to create interface for each manager.
GameDef
IGameDefManager
There is nothing complex in this one. The interface should extend IPersistenceManager with generic parameters of the domain class and domain class id
[java]
package com.pixelnation.gaming.engine.persistence.iface;
import com.pixelnation.common.persistence.iface.IPersistenceManager;
import com.pixelnation.gaming.engine.domain.GameDef;
public interface IGameDefManager extends IPersistenceManager<GameDef, Integer>
{
}
[/java]
GameDefManager
Same here. Just extend it from PersistenceManager and implement newly created interface (defined above). Make sure to add annotations @Repository and @GenericClass
@GenericClass annotation tells underlying implementation what class to use
[java]
package com.pixelnation.gaming.engine.persistence;
import org.springframework.stereotype.Repository;
import com.pixelnation.common.annotations.GenericClass;
import com.pixelnation.common.persistence.PersistenceManager;
import com.pixelnation.gaming.engine.domain.GameDef;
import com.pixelnation.gaming.engine.persistence.iface.IGameDefManager;
@GenericClass(GameDef.class)
@Repository
public class GameDefManager extends PersistenceManager<GameDef, Integer> implements IGameDefManager
{
}
[/java]
Now let’s see what can we do with those newly created manager, what methods are available to us out of the box. As you can see we implement IPersistenceManager and extend PersistenceManager. So all methods available to this class could be called for newly created managers.
Let’s create managers for other two classes
GameRun
IGameRunManager
[java]
package com.pixelnation.gaming.engine.persistence.iface;
import com.pixelnation.common.persistence.iface.IPersistenceManager;
import com.pixelnation.gaming.engine.domain.GameRun;
public interface IGameRunManager extends IPersistenceManager<GameRun, Long>
{
}
[/java]
GameRunManager
[java]
package com.pixelnation.gaming.engine.persistence;
import org.springframework.stereotype.Repository;
import com.pixelnation.common.annotations.GenericClass;
import com.pixelnation.common.persistence.PersistenceManager;
import com.pixelnation.gaming.engine.domain.GameRun;
import com.pixelnation.gaming.engine.persistence.iface.IGameRunManager;
@GenericClass(GameRun.class)
@Repository
public class GameRunManager extends PersistenceManager<GameRun, Long> implements IGameRunManager
{
}
[/java]
GameMetrics
IGameMetricsManager
[java]
package com.pixelnation.gaming.engine.persistence.iface;
import com.pixelnation.common.persistence.iface.IPersistenceManager;
import com.pixelnation.gaming.engine.domain.GameMetrics;
public interface IGameMetricsManager extends IPersistenceManager<GameMetrics, Integer>
{
}
[/java]
GameMetricsManager
[java]
package com.pixelnation.gaming.engine.persistence;
import org.springframework.stereotype.Repository;
import com.pixelnation.common.annotations.GenericClass;
import com.pixelnation.common.persistence.PersistenceManager;
import com.pixelnation.gaming.engine.domain.GameMetrics;
import com.pixelnation.gaming.engine.persistence.iface.IGameMetricsManager;
@GenericClass(GameMetrics.class)
@Repository
public class GameMetricsManager extends PersistenceManager<GameMetrics, Integer> implements IGameMetricsManager
{
}
[/java]