C#类设计的一般结构
-
15-10-2019 - |
题
这可能是一个简单的一个,但是我的头被拒绝围绕这样的,一个外部视图总是有用的,在这种情况下!
我需要设计的一个目层次实现一个参数登记患者。这将在某一日期,收集一些不同的参数,关于一个病人(血压,心率等)。值的那些参数的注册可以是不同的类型,例如串,整数、浮标或甚至guid(用于查找清单)。
因此我们有:
public class ParameterRegistration
{
public DateTime RegistrationDate { get; set; }
public IList<ParameterRegistrationValue> ParameterRegistrationValues { get; set; }
}
public class ParameterRegistrationValue
{
public Parameter Parameter { get; set; }
public RegistrationValue RegistrationValue { get; set; } // this needs to accomodate the different possible types of registrations!
}
public class Parameter
{
// some general information about Parameters
}
public class RegistrationValue<T>
{
public RegistrationValue(T value)
{
Value = value;
}
public T Value { get; private set; }
}
更新:谢谢你的建议,该模型现已演变到如下:
public class ParameterRegistration
{
public DateTime RegistrationDate { get; set; }
public IList<ParameterRegistrationValue> ParameterRegistrationValues { get; set; }
}
public abstract class ParameterRegistrationValue()
{
public static ParameterRegistrationValue CreateParameterRegistrationValue(ParameterType type)
{
switch(type)
{
case ParameterType.Integer:
return new ParameterRegistrationValue<Int32>();
case ParameterType.String:
return new ParameterRegistrationValue<String>();
case ParameterType.Guid:
return new ParameterRegistrationValue<Guid>();
default: throw new ArgumentOutOfRangeException("Invalid ParameterType: " + type);
}
}
public Parameter Parameter { get; set; }
}
public class ParameterRegistrationValue<T> : ParameterRegistrationValue
{
public T RegistrationValue {get; set; }
}
public enum ParameterType
{
Integer,
Guid,
String
}
public class Parameter
{
public string ParameterName { get; set; }
public ParameterType ParameterType { get; set;}
}
这确实是一个比较简单,但是现在我想知道,由于IList在ParameterRegistration点 抽象 ParameterRegistrationValue目的,我将如何能够得到实际的价值(因为它的存在子对象的)?
也许整个通用的事情的确是不相当的方式去毕竟:s
解决方案
如果您不知道最终参数和每个参数的相应类型 目的 作为参数值类型。
此外,通过参数列表进行迭代将很痛苦,因为您必须检查每个项目的类型才能确定如何处理值。
您想通过仿制药实现什么?是的,它们很酷(进行拳击/拆箱可能不是最好的主意),但是在某些情况下,您可能想使用 目的 而是(出于简单性和灵活性)。
- 帕维尔
其他提示
您可能想介绍的是一个抽象的基类 RegistrationValue<T>
那不是通用的,所以你 ParameterRegistrationValue
班级可以拥有非一般参考,而无需了解所涉及的类型。或者,制作可能是合适的 ParameterRegistrationValue
同样,也要为其添加一个非生成基类(因此 ParameterRegistration
可以是不同类型的。
第一条方法:
public abstract class RegistrationValue
{
}
public class RegistrationValue<T> : RegistrationValue
{
public RegistrationValue(T value)
{
Value = value;
}
public T Value { get; private set; }
}
现在您的代码应该编译。
一旦您拥有非传播基类,我还将将不依赖通用类型参数的通用类的任何成员移至此基类中。此示例中没有任何内容,但是如果我们正在修改 ParameterRegistrationValue
要通用,我会移动 Parameter
进入非生成基类(因为它不取决于类型参数 RegistrationValue
)
可以,你应该使用公共RegistrationValue RegistrationValue,T-是的类型,使用通用的。例如,T-是串或其他类或结构中。
或者你应该做出类ParameterRegistrationValue作为通用的,使用通用的参数,在该领域RegistrationValue.
我相信您想拥有不同的实例 RegistrationValue
S衍生的类,能够迭代它并为每个元素具有不同的类型。那是不可能的。
您仍然需要将每个元素投入到您知道的类型上,因为迭代集合将返回您的基本类型(ParameterRegistrationValue
- 这个由 IList
类型参数)。因此,与非生成有关的迭代不会有任何真正的影响 object
列表。
而且,如果您可以安全地为每个参数进行铸造(您知道所有类型),那么您可能根本不需要这样的集合 - 最好将所有参数封装在一种类型中的类,因此您可以使用强大的类型,具有Intellisense等。这样:
public class ParameterRegistration
{
public DateTime RegistrationDate { get; set; }
public PatientData PatientData { get; set; }
public Guid Identifier { get; set; }
// ...
}