开发者

Interface as property return type when underlying type *could* be different?

Sorry about the title. I do realize it's not very descriptive. :|

Here is my problem. I have a Table class that defines various properties found for a database table. Within this object I also have a property called PrimaryKey. PrimaryKey can either be of type PrimaryKey (I know, confusing) or CompositeKey. Evidently, a single column primary key consists of one column while a composite key consists of two or more columns.

/// <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; }
}

I am trying to make it that regardless of PrimaryKeyType, that accessing a specific Table object will give me access to property Columns from class CompositeKey.

How can I achieve this? If this isn't possible, what alternatives do I have? I do understand that I could simply add IList<Column> Columns to IPrimaryKey. That however doesn't seem very correct if I have a single column primary (which I know for a fact will always be one) inside a list. This is my first go at it so I'm sure there is room for improvement with this design.


It's fine to add a KeyColumns property to AbstractPrimaryKey even though PrimaryKey will always have one value—at least the interface will be uniform. In ADO.NET, DataTable.PrimaryKey is an array of DataColumns (although modern guidlines dictate that it should be an IList or a collection class).

The alternative would be to leave it as is and then check the PrimaryKeyType and cast to CompositeKey as needed. This would only make sense if you only need make the distinction in one place.


If the abstraction is IPrimaryKey, then I think having a collection of columns is appropriate, because when you're working with that abstraction, you won't know that there is only one column. Anything you're doing polymorphically will have to assume that the PrimaryKey is composed of zero or more columns (or "null or 1 or more columns").

For convenience, you could add a Column property to PrimaryKey.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜