简体   繁体   English

有关在C#MVC中实现命令模式的问题

[英]Questions on implementing Command Pattern in C# MVC

I'm hoping this is straight forward. 我希望这是直接的。 I'm trying to implement the Command Pattern in my MVC application I'm making. 我正在尝试在正在制作的MVC应用程序中实现命令模式。 The only issue I'm seeing is that the number of Command objects is really high. 我看到的唯一问题是Command对象的数量确实很高。

I have 11 tables each with around 20 fields that need to be updated. 我有11个表,每个表有大约20个字段需要更新。

My question is do I need to have a different object for each field? 我的问题是,每个字段都需要有一个不同的对象吗?

This is a healthcare application, so let me give an example. 这是一个医疗应用程序,所以让我举一个例子。 I have a table called KeyHospital, this table stores Hospital information and only Hospital information for our Hospital Clients. 我有一个名为KeyHospital的表,该表存储医院信息,并且仅存储我们医院客户的医院信息。 I've used Linq to SQL for my connection to the database. 我已经使用Linq to SQL来连接数据库。 The KeyHospital table is probably the largest as far as fields go. 就字段而言,KeyHospital表可能是最大的。 What I've done is create a new object per field. 我要做的是在每个字段中创建一个新对象。

public class ChangeHospitalDEA : ICommand
{
    public ChangeHospitalDEA(int id, string newDEA)
    {
        var Thishospital = (from Hospital in _context.Keyhospitals
                            where Hospital.ID == id
                            select Hospital).Single();

        Thishospital.DEAnum = newDEA;
    }
}

I have ICommand as an abstract class. 我将ICommand作为抽象类。

public abstract class ICommand
{
    public AllkeysDataContext _context = new AllkeysDataContext();

    public void Execute()
    {
        _context.SubmitChanges();
    }
}

Am I doing this correct? 我这样做正确吗? I just feel like I'm writing lots of code for this and it's one of my first times using the Command Pattern. 我只是觉得自己为此写了很多代码,这是我第一次使用命令模式。

Considerably too granular. 太细了。 Instead, you should define commands for actions such as inserting a new entity, (graph of one or more related objects) updating an entity, etc. 相反,您应该为操作定义命令,例如插入新实体,(一个或多个相关对象的图形)更新实体等。

A command would be used whenever you want to perform an action. 每当您要执行操作时,都将使用命令。 In some cases changing a single field may constitute an action, such as returning additional data, or providing suggestions. 在某些情况下,更改单个字段可能会构成一项操作,例如返回其他数据或提供建议。 Such as an AJAX call to validate an entered address. 例如AJAX调用以验证输入的地址。 Also, ICommand is a poor name choice for a base abstract class. 同样,对于基本抽象类, ICommand也是一个较差的选择。 ICommand is a common interface for command architectures. ICommand是命令体系结构的通用接口。 (the "I" prefix is conventionally reserved for interface names.) I deal predominantly with MVVM but I would suspect that MVC has a common command interface already. (“ I”前缀通常是为接口名称保留的。)我主要处理MVVM,但我怀疑MVC已经具有通用的命令接口。

This is might not be what you want to hear but I would restructure your user interface to be Tasks based UI and construct your UI into common Tasks for example Change Drug Enforcement Administration number and these tasks can be refined over time. 这可能不是您想听到的,但是我会将您的用户界面重新构造为基于任务的UI,并将您的UI构造为常见任务,例如,更改毒品执法管理编号,并且这些任务会随着时间的流逝而完善。

If you have existing analytics you will notice that certain field will be only changed together and these could be logically grouped into common tasks I also feel that is it is a bad practice to hide database calls within constructors and would move that linq statement to the Execute method and have the ctor only initialise public properties or private fields and have them field being used within the execute method. 如果您已有分析,则将注意到某些字段只会一起更改,并且可以在逻辑上将这些字段分组为常见任务,我也觉得在构造函数中隐藏数据库调用并将linq语句移至Execute是一种不好的做法。方法,并且让ctor仅初始化公共属性或私有字段,并在execute方法中使用它们。

Major reason for moving the query into the execute method is to reduce the time the chances of any Optimistic concurrency errors. 将查询移至execute方法的主要原因是为了减少时间,避免出现任何乐观并发错误。

Also I also feel that calling the Base class ICommand is not a good practice and may lead to confusion in the future and would recommend you calling it CommandBase or changing it to an interface once again. 另外,我还觉得调用基类ICommand并不是一个好习惯,将来可能会引起混乱,建议您将其称为CommandBase或再次将其更改为接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM