MVVM Dapper 如何将更改的集合项立即保存到数据库

MVVM Dapper how to save changed collectionitems instantly to database

我有一个与上述主题相关的问题。

------使用 Dapper 和 Caliburn Micro-----

目前我正在构建一个显示订单列表的 mvvm wpf 应用程序。这些订单需要分配一些资源,以便可以开始工作流程。

订单列表为每行(订单)提供了几个按钮,用于开始、暂停、完成和设置订单状态,如“已 material 已分配”。

将通过列表对上述步骤所做的更改保存到数据库的最佳做法是什么? 创建订单时,我只是通过单击按钮将值传递给数据库访问项目中的方法。

目前我们只讨论 UIOrderModel 和 属性 IsAllocated

单击按钮(丑陋的米色按钮)后,将触发以下方法:

     public void MatAllocated(UIOrderModel order) {
        order.IsMatAllocated = "Y";
        order.IsAllocated = true;
        order.StatusFlag = MKDataWork.Library.Enums.StatusFlag.allocated;
        OrdersBc.Refresh();
     }

既然它能正常工作,我想问的是如何在我的数据库中存储有关分配状态更改的信息。例如,在上面的方法中触发更新查询 (sp) 会很容易。

集合和数据库应该始终具有相同的数据状态。

UiOrderModel:

   public class UIOrderModel {
    private UIOrderModel UIorder = null;
    public int Code { get; set; }
    public UIuserModel OrderedByEmp { get; set; }
    public int OrderedByEmpPersId { get; set; }
    public string OrderedByEmpName { get; set; }
    public List<UIAllocationModel> AllocationList {get;set;}
    public DateTime OrderDate { get; set; } 
    public UIDepartmentModel ForDepartment { get; set; }
    public string DepartmentName { get; set; }
    public DateTime ExpectedFinishDate { get; set; }
    public string Project { get; set; }
    public string Commission { get; set; }
    public string IsMatAllocated { get; set; } = "N";
    public bool IsAllocated { get; set; } = false;
    public string Additions { get; set; }
    public StatusFlag StatusFlag { get; set; }
    public decimal? Quantity { get; set; }
    public UnitOfMeasures Unit { get; set; }
    public UIitemModel Item { get; set; }
    public string ItemName { get; set; }
    public string ItemCode { get; set; }
    public DateTime FinishedDateTime { get; set; }
    public UIuserModel FinishedByEmp { get; set; }

    public UIOrderModel() { }

    public UIOrderModel(Dictionary<string,object> entries) {
        int i = 0;

        this.UIorder = this;
        this.Code = int.TryParse(entries["Code"].ToString(), out i) ? i : 0;
        this.OrderedByEmp = (UIuserModel)entries["OrderedByEmp"];
        this.OrderedByEmpPersId = ((UIuserModel)entries["OrderedByEmp"]).PersId;
        this.ForDepartment = (UIDepartmentModel)entries["SelectedDepartment"];
        this.DepartmentName = ((UIDepartmentModel)entries["SelectedDepartment"]).Bezeichnung;
        this.ExpectedFinishDate = (DateTime)entries["ExpectedFinishDate"];
        this.Quantity = (decimal?)entries["Quantity"];
        this.Unit = (UnitOfMeasures)entries["SelectedUnitOfMeasures"];
        this.Item = (UIitemModel)entries["Item"];
        this.ItemName = ((UIitemModel)entries["Item"]).ItemName;
        this.ItemCode = ((UIitemModel)entries["Item"]).ItemCode;
        this.StatusFlag = (StatusFlag)entries["StatusFlag"];
        this.Project = (string)entries["Project"];
        this.Commission = (string)entries["Commission"];
        this.Additions = (string)entries["Additions"];
    }

    public UIOrderModel(int code,string orderedByEmpName, int orderedByEmpPersId, string departmentName, DateTime expectedFinishDate,
                        decimal? quantity, UnitOfMeasures unit, string itemname, string itemcode, string project, string commission,
                        StatusFlag statusFlag, string additions) 
    {
        this.UIorder = this;
        this.Code = code;
        this.OrderedByEmpPersId = orderedByEmpPersId;
        this.OrderedByEmpName = orderedByEmpName;
        this.DepartmentName = departmentName;
        this.ExpectedFinishDate = expectedFinishDate;
        this.Quantity = quantity;
        this.Unit = unit;
        this.ItemName =  itemname;
        this.StatusFlag = statusFlag;
        this.Project = project;
        this.Commission = commission;
        this.Additions = additions;
    }

    public void SaveOrder() {
        OrderModel result = (OrderModel)this;

        result.SaveOrder();
    }

    public static explicit operator OrderModel(UIOrderModel uiOrder) {
        return new OrderModel()
        {
            Code = uiOrder.Code,
            OrderDate = uiOrder.OrderDate,
            OrderedByEmp = (UserModel)uiOrder.OrderedByEmp,
            OrderedByEmpName = $"{uiOrder.OrderedByEmp.FirstName} {uiOrder.OrderedByEmp.LastName}",
            ExpectedFinishDate = uiOrder.ExpectedFinishDate,
            ForDepartment = (DepartmentModel)uiOrder.ForDepartment,
            AllocationList = uiOrder.AllocationList?.Select(am => (AllocationModel)am).ToList(),
            IsMatAllocated = uiOrder.IsMatAllocated,
            Quantity = uiOrder.Quantity,
            Unit = uiOrder.Unit,
            Item = (ItemModel)uiOrder.Item,
            ItemCode = uiOrder.Item.ItemCode,
            ItemName = uiOrder.Item.ItemName,
            Project = uiOrder.Project,
            Commission = uiOrder.Commission,
            StatusFlag = uiOrder.StatusFlag,
            Additions = uiOrder.Additions
        };
    }

    public static explicit operator UIOrderModel(OrderModel order) {
        return new UIOrderModel()
        {
            Code = order.Code,
            OrderDate = order.OrderDate,
            OrderedByEmp = (UIuserModel)order.OrderedByEmp,
            OrderedByEmpName = $"{order.OrderedByEmp.FirstName} {order.OrderedByEmp.LastName}",
            ExpectedFinishDate = order.ExpectedFinishDate,
            ForDepartment = (UIDepartmentModel)order.ForDepartment,
            AllocationList = order.AllocationList?.Select(am => (UIAllocationModel)am).ToList(),
            IsMatAllocated = order.IsMatAllocated,
            Quantity = order.Quantity,
            Unit = order.Unit,
            Item = (UIitemModel)order.Item,
            ItemCode = order.ItemCode,
            ItemName = order.ItemName,
            Project = order.Project,
            Commission = order.Commission,
            StatusFlag = order.StatusFlag,
            Additions = order.Additions
        };
    }
}

但是以正确的方式完成此任务的正确 MVVM 方法是什么?

谢谢指教!

好吧,我一直在度假,回来并且有一些时间和距离来回答上述问题。昨天我选择了一个比较简单的方法来解决。

我已经实现了命令模式并通过我的 UI 项目将执行传递给了数据访问。它只是对各种状态步骤(以及相关 table)和整个订单更新的一次查询。因此,我传递了一个枚举值(操作)、订单号和员工 ID 作为方法/查询的参数。

我不完全确定它是否是最好的 mvvm 方式,但它以舒适table 和快速的方式工作。