NHibernate 和状态模式持久性 - 一个好的实现吗?

发布于 2024-08-31 19:06:19 字数 2521 浏览 2 评论 0原文

下面是我对状态模式的实现。为了使用 NHibernate 将 State 对象持久保存到我的数据库中,我为每个状态类分配一个枚举值。它作为私有字段存储在实体上,并映射到我的数据库表中的整数字段。

我想知道这是否是一个好的实现,因为我将在整个应用程序中使用状态模式,并且希望第一次就正确。谢谢

public class Order
{
    private OrderStatusEnum _statusId;
    public virtual Guid Id { get; set; }

    private OrderState _status;
    public virtual OrderState Status {
        get
        {
            if (_status == null)
                _status = GetState(_statusId);
            return _status;
        }
        set
        {
            _status = value;
            _statusId = _status.Id;
        }
    }

    private OrderState GetState(OrderStatusEnum status)
    {
        switch (_statusId) {
            case OrderStatusEnum.Pending:
                return new Submitted(this);
            case OrderStatusEnum.Completed:
                return new Completed(this);
            default:
                return new NewOrder(this);
        }
    }
}

public abstract class OrderState
{
    private readonly Order _order;

    public OrderState(Order order) {
        _order = order;
    }

    internal Order Order { get { return _order; } }
    public abstract OrderStatusEnum Id { get; }

    public virtual void Submit() {
        throw new InvalidOperationException(
            string.Format("Can't Submit a {0} Order", this.GetType().Name)
        );
    }

    public virtual void Complete() {
        throw new InvalidOperationException(
                string.Format(string.Format("Can't Cancel a {0} Order", this.GetType().Name))
            );
    }

    protected internal void _Submit() {
        Order.Status = new Submitted(Order);
    }

    protected internal void _Complete() {
        Order.Status = new Completed(Order);
    }
}

public class NewOrder : OrderState
{
    public NewOrder(Order order) : base(order) { }

    public override OrderStatusEnum Id {
        get { return OrderStatusEnum.New; }
    }

    public override void Submit() {
        _Submit();
    }
}

public class Submitted : OrderState
{
    public Submitted(Order order) : base(order) { }

    public override OrderStatusEnum Id {
        get { return OrderStatusEnum.Pending; }
    }

    public override void Complete() {
        _Complete();
    }
}

public class Completed : OrderState
{
    public Completed(Order order) : base(order) { }

    public override OrderStatusEnum Id {
        get { return OrderStatusEnum.Completed; }
    }
}

public enum OrderStatusEnum {
    New = 1,
    Pending = 2,
    Completed = 3
}

Below is my implementation of the state pattern. In order to persist the State object to my database with NHibernate, I am assigning each state class an enum value. This is stored as a private field on the entity, and mapped to a integer field in my database table.

I want to know whether this is a good implementation as I will be using the state pattern throughout my application and want to get it right the first time. Thanks

public class Order
{
    private OrderStatusEnum _statusId;
    public virtual Guid Id { get; set; }

    private OrderState _status;
    public virtual OrderState Status {
        get
        {
            if (_status == null)
                _status = GetState(_statusId);
            return _status;
        }
        set
        {
            _status = value;
            _statusId = _status.Id;
        }
    }

    private OrderState GetState(OrderStatusEnum status)
    {
        switch (_statusId) {
            case OrderStatusEnum.Pending:
                return new Submitted(this);
            case OrderStatusEnum.Completed:
                return new Completed(this);
            default:
                return new NewOrder(this);
        }
    }
}

public abstract class OrderState
{
    private readonly Order _order;

    public OrderState(Order order) {
        _order = order;
    }

    internal Order Order { get { return _order; } }
    public abstract OrderStatusEnum Id { get; }

    public virtual void Submit() {
        throw new InvalidOperationException(
            string.Format("Can't Submit a {0} Order", this.GetType().Name)
        );
    }

    public virtual void Complete() {
        throw new InvalidOperationException(
                string.Format(string.Format("Can't Cancel a {0} Order", this.GetType().Name))
            );
    }

    protected internal void _Submit() {
        Order.Status = new Submitted(Order);
    }

    protected internal void _Complete() {
        Order.Status = new Completed(Order);
    }
}

public class NewOrder : OrderState
{
    public NewOrder(Order order) : base(order) { }

    public override OrderStatusEnum Id {
        get { return OrderStatusEnum.New; }
    }

    public override void Submit() {
        _Submit();
    }
}

public class Submitted : OrderState
{
    public Submitted(Order order) : base(order) { }

    public override OrderStatusEnum Id {
        get { return OrderStatusEnum.Pending; }
    }

    public override void Complete() {
        _Complete();
    }
}

public class Completed : OrderState
{
    public Completed(Order order) : base(order) { }

    public override OrderStatusEnum Id {
        get { return OrderStatusEnum.Completed; }
    }
}

public enum OrderStatusEnum {
    New = 1,
    Pending = 2,
    Completed = 3
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2024-09-07 19:06:19

不确定是否回答或添加评论,但您的方法在类似情况下对我来说非常有效。

我还尝试了描述的方法 这里使用 Tarantino 框架,但我发现从代码中扩展更容易。

Not sure whether to answer or add a comment, but your approach worked very well for me in a similar situation.

I also experimented with the approach described here using the Tarantino framework, but I found it easier to extend from your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文