C#公共嵌套类或更好的选择? [英] c# Public Nested Classes or Better Option?

查看:145
本文介绍了C#公共嵌套类或更好的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有多个设置,并且可以具有任何数量的附连到它的传感器(每个都有它自己的一组设置)的控制电路。这些传感器只可与控制电路所使用。我想用像这样的嵌套类:

I have a control circuit which has multiple settings and may have any number of sensors attached to it (each with it's own set of settings). These sensors may only be used with the control circuit. I thought of using nested classes like so:

public class ControlCircuitLib
{
    // Fields.
    private Settings controllerSettings;
    private List<Sensor> attachedSensors;

    // Properties.
    public Settings ControllerSettings
    { get { return this.controllerSettings; } }

    public List<Sensor> AttachedSensors
    { get { return this.attachedSensors; } }

    // Constructors, methods, etc.
    ...

    // Nested classes.
    public class Settings
    {
       // Fields.
       private ControlCircuitLib controllerCircuit;
       private SerialPort controllerSerialPort;
       private int activeOutputs;
       ... (many, many more settings)

       // Properties.
       public int ActiveOutputs
       { get { return this.activeOutputs; } }
       ... (the other Get properties for the settings)

       // Methods.
       ... (method to set the circuit properties though serial port)        
    }

    public class Sensor
    {
       // Enumerations.
       public enum MeasurementTypes { Displacement, Velocity, Acceleration };

       // Fields.
       private ControlCircuitLib controllerCircuit;
       private string sensorName;
       private MeasurementTypes measurementType;
       private double requiredInputVoltage;
       ... (many, many more settings)

       // Properties.
       public string SensorName {...}
       ... (Get properties)

       // Methods.
       ... (methods to set the sensor settings while attached to the control circuit)
    }
}

我已阅读,公共嵌套类是一个没有没有,但也有例外。这是结构确定或是否有更好的选择吗?

I have read that public nested classes are a "no-no" but that there are exceptions. Is this structure OK or is there a better option?

谢谢!

修改

下面是我想写一个库类的控制电路的原油等级;我用code格式为prevent文本换行。

Below is a crude hierarchy of the control circuit for which I am trying to write a library class for; I used code formatting to prevent text-wrap.

Control Circuit (com. via serial port) -> Attached Sensors (up to 10) -> Sensor Settings (approx. 10 settings per sensor)
                                          Basic Controller Settings (approx. 20 settings)
                                          Output Settings (approx. 30 settings)
                                          Common Settings (approx. 30 settings)
                                          Environment Settings (approx. 10 settings)

所有的设置是通过控制器设置的,但我想一个有组织的库,而不是只在一个控制器类临时抱佛脚所有〜100方法,属性和设置。这将是pciated如果有人可以提供一个简单的例子,概述,他们将使用这种结构非常AP $ P $。谢谢!

All of the settings are set through the controller but I would like an organized library instead of just cramming all ~100 methods, properties, and settings under one Controller class. It would be HUGELY appreciated if someone could offer a short example outlining the structure they would use. Thanks!

推荐答案

一类的内容应该是的的实施细节的的OF 的。是嵌套类的实施细则外部类的的,还是仅仅利用外部类作为一种方便的名称确定范围和发现机制的?

The contents of a class should be the implementation details of that class. Are the nested classes implementation details of the outer class, or are you merely using the outer class as a convenient name scoping and discovery mechanism?

如果是前者,那么你不应该做私人的实施细则公开提供。让他们私有,如果他们是类的实现细节。

If the former, then you shouldn't be making the private implementation details publically available. Make them private if they are implementation details of the class.

如果是后者,那么你应该使用的命名空间,而不是外部类,你的范围和发现机制。

If the latter, then you should be using namespaces, not outer classes, as your scoping and discovery mechanism.

无论哪种方式,公共嵌套类是一个糟糕的code气味。我想有一个很好的理由,露出一个嵌套类。

Either way, public nested classes are a bad code smell. I'd want to have a very good reason to expose a nested class.

这篇关于C#公共嵌套类或更好的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆