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.