Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Created November 15, 2014 10:58
09. 非公開メソッドの入れ替え で解説するテスト対象 DTO 群。こんな感じで設計されていれば・・・。
...(snip)...
public class ULColumns : IEnumerable
{
List<ULColumn> m_columns = new List<ULColumn>();
IValidation<ULColumns> m_val;
public ULColumns(IValidation<ULColumns> val)
{
if (val == null)
throw new ArgumentNullException("val");
m_val = val;
}
public void Add(ULColumn column)
{
m_val.Validate(this);
m_columns.Add(column);
}
public void Remove(ULColumn column)
{
m_val.Validate(this);
m_columns.Remove(column);
}
...(snip)...
internal static IValidation<ULColumns> GetDefaultValidation(ULTableStatus status)
{
return new ColumnsVariabilityValidator(status);
}
class ColumnsVariabilityValidator : IValidation<ULColumns>
{
readonly ULTableStatus m_status;
public ColumnsVariabilityValidator(ULTableStatus status)
{
m_status = status;
}
public void Validate(ULColumns t)
{
if (!m_status.IsOpened)
throw new InvalidOperationException("The column can not be modified because owner table has not been opened.");
if (0 < m_status.RowsCount)
throw new ArgumentException("The column can not be modified because some rows already exist.");
}
}
}
public class ULTable
{
ULTableStatus m_status = new ULTableStatus();
public ULTable(string tableName)
{
TableName = tableName;
}
...(snip)...
ULColumns m_columns;
public virtual ULColumns Columns
{
get
{
if (m_columns == null)
m_columns = new ULColumns(ULColumns.GetDefaultValidation(m_status));
return m_columns;
}
}
...(snip)...
}
public interface IValidation<T>
{
void Validate(T obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment