1

偽のリポジトリをセットアップしようとしています。

public class FooRepo {

    public FutureFoo<Foo> GetById(int id) {

        var foo = new Foo();
        return new FutureValue(foo);
    }

    public FutureQuery<Foo> GetByCategory(int categoryId) {

        var foos = new[] { new Foo(), new Foo() new Foo() };

        return  //what goes here?
    }

}

これの目的は、データベース接続に依存せずに、データに依存するテストを作成することです。FutureValue<>直接オブジェクトを受け入れるコンストラクターを提供するため、これは型にとって非常に簡単でした。ただし、コンストラクターFutureQuery<>は引数を取りますIQueryable query, Action loadAction

無視してもloadActionいいですか?

そのような:new FutureQuery<Foo>(foos.AsQueryable(), () => { });

または、これについての適切な方法は何ですか?


強制解決:

(FutureQuery<Foo>) Activator.CreateInstance(typeof(FutureQuery<Foo>),
                   BindingFlags.NonPublic | BindingFlags.Instance, null, 
                   new object[] { foos.AsQueryable(), null }, null);
4

1 に答える 1

1

から取得FutureQueryBase.GetResult()

    /// <summary>
    /// Gets the result by invoking the <see cref="LoadAction"/> if not already loaded.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Collections.Generic.IEnumerable`1"/> that can be used to iterate through the collection.
    /// </returns>
    protected virtual IEnumerable<T> GetResult()
    {
        if (IsLoaded)
            return _result;

        // no load action, run query directly
        if (LoadAction == null)
        {
            _isLoaded = true;
            _result = _query as IEnumerable<T>;
            return _result;
        }

        // invoke the load action on the datacontext
        // result will be set with a callback to SetResult
        LoadAction.Invoke();
        return _result ?? Enumerable.Empty<T>();
    }

を介しnullて明示的に更新する場合を除き、load アクションにはを渡す必要があります。_resultSetResult(ObjectContext, DbDataReader)

于 2016-05-10T21:28:17.573 に答える