【问题标题】:Interface as property return type when underlying type *could* be different?当底层类型*可能*不同时,接口作为属性返回类型?
【发布时间】: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&lt;Column&gt; Columns 添加到IPrimaryKey。但是,如果我在列表中有一个主列(我知道事实总是一个),那似乎不太正确。这是我第一次尝试,所以我确信这个设计还有改进的空间。

【问题讨论】:

    标签: c# interface entity


    【解决方案1】:

    向AbstractPrimaryKey 添加KeyColumns 属性很好,即使PrimaryKey 总是有一个值——至少接口是统一的。在 ADO.NET 中,DataTable.PrimaryKey 是 DataColumns 的数组(尽管现代指导方针规定它应该是 IList 或集合类)。

    另一种方法是保持原样,然后检查PrimaryKeyType 并根据需要转换为CompositeKey。这只有在您只需要在一个地方进行区分时才有意义。

    【讨论】:

      【解决方案2】:

      如果抽象是IPrimaryKey,那么我认为有一个列集合是合适的,因为当您使用该抽象时,您不会知道只有一个列。您在多态上所做的任何事情都必须假设 PrimaryKey 由零个或多个列(或“null 或 1 个或多个列”)组成。

      为方便起见,您可以将Column 属性添加到PrimaryKey。

      【讨论】:

        猜你喜欢
        • 2020-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多