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

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>
{
}

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

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
{
}

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 PersistenceManagerSo all methods available to this class could be called for newly created managers.
Let’s create managers for other two classes

GameRun

IGameRunManager

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>
{
}

GameRunManager

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
{
}

GameMetrics

IGameMetricsManager

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>
{
}

GameMetricsManager

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
{
}