本文介绍了与实体框架状态模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型查询,它可以是两种状态(有更多,但是本作的目的,我只是比较两个):关闭。调查是在国家依赖于用户能够与询问该怎么办。例如封闭询问不能删除,其中作为一个新的调查能够被删除,等等(基本实施例)。

I have a model Enquiry, which can be in one of two states (there are more but for the purposes of this I will just compare two): New and Closed. The state the enquiry is in is dependant upon what a user is able to do with an enquiry. For example a closed enquiry cannot be deleted where as a new enquiry is able to be deleted and so forth (basic example).

我想用实体框架但不知道如何坚持这一点。下面是我的code。

I am wanting to persist this with Entity Framework but not sure how. Below is my code.

查询:

public class Enquiry
{
    public int Id { get; set; }
    public string CustomerAccountNumber { get; set; }

    public EnquiryState CurrentState { get; set; }
    public bool CanAddLines { get { return CurrentState.CanAddLines; } }
    public bool CanDelete { get { return CurrentState.CanDelete; } }

    public void ChangeState(EnquiryState currentState)
    {
        CurrentState = currentState;
    }

    public void CloseEnquiry()
    {
        CurrentState.CloseEnquiry();
    }

    /* More methods to change state here */

    public Enquiry()
    {
        CurrentState = new NewEnquiryState(this);
    }
}

EnquiryState:

EnquiryState:

public abstract class EnquiryState
{
    internal readonly Enquiry CurrentEnquiry;

    protected EnquiryState(Enquiry currentEnquiry)
    {
        CurrentEnquiry = currentEnquiry;
    }

    public virtual bool CanDelete
    {
        get { return false; }
    }

    public virtual bool CanAddLines
    {
        get { return false; }
    }

    /* More properties here */

    public abstract void CloseEnquiry();

    /* More states here */
}

NewEnquiryState:

NewEnquiryState:

public class NewEnquiryState : EnquiryState
{
    public NewEnquiryState(Enquiry enquiry) : base(enquiry) { }

    public override bool CanDelete
    {
        get { return true; }
    }

    public override bool CanAddLines
    {
        get { return true; }
    }

    /* ... */

    public override void CloseEnquiry()
    {
        CurrentEnquiry.ChangeState(new CloseEnquiryState(CurrentEnquiry));
    }

    /* ... */
}

CloseEnquiryState:

CloseEnquiryState:

public class CloseEnquiryState : EnquiryState
{
    public CloseEnquiryState(Enquiry enquiry) : base(enquiry) { }

    public override bool CanAddLines
    {
        get { return false; }
    }
    public override bool CanDelete
    {
        get { return false; }
    }

    /* ... */

    public override void CloseEnquiry()
    {
        throw new Exception("Closed Enquiry can't be closed");
    }
}

所以我的问题是,我不知道如何将这些不同的状态存储在数据库中,我应该使用每个国家的某种 INT 字段并将其映射在查询通过FK?另外,我需要的字段 CanAddLines CanDelete 太映射到数据库?看到作为逻辑包含在国家内部,很新的状态模式范例

So my question is, I'm not sure how to store these different states in a database, should I use some sort of int field on each state and map them to the Enquiry via an FK? Also, do I need to map the fields CanAddLines and CanDelete to a database too? Seeing as the logic is contained within the state, quite new to the state pattern paradigm

推荐答案

要存储在您的国家不具有任何数据。所以,实际上你需要存储唯一的国家类型:

Your state does not have any data to be stored. So, actually you need to store only state type:

[NotMapped]
public EnquiryState CurrentState { get; set; }

public int StateType
{
    get
    {
       // get value based on CurrentState
       return (CurrentState is NewEnquiryState) ? 0 : 1;
    }
    set
    {
        // create EnquireState based on value
        CurrentState = value == 0 ?
            (EnquiryState)new NewEnquiryState(this) :
            (EnquiryState)new CloseEnquiryState(this);
    }
}

BTW你不需要重写抽象类的虚拟成员,如果他们已经返回你所需要的(如 CanAddLines CanDelete 属性 CloseEnquiryState

这篇关于与实体框架状态模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:39