c# - What is this called and what does it do? (Part of a Generic Repository) -
i'm reading on generic repositories. keep stumbling bits of code don't understand. don't know if there's specific name them or not.
this complicated example i've seen:
ienumerable<t> getbyquery( expression<func<t, bool>> query = null, func<iqueryable<t>, iorderedqueryable<t>> orderby = null, string includeproperties = "");
i know (whatever is) apparently returns ienumerable
, have no idea how read mess of gobbledygook let alone how go using it.
edit: i'm more interested in knowing that's example of breakdown of specific example. need know they're called before can find explains syntax. linq??
edit 2: i'm asking much, simpler question 1 being answered. want know "what few search terms can use in order research above code?"
note, of extremely educated guess, based on how iqueryable<t>
work.
breaking down 3 parts:
expression<func<t, bool>> query = null
this clause, it's same signature that's passed where
extension method on iqueryable<t>
.
you'd pass expression, represented in code same way lambda is; c# compiler knows parameter looking expression
, compile expression tree , not delegate.
for example, assuming t
person
class int
property age
, filter out person
returned iqueryable<person>
30 years or older following:
p => p.age > 30
func<iqueryable<t>, iorderedqueryable<t>> orderby
this delegate if provided, allow set order. assuming want person
instances ordered age, you'd use:
q => q.orderby(p => p.age)
this because return type iorderedqueryable<t>
, how extension methods such thenby
, thenbydescending
pop after call orderby
, orderbydescending
; extension methods defined on iorderedqueryable<t>
(it's little more marker interface).
includeproperties
this gives away it's using entity framework underneath covers. when call include
in entity framework, allows related entities (via foreign key) , load entities related entity being returned in query.
let's person
class has property father
of type person
. if wanted query person
instances , have father
property returned well, you'd call include("father")
indicate not should entity framework instances of person
, should resolve father
relationship well.
ienumerable<t>
return type
this returned don't have access iqueryable<t>
instance , forces materialize result set (as iterate through it).
an implementation should returning materialized list (ireadonlycollection<t>
), or not (assumedly) cast of iqueryable<t>
ienumerable<t>
.
it's indication of trust writer of method has clients; iqueryable<t>
isn't returned, indicates doesn't trust clients not make inefficient database calls (which valid concern).
Comments
Post a Comment