1

I am working on a repository pattern where the API look as follows:

var visitor = repository.Find(x => x.EmailAddress == credentials.EmailAddress &&
                              x.Password == credentials.Password);

where visitor is a domain object and x represents this domain object. The method signature of the Find method on the repository is:

T Find(Func<T, bool> query);

This is all wonderful until I attempt to use this with Linq2Sql because linq2sql creates its own objects and as a result when I want to call:

context.visitors.FirstOrDefault(query); 

there is a type mismatch because linq2sql expects a function of the type it created and not the function I am passing in.

4

1 に答える 1

1

Findまず、署名を次のように変更する必要があります。

T Find(Expression<Func<T, bool>> query);

LINQ to SQLは、単純なデリゲートではなく式ツリーとしてロジックを持っている必要があります。そうしないと、SQLに変換する方法を理解できません。

それを超えて、それはひどく明確ではないのではないかと思います-リポジトリとLINQtoSQLに同じドメインクラスを使用していないようです。そうですか?それは潜在的な問題のように聞こえます。少なくとも、それは人生をかなりトリッキーにするでしょう。

于 2009-08-22T18:03:05.940 に答える