C#と諸々

C#がメインで他もまぁ諸々なブログです
おかしなこと書いてたら指摘してくれると嬉しいです(´・∀・`)
つーかコメント欲しい(´・ω・`)

2008/09/24 21:50
C#と諸々 処理のクラス化 で紹介した HogeOperation<TResult> クラスは、ややトリッキーだったかもしれない。HogeOperation<TResult> クラスを用意せず、HogeOperation クラスを直接継承して、戻り値を持つ処理をクラス化するのも一つの手だ。
方法は簡単で、HogeOperation<TResult> クラスが行っていた戻り値をフィールドに保持する処理を、各処理クラスが直接行えば良いだけ。

GetFugasOperation クラス
internal sealed class GetFugasOperation : HogeOperation
{
    public GetFugasOperation(HogeService service)
        : base(service)
    {
    }

    private IList<Fuga> _result;

    public IList<Fuga> Execute()
    {
        this.Processing();
        return this._result;
    }

    protected override void MainProcessing()
    {
        this._result = this._service.GetFugas();
    }
}

GetFugaOperation クラス
internal sealed class GetFugaOperation : HogeOperation
{
    public GetFugaOperation(HogeService service)
        : base(service)
    {
    }

    private int _id;
    private Fuga _result;

    public Fuga Execute(int id)
    {
        this._id = id;
        this.Processing();
        return this._result;
    }

    protected override void MainProcessing()
    {
        var findFugaById =
            from fuga in this._service.GetFugas()
            where (fuga.Id == this._id)
            select fuga;

        this._result = findFugaById.Single();
    }
}


どうせ引数は各処理クラスでフィールド化しなきゃいけないのだから、戻り値も各処理クラスでフィールド化した方が自然な気がする。











トラックバックURL↓
http://csharper.blog57.fc2.com/tb.php/231-aa123409