【发布时间】:2010-08-28 01:25:31
【问题描述】:
对不起标题。我确实意识到它不是很具有描述性。 :|
这是我的问题。我有一个 Table 类,它定义了为数据库表找到的各种属性。在这个对象中,我还有一个名为PrimaryKey 的属性。 PrimaryKey 可以是 PrimaryKey 类型(我知道,令人困惑)或 CompositeKey。显然,单列主键由一列组成,而复合键由两列或更多列组成。
/// <summary>
/// Defines what primary keys are supported.
/// </summary>
public enum PrimaryKeyType
{
/// <summary>
/// Primary key consisting of one column.
/// </summary>
PrimaryKey,
/// <summary>
/// Primary key consisting of two or more columns.
/// </summary>
CompositeKey,
/// <summary>
/// Default primary key type.
/// </summary>
Default = PrimaryKey
}
/// <summary>
/// Defines a database table entity.
/// </summary>
public class Table
{
public Table()
{
Columns = new List<Column>();
}
public string Name { get; set; }
public string Owner { get; set; }
public AbstractPrimaryKey (What must the type be) PrimaryKey { get; set; }
public IList<Column> Columns { get; set; }
public override string ToString()
{
return Name;
}
}
/// <summary>
/// Defines a database column entity;
/// </summary>
public class Column
{
public string Name { get; set; }
public bool IsPrimaryKey { get; set; }
public string DataType { get; set; }
public bool IsNullable { get; set; }
}
public interface IPrimaryKey
{
PrimaryKeyType KeyType { get; }
}
public interface IPk : IPrimaryKey
{
Column KeyColumn { get; set; }
}
public interface ICompositeKey : IPrimaryKey
{
IList<Column> KeyColumns { get; set; }
}
public abstract class AbstractPrimaryKey
{
public abstract PrimaryKeyType KeyType { get; }
}
/// <summary>
/// Defines a primary key entity.
/// </summary>
public class PrimaryKey : AbstractPrimaryKey, IPk
{
public override PrimaryKeyType KeyType
{
get { return PrimaryKeyType.PrimaryKey; }
}
public Column KeyColumn { get; set; }
}
/// <summary>
/// Defines a composite key entity.
/// </summary>
public class CompositeKey : AbstractPrimaryKey, ICompositeKey
{
public CompositeKey()
{
KeyColumns = new List<Column>();
}
public override PrimaryKeyType KeyType
{
get { return PrimaryKeyType.CompositeKey; }
}
public IList<Column> KeyColumns { get; set; }
}
我试图让无论PrimaryKeyType如何,访问特定的Table对象都可以让我访问CompositeKey类中的属性Columns。
我怎样才能做到这一点?如果这是不可能的,我有什么选择?我明白我可以简单地将IList<Column> Columns 添加到IPrimaryKey。但是,如果我在列表中有一个主列(我知道事实总是一个),那似乎不太正确。这是我第一次尝试,所以我确信这个设计还有改进的空间。
【问题讨论】: