テスト中に使用されるエンティティ ビルダーの基本的な実装を提供する汎用 (抽象) ビルダーを作成しました。
これはエンティティ基本クラスです:
public abstract class Entity : IObjectState
{
[NotMapped]
public ObjectState ObjectState { get; set; }
}
これはIKey インターフェイスです:
public interface IKey
{
int Id { get; set; }
}
これはBuilder クラスです:
public abstract class Builder<T> where T : Entity, IKey, new()
{
protected int _id { get; set; }
protected ObjectState _objectState { get; set; }
public Builder()
{
_objectState = ObjectState.Added;
}
public virtual Builder<T> WithId(int id)
{
this._id = id;
return this;
}
public virtual Builder<T> HavingObjectState(ObjectState objectState)
{
_objectState = objectState;
return this;
}
public static implicit operator T(Builder<T> builder)
{
return new T
{
Id = builder._id,
ObjectState = builder._objectState
};
}
}
これはサンプルのUnitBuilder実装です。
public class UnitBuilder : Builder<Unit>
{
private string _shortDescription;
private string _longDescription;
public UnitBuilder WithShort(string shortDescription)
{
_shortDescription = shortDescription;
return this;
}
public UnitBuilder WithLong(string longDescription)
{
_longDescription = longDescription;
return this;
}
public static implicit operator Unit(UnitBuilder builder)
{
return new Unit
{
Id = builder._id,
ObjectState = builder._objectState,
Short = builder._shortDescription,
Long = builder._longDescription
};
}
}
そして、これは私が抱えている問題です:
エラー:
エラー CS1061 'Builder' には 'WithShort' の定義が含まれておらず、タイプ 'Builder' の最初の引数を受け入れる拡張メソッド 'WithShort' が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)
何が起こっているかは理解していますが、よりも優れた (よりエレガントな) ソリューションが必要thirdUnit
です。
アップデート:
提案に従って、UnitBuilder
クラスに以下を追加しました。
public new UnitBuilder WithId(int id)
{
return (UnitBuilder)base.WithId(id);
}
public new UnitBuilder WithObjectState(ObjectState objectState)
{
return (UnitBuilder)base.WithObjectState(objectState);
}
しかし、今では基本クラスに何のポイントも見当たりません... これは、一般的な一般的な基本クラスの問題でなければなりません。他の人はこれをどのように処理しますか? 多分thirdUnit
解決策はエレガントですが、私はそれについて難しいですか?:)