jeudi 21 novembre 2019

Trouble updating original item in list from object reference retrieved in dictionary

I have a redux like state tree that looks as such (reduced down for readability). I cannot mutate an object, I must replace it, because the StateBase I am using has a bunch of logic that requires you to not mutate (similar to redux).

public class StateTree : StateBase
{
      public List<News> News;
      public List<Opinion> Opinions;
}

These items have visual representations that I am dragging and dropping. my visual objects are UIObject and they represent a domain object, which can be one of several types. I created an IMoveable interface for my domain objects that can be dragged and dropped (through a UIObject)

I have no real control over the UIObject creation so when a UIObject is dropped i dont have a link directly to my domain object. As such I have tried to create a Dictionary<UIObject, IMoveable>

I can now find my IMoveable (domain object), but I do not know where in the state tree it is, as that depends on its type. I could for loop over each item in each list in my StateTree and check if it's the item and if so replace that array index with a new item. I would prefer not to do that.

Here is my drag end, where I get the domain object and pass it on to my reducer. (Just a function that returns a new state)

IMoveable Actor = movementManager.GetIMoveableFromDroppedObject(droppedObject);

Store.Dispatch<MoveableAction>(new MoveableAction() {
    ActionType = "MoveObject", ObjectPosition = ObjectPosition, Actor = Actor 
});

This store dispatch gets picked up and does the following

        public override StateTree Reduce(StateTree state, MoveableAction action)
        {
            switch (action.ActionType)
            {
                case "MoveObject":

                  var moveable = action.Actor;
                  IMoveable obj = (IMoveable)moveable.Clone();
                  obj.UpdatePosition(action.ObjectPosition);
                  moveable = obj;
                break;
            }
        return state;
        }

This doesn't seem to function as expected, the original List that contains the IMoveable I found in my dictionary is unaffected. Is there a way to accomplish what I want to do, or am I forced to go through every possible list and do a comparison, and ditch the dictionary idea?

Aucun commentaire:

Enregistrer un commentaire