What does it mean to be Composable?

.net entity-framework

Question

I'm currently reading through EF4 Recipes, the book, by Larry Tenny and Zeeshan Hirani. I came across the word "Composable" a lot, during reading the book, & had a general sense about what the word means, but no exact definition though.

I was wondering what the exact definition is, & what makes (say for eg) a function "Composable" or not?

For more context, Check this FAQ (Look for the word "Composable" on the page, there's only one) which is pretty similar to the same context on the book..

Here's is a paragraph where I feel confused about what it means (from the book page 397):

The parameters for model defined functions don’t show direction. There are no ‘out’ parameters, only implied ‘in’ parameters. The reason for this is that model defined functions are composable and can be used as part of LINQ queries. This prevents them from returning values in output parameters.

1
14
12/20/2010 12:18:48 AM

Accepted Answer

Composability, in this sense, means that you can further refine the query.

EF queries are very composable. So you can take a query and change it:

var q = Context.MyStuff;
q = q.Where(s => s.IsGood);
var r = from s in q select new { Id = s.Id, Description = s.Description };
r = r.OrderBy(s => s.Description);
r = r.Take(100);

All this work will be done on the DB server, because the final query is composed of its parts, built up in the code above.

WCF Data Services, OTOH, are much more limited. You can project, and you can order, but you can't order on the projection. So the code above won't work, although it could be tweaked and re-ordered to work.

9
12/20/2010 3:39:27 PM

Popular Answer

It's referring to something called Model Defined Functions.

Essentially, you define these MDF's in your EDMX, then you can "compose" these queries in your LINQ statement.

For example, say you had a scalar UDF in your DB which returned the age of a person, then you could map it in your conceptual model and do this:

var results = from person in ctx.People
              where GetAge(person) > 35 // GetAge is a UDF mapped in your EDMX
              select person;

There is a article here which explains it a bit more.

I haven't used them much - but i believe it has some limitations (for example you can't use Table-Valued Functions, only Scalar).

HTH



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow