vendredi 9 juin 2023

How to support Entity Framework when using State pattern and avoid data bloat?

I want to implement a state machine for Entity, I followed State design pattern: https://refactoring.guru/design-patterns/state/csharp/example . The issue is that I don't know how to properly support EF Core in this scenario to avoid data redundancy.

Issues:

  1. I cannot generate migration because State is abstract. Should I just remove abstract?
  2. abstract State needs primary key
  3. How to avoid State table bloating? In case I just would use switch based logic and enums I would just have few entries in the State table.

public class Entity
{
    public int Id { get; private set; }
    public State State { get; private set; }
}

public abstract class State
{
    protected Entity _entity;

    public State(Entity conformanceRegister)
    {
        _entity = entity;
    }

    public abstract void Action1();
    public abstract void Action2();
    public abstract void Action3();
    public abstract void Action4();
}

public class ConcreteState1 : State
{
    public ConcreteState1 (Entity entity)
    {
        _entity = entity;
    }

    public override void Action1()
    {
        throw new NotImplementedException();
    }

    public override void Action4()
    {
        throw new NotImplementedException();
    }

    public override void Action3()
    {
        throw new NotImplementedException();
    }

    public override void Action2()
    {
        throw new NotImplementedException();
    }
}

public class ConcreteState2 : State
{
    public ConcreteState2 (Entity entity)
    {
        _entity = entity;
    }

    public override void Action1()
    {
        throw new NotImplementedException();
    }

    public override void Action4()
    {
        throw new NotImplementedException();
    }

    public override void Action3()
    {
        throw new NotImplementedException();
    }

    public override void Action2()
    {
        throw new NotImplementedException();
    }
}

public class ConcreteState3 : State
{
    public ConcreteState3 (Entity entity)
    {
        _entity = entity;
    }

    public override void Action1()
    {
        throw new NotImplementedException();
    }

    public override void Action4()
    {
        throw new NotImplementedException();
    }

    public override void Action3()
    {
        throw new NotImplementedException();
    }

    public override void Action2()
    {
        throw new NotImplementedException();
    }
}

Aucun commentaire:

Enregistrer un commentaire