タイプとInputTypeタイプの2つのシーケンスを生成するシーケンスを処理する必要があるとします。OutputTypeErrorType
基本的な実装は次のとおりです。
class SeqProcessor {
private IEnumerable<ErrorType> errorTypes;
public SeqProcessor()
{
this.errorTypes = Enumerable.Empty<ErrorType>;
}
public IEnumerable<ErrorType> Errors
{
get { return this.errors; }
}
public IEnumerable<OutputType> ProcessItems(IEnumerable<InputType> inputTypes)
{
yield return new OutputType();
if (err) this.errorTypes = this.errorTypes.Concat(new ErrorType());
yield return new OutputType();
yield return new OutputType();
if (err) this.errorTypes = this.errorTypes.Concat(new ErrorType());
// ...
yield break;
}
}
たとえば、次の2つの選択肢があります。
IProductとの間OutputTypeで共通のインターフェース(例)を使用し、 returnErrorTypeを許可します( Linqを使用して区別するよりも)。ProcessItemsIEnumerable<IProduct>ErrorTypecalledのサブクラスを定義し、NoErrorタプルをProcessItems返しますIEnumerable<Tuple<OutputType, ErrorType>>(エラーNoErrorがない場合は、タプルで使用されます)。
編集:
ErrorTypeとは意味的に異なるためOutputType、これらのタイプを混在させると、単一責任原則に違反する可能性があります。デリゲートの使用は、許容できる代替設計になりますか?
class SeqProcessor {
public IEnumerable<OutputType> ProcessItems(
IEnumerable<InputType> inputTypes,
Action<ErrorType> onError)
{
yield return new OutputType();
// ...
onError(new ErrorType());
}
}
そのような場合、どのアプローチを使用しますか?