简体   繁体   English

将大量参数传递到配置对话框的最佳方法

[英]Best way to pass a large number of arguments into a configuration dialog

I've got a situation where I have a main form that pops up an advanced configuration form that just has half a dozen matched check boxes and combo boxes to select some advanced options (the check boxes to enable/disable, the combo to select a media if enabled). 我遇到的一种情况是,我有一个主窗体,它弹出一个高级配置窗体,该窗体只有六个匹配的复选框和组合框以选择一些高级选项(启用/禁用复选框,组合框选择一个高级选项)。媒体(如果启用)。

If I just pass the individual settings for the check and combo boxes in to the constructor for the dialog that's obviously a dozen arguments, which seems a bit excessive. 如果我只是将复选框和组合框的各个设置传递给对话框的构造函数,则显然有十几个参数,这似乎有点多余。

My other obvious option would be since in the main form these settings are stored in a large IDictionary with all the other main form settings I could just pass this dictionary in and fetch it back afterward with the updated values, but my understanding is that this wouldn't really be very good coding practice. 我的另一个明显选择是,因为在主表单中这些设置与其他所有主表单设置一起存储在大型IDictionary中,所以我可以传入此字典,然后使用更新后的值取回它,但是我的理解是,真的不是很好的编码实践。

Am I missing a good way to do this that is both efficient and good coding practice? 我是否错过了一种既有效又良好的编码实践的好方法?

(this particular code is in C#, although I have a feeling a general solution would apply to other languages as well) (虽然我觉得一般的解决方案也适用于其他语言,但是该特定代码是用C#编写的)

I personally would create a carrier object to store the values. 我个人将创建一个载体对象来存储值。 You then get the nice intellisense for it, and changes to it would be quite straightforward. 然后,您将获得很好的智能感知,并且对其进行更改将非常简单。 It would also be faster than dictionary lookups for parameter values. 与参数值的字典查找相比,它还将更快。 And of course, you get type safety. 当然,您会获得类型安全性。 :) :)

You could go with Rob's solution; 您可以采用Rob的解决方案; that's the prettiest for development. 这是最漂亮的发展。 Your "carrier object" could contain the entire IDictionary and have typed properties to help intellisense. 您的“载体对象”可能包含整个IDictionary,并具有有助于智能感知的类型化属性。 The properties could update the IDictionary. 这些属性可以更新IDictionary。 When you're done, you can pass the carrier object back and fetch the IDictionary directly from it. 完成后,您可以将载体对象传回并直接从中获取IDictionary。

For example, if your dictionary had key/value pair "FirstEnabled"/boolean, you could do this: 例如,如果您的字典具有键/值对“ FirstEnabled” /布尔值,则可以执行以下操作:

class ContainerObject
{
    public IDictionary<object, object> _dict;
    public ContainerObject(IDictionary<object, object> dict)
    {
        _dict = dict;
    }

    public bool FirstEnabled
    {
        get { return (bool) _dict["FirstEnabled"]; }
        set { _dict["FirstEnabled"] = value; }
    }
}

You can change the member "_dict" to private or protected and have a accessor function if you want. 您可以将成员“ _dict”更改为私有或受保护,并根据需要具有访问器功能。

Something like this should be good: 这样的事情应该很好:

MyConfigurationDialog dialog = new MyConfigurationDialog();

//Copy the dictionary so that the dialog can't mess with our settings
dialog.Settings = new Dictionary(existingSettings);

if(DialogResult.OK == dialog.Show()) {
  //grab the settings that the dialog may have changed
  existingSettings["setting1"] = dialog.Settings["setting1"];
  existingSettings["setting2"] = dialog.Settings["setting2"];
}

I agree with Rob Cooper. 我同意Rob Cooper。 Create a class to represent your configuration, and pass that into the constructor of your form. 创建一个代表您的配置的类,然后将其传递到表单的构造函数中。 This will also allow you to define methods on your new "config" class like "saveSettings", "LoadSettings", etc. That in turn should keep the code more maintainable in general. 这也将允许您在新的“ config”类上定义方法,例如“ saveSettings”,“ LoadSettings”等。这反过来又应该使代码更易于维护。

As an quick-and-dirty alternative, if you are saving these to a file somewhere, just pass the name of the file, and have your form read that at run-time. 作为一种快捷的替代方法,如果要将它们保存到某个文件中,则只需传递文件名,然后让表单在运行时读取即可。

The first option really is the way to go though, IMO. IMO确实是第一个选择。

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

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