【发布时间】:2011-01-04 09:41:31
【问题描述】:
这可能是一个简单的,但我的头脑拒绝围绕它,所以在这种情况下外部视图总是有用的!
我需要设计一个对象层次结构来为患者实现参数注册。这将在某个日期发生并收集有关患者的许多不同参数(血压、心率等)。这些参数注册的值可以是不同的类型,例如字符串、整数、浮点数甚至是 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;}
}
这确实有点简单,但现在我想知道,由于 ParameterRegistration 中的 IList 指向 abstract ParameterRegistrationValue 对象,我将如何获得实际值(因为它存储在子对象上)?
也许整个通用的东西毕竟不是完全可以走的路:s
【问题讨论】:
-
问题是什么,为什么要使用公共字段?
-
你没有提到你的问题是什么。
-
您在这里缺少什么?这不起作用吗?它在哪里/如何失败?
-
你想要一个强类型的不同
RegistrationValues派生类实例的集合吗? -
更新了示例以使用属性(只是简写)
标签: c# generics class-design object-design