I am testing my WCF service via WCFTestClient. My methods run for me and return the correct values. However, when I execute from the WCFTestClient my entities are not persisting. Here is the method I am testing:
public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null)
{
Player player;
PlayerActionResult result;
PlayerAction action = (PlayerAction)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", type.ToString())).Unwrap();
action.Logic = (ActionLogic)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", type.ToString(), "Logic")).Unwrap();
action.Logic.Action = action;
if (!PlayerExists(uid))
{
return new PlayerActionResult(false, ResultType.NotFound);
}
else { player = GetPlayer(uid); }
if (pparam != null & !PlayerExists(pparam))
{
if (!PlayerExists(pparam))
{
return new PlayerActionResult(false, ResultType.NotFound);
}
action.playerparam = GetPlayer(pparam);
}
result = player.DoAction(action);
new PlayersRepositoryManager(entities).SavePlayer(player);
return result;
}
And here is the persistence logic in the PlayerRepositoryManager:
public class PlayersRepositoryManager : IPlayersRepository
{
#region Member Variables
private GameDataEntities entity = null;
#endregion
#region Constructor
public PlayersRepositoryManager(GameDataEntities entity)
{
this.entity = entity;
}
public Player SavePlayer(Player player)
{
var updated = entity.Players.Attach(player);
entity.Entry(player).State = System.Data.EntityState.Modified;
return updated;
}
}
The "entities" parameter is a property of the WCF service "ConquestService" which contains the DoAction method:
GameDataEntities entities = new GameDataEntities();
The only value that is changing in the Player object is Movement, which when I test in WcfTestClient returns that is succeeded, here is the logic of that particular action.
public class PrayLogic : ActionLogic
{
public PlayerActionResult Execute()
{
if (!CheckMP())
{
return new PlayerActionResult(false, ResultType.NoMovement);
}
Kingdom kingdom = Action.player.CurrentKingdom;
if (Helper.GetRandom(100, 1) <= 25 + (kingdom.Honor + 1))
kingdom.Honor += Helper.GetRandom(3, 1);
return new PlayerActionResult(true, ResultType.Success);
}
}
And the ActionLogic base class contains the CheckMP() method where the actual subtraction of the movement is done:
public abstract class ActionLogic
{
public PlayerAction Action { get; set; }
internal bool CheckMP()
{
if (Action.player.Movement < Action.Movement) { return false; }
Action.player.Movement -= Action.Movement;
return true;
}
}
I have tried the approach above and I have tried calling SaveChanges() on the player object from the service method and from a method within the player object and none seem to work to persist the deduction of 2 movement from the player. ARGH. Any help would be greatly appreciated I'm eager to learn what I'm doing wrong, thanks!