Coding and Dismantling Stuff

Don't thank me, it's what I do.

About the author

Russell is a .Net developer based in Lancashire in the UK.  His day job is as a C# developer for the UK's largest online white-goods retailer, DRL Limited.

His weekend job entails alternately demolishing and constructing various bits of his home, much to the distress of his fiance Kelly, 3-year-old daughter Amelie, and menagerie of pets.

TextBox

  1. Fix dodgy keywords Google is scraping from my blog
  2. Complete migration of NHaml from Google Code to GitHub
  3. ReTelnet Mock Telnet Server à la Jetty
  4. Learn to use Git
  5. Complete beta release FHEMDotNet
  6. Publish FHEMDotNet on Google Code
  7. Learn NancyFX library
  8. Pull RussPAll/NHaml into NHaml/NHaml
  9. Open Source Blackberry Twitter app
  10. Other stuff

Cooking with .Net Lambdas Part 2 - Injecting Predicates into Where Expressions

A nice quick post this morning, something I spotted in a code snippet from the Castle Windsor folks over at http://docs.castleproject.org/Windsor.Windsor-tutorial-part-three-a-testing-your-first-installer.ashx and thought was nice. This post will show you how to pass a lambda into a method, and then invoke that lambda as part of a Where clause. It may not warrant a full blog post, but it's been a quiet few weeks, so there.

Also, check out the first part of this series, Cooking with .Net Lambdas Part 1 - Covering Up Them Code Smells for a more generic example of lambda goodness.

As for why it's been so quiet, I may go into that some time on another post, but for now suffice to say I've been faffing around with joists and load-bearing walls at home, not fun. Let's take a look at some code:

[Test] public void GetProductsWhere_OneProductIsInStock_ReturnsOneInStockProduct()
{
  // Arrange
var productList = new IList { new Product { IsInStock = true }, new Product { IsInStock = false } }; _productRepositoryMock.Setup(x => x.GetProducts()).Returns(productList); // Act var result = GetProductsWhere(x => x.IsInStock); // Assert Assert.AreEqual(1, result.Count()); Assert.IsTrue(result[0].IsInStock); } public IList GetProductsWhere(Predicate pred) { var productList = _productRepositoryMock.Object.GetProducts(); return productList.Where(pred.Invoke); }

You can see here I've got a little unit test that's setting up a product repository to return two products, one that's in stock and one that's out of stock, and from within my test I'm able to pass in a predicate that's used in a Where statement in my repository to filter the result set.

What's the difference between a lambda and a predicate? A predicate is simply a function that returns a boolean, in .Net a predicate is actually a delegate. .Net 3.5 gives us lambdas as a nicer way of writing our delegate. Also, I have a suspicion that with data-access technologies like Linq-To-SQL or Linq-To-NHibernate this injected predicate will become part of the select statement used to retrieve data from the database, this kinda stuff is simple, but I like it.


Categories: Architecture
Permalink | Comments (0)

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading