﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Expression</title>
	<atom:link href="http://byatool.com/tag/expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>F#: Creating your own mocks using Object Expressions&#8230; Wow</title>
		<link>http://byatool.com/f/f-creating-your-own-mocks-using-object-expressions-wow/</link>
		<comments>http://byatool.com/f/f-creating-your-own-mocks-using-object-expressions-wow/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 15:57:22 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Expression]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2365</guid>
		<description><![CDATA[This just in from the &#8220;Wow, I can&#8217;t believe how easy this is&#8221; update desk: You can roll your own mocks &#8220;dynamically&#8221;, even when mocking an interface. No, this isn&#8217;t too good to be true. No that weird tingling isn&#8217;t from your bullsh@# meter going off. (But you really should get that tingling checked by [...]]]></description>
			<content:encoded><![CDATA[<p>This just in from the &#8220;Wow, I can&#8217;t believe how easy this is&#8221; update desk:</p>
<p>You can roll your own mocks &#8220;dynamically&#8221;, even when mocking an interface. No, this isn&#8217;t too good to be true. No that weird tingling isn&#8217;t from your bullsh@# meter going off. (But you really should get that tingling checked by a doctor) This is F#. And this isn&#8217;t SPARTA!!! AHRHARHARHRAhRHARHR SO FUNAY!11</p>
<p>Say you have a test method that wants to call a method Validate on the Validator : IValidator class. Here is the IValidator class:</p>
<pre>  <span style="color: #0000ff;">type</span> <span style="color: #008080;">IValidator</span>&lt;'a, 'b&gt; =
    <span style="color: #0000ff;">abstract</span> AddValidationMethod : ('a -&gt; <span style="color: #008080;">MethodResult</span>&lt;'b&gt;) -&gt; <span style="color: #008080;">IValidator</span>&lt;'a, 'b&gt;</pre>
<p><em>(Side note: the &#8216;a and &#8216;b are just F#&#8217;s generic notation. Generic members are preceeded by a &#8216;)</em></p>
<p>Somewhere in your code this IValidator is called by someone, say a controller action:</p>
<pre>  <span style="color: #0000ff;">member</span> <span style="color: #0000ff;">public</span> x.LoginPost(userName:<span style="color: #008080;">String</span>, password:<span style="color: #008080;">String</span>) =
    <span style="color: #0000ff;">let</span> loginModel = <span style="color: #0000ff;">new</span> <span style="color: #008080;">LoginModel</span>(userName, password)
    <span style="color: #0000ff;">let</span> result = x.ControllerValidator.Validate(loginModel)</pre>
<p>And of course you would have a controller definition look like this:</p>
<pre>  <span style="color: #0000ff;">type</span> <span style="color: #008080;">MainController</span>(validator:IValidator&lt;<span style="color: #008080;">LoginModel</span>, <span style="color: #008080;">LoginModel</span>&gt;) =
    <span style="color: #0000ff;">inherit</span> Controller()
      <span style="color: #0000ff;">member</span> <span style="color: #0000ff;">private</span> x.ControllerValidator <span style="color: #0000ff;">with</span> <span style="color: #0000ff;">get</span>() = validator</pre>
<p>So you are set up for mocking.</p>
<p>There are three way, but if you&#8217;re already bored (And I assume you are) then just go to 3.</p>
<p><strong>1) Mock the IValidator</strong></p>
<p>As you can imagine, this could be obnoxious to mock up as the AddValidationMethod is adding a method that takes in &#8216;a and a MethodResult&lt;&#8217;a&gt; and then returns an IValidator&lt;&#8217;a, &#8216;b&gt;. Can it be done? I would asssume so, but there is another way.</p>
<p><strong>2) Create a new class (Class file) that implements IValidator.</strong></p>
<p>This is the most simple way of doing it. You just make a class that implements IValidator and just pass back what you want. Problem is: This isn&#8217;t very reusable as it is now a static class.</p>
<p><strong>3) Create a class on the fly.</strong></p>
<p>And this is the good stuff. F# allows the creation of a class (That implements an interface) &#8220;inline&#8221;&#8230; not sure what word I&#8217;m looking for so just go with it.</p>
<pre>  <span style="color: #0000ff;">let</span> validatorWithFalseReturn =
    {
      <span style="color: #0000ff;">new</span> <span style="color: #008080;">IValidator</span>&lt;'a, 'b&gt; <span style="color: #0000ff;">with</span>
        <span style="color: #0000ff;">member</span> x.AddValidationMethod(methodToAdd:('a -&gt; MethodResult&lt;'b&gt;)) =
          <span style="color: #0000ff;">new</span> <span style="color: #008080;">Validator</span>&lt;'a, 'b&gt;() :&gt; <span style="color: #008080;">IValidator</span>&lt;'a, 'b&gt;

        <span style="color: #0000ff;">member</span> x.Validate(model:'a) =
          (<span style="color: #0000ff;">new</span> <span style="color: #008080;">MethodResult</span>&lt;'b&gt;()).AddErrorMessage(<span style="color: #800000;">""</span>)
    }

    <span style="color: #0000ff;">let</span> mainController = <span style="color: #0000ff;">new</span> MainController(validatorWithFalseReturn)</pre>
<p>(Side note: you might see the :&gt; notation. This is basically the same as (SomeInterface)SomeClass in C#.)</p>
<p>As you can see, I created a new type and set it into the instantiated controller. As you can imagine, you could create a method that takes in a method to set AddValidation to. This has some really great potential like rolling your own mocking system.</p>
<p>So <a href="http://byatool.com/f/f-duck-typing-with-generic-constraints-and-why-you-will-be-speechless">once again</a> F# <a href="http://byatool.com/f/f-and-using-strings-for-method-and-classtype-names/">is looking</a> to be an amazing language.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/f/f-creating-your-own-mocks-using-object-expressions-wow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combine Lambda Expressions: The And and the Or</title>
		<link>http://byatool.com/general-coding/combining-lambda-expressions/</link>
		<comments>http://byatool.com/general-coding/combining-lambda-expressions/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 20:17:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/09/09/combining-lambda-expressions/</guid>
		<description><![CDATA[Found this post here but wanted to make a really simple example to demonstrate this. The idea is simple, take something like this: currentItem =&#62; currentItem.BooleanMethodOne() &#38;&#38; currentItem.BooleanMethodTwo() but say you only want to have one clause or both. Well you could make three separate expressions, but what if you wanted to add even more [...]]]></description>
			<content:encoded><![CDATA[<p>Found this post <a href="http://www.albahari.com/nutshell/predicatebuilder.html">here</a> but wanted to make a really simple example to demonstrate this.</p>
<p>The idea is simple, take something like this:</p>
<pre>currentItem =&gt; currentItem.BooleanMethodOne() &amp;&amp; currentItem.BooleanMethodTwo()</pre>
<p>but say you only want to have one clause or both. Well you could make three separate expressions, but what if you wanted to add even more later? What if you wanted to mix and match? What if you&#8217;re reading thing because you watched to see what page could possibly be on the last page of a google search? Well I have answers&#8230; stolen answers.</p>
<p>First the needed And and Or methods:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; And&lt;T&gt;(
<span style="color: #00cccc;">   Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionOne,
<span style="color: #00cccc;">    Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionTwo
)
{
<span style="color: #009900;">  //Basically this is like a bridge between the two expressions.  It will take the T</span>
<span style="color: #009900;">  //parameter from expressionOne and apply it to expression two. So if </span>
<span style="color: #009900;">  // oneItem =&gt; oneItem.OneMethod() is expressionOne</span>
<span style="color: #009900;">  // twoItem =&gt; twoItem.TwoMethod() is expressionTwo</span>
<span style="color: #009900;">  //it would be like replacing the twoItem with the oneItem so that they now point</span>
<span style="color: #009900;">  //to the same thing.</span>
<span style="color: #3333ff;">  var</span> invokedSecond = <span style="color: #00cccc;">Expression</span>.Invoke(expressionTwo, expressionOne.Parameters.Cast&lt;<span style="color: #00cccc;">Expression</span>&gt;());

<span style="color: #009900;">  //Now this is to create the needed expresions to return.  It will take both early expressions</span>
<span style="color: #009900;">  //and use the item from the first expression in both.</span>
<span style="color: #009900;">                    </span>
<span style="color: #009900;">  //It will look something like this:</span>
<span style="color: #009900;">  //currentItem =&gt; (currentItem.OneMethod And Invoke(currentItem =&gt; currentItem.TwoMethod()))</span>
<span style="color: #009900;">  //As you can see, it looks to be running expressionOne and then a new method that basically</span>
<span style="color: #009900;">  //calls expressionTwo with the same value (currentItem)</span>
<span style="color: #3333ff;">  return</span> <span style="color: #00cccc;">Expression</span>.Lambda&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt;(<span style="color: #00cccc;">
   Expression</span>.And(expressionOne.Body, invokedSecond), expressionOne.Parameters
  );
}

<span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Expressio</span><span style="color: #00cccc;">n</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; Or&lt;T&gt;(<span style="color: #00cccc;">
Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionOne, <span style="color: #00cccc;">
Expression</span>&lt;<span style="color: #00cccc;">Fun</span>c&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionTwo
)
{
<span style="color: #3333ff;">  var</span> invokedSecond = <span style="color: #00cccc;">Expression</span>.Invoke(expressionTwo, expressionOne.Parameters.Cast&lt;<span style="color: #00cccc;">Expression</span>&gt;());

<span style="color: #3333ff;">  return</span> <span style="color: #00cccc;">Expression</span>.Lambda&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt;(
<span style="color: #00cccc;">   Expression</span>.Or(expressionOne.Body, invokedSecond), expressionOne.Parameters
  );
}</pre>
<p>And here&#8217;s a test for it:</p>
<pre><span style="color: #3333ff;">String</span>[] list;

list = <span style="color: #3333ff;">new</span> <span style="color: #3333ff;">String</span>[] { <span style="color: #990000;">"a"</span>, <span style="color: #990000;">"b"</span>, <span style="color: #990000;">"c"</span>, <span style="color: #990000;">"ac"</span>, <span style="color: #990000;">"ab"</span>, <span style="color: #990000;">"cc"</span>, <span style="color: #990000;">"d"</span>, <span style="color: #990000;">"dd"</span>, <span style="color: #990000;">"dc"</span> };

<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; stringLikeA = currentString =&gt; currentString.Contains(<span style="color: #990000;">"a"</span>);
<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; stringLikeB = currentString =&gt; currentString.Contains(<span style="color: #990000;">"b"</span>);
<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; stringLikeC = currentString =&gt; currentString.Contains(<span style="color: #990000;">"c"</span>);

<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, Boolean&gt;&gt; neededUser = And&lt;<span style="color: #00cccc;">String</span>&gt;(stringLikeA, stringLikeB);
list.Where(neededUser.Compile());

<span style="color: #009900;">//a</span>
<span style="color: #00cccc;">Assert</span>.IsTrue(list.Where(neededUser.Compile()).Count() == 1);  //ab

<span style="color: #009900;">//a, c, ac, ab, cc, dc</span>
neededUser = Or&lt;<span style="color: #00cccc;">String</span>&gt;(stringLikeA, stringLikeC);

<span style="color: #00cccc;">Assert</span>.IsTrue(list.Where(neededUser.Compile()).Count() == 6);

<span style="color: #009900;">//ab, c, ac, cc, dc</span>
neededUser = And&lt;<span style="color: #00cccc;">String</span>&gt;(stringLikeA, stringLikeB);
neededUser = Or&lt;<span style="color: #00cccc;">String</span>&gt;(neededUser, stringLikeC);
<span style="color: #00cccc;">Assert</span>.IsTrue(list.Where(neededUser.Compile()).Count() == 5);</pre>
<p>USINGS!!ONEONE</p>
<pre><span style="color: #3333ff;">using</span> System;
<span style="color: #3333ff;">using</span> System.Linq;
<span style="color: #3333ff;">using</span> System.Linq.Expressions;
<span style="color: #3333ff;">using</span> Microsoft.VisualStudio.TestTools.UnitTesting;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/combining-lambda-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;m starting to worry about myself</title>
		<link>http://byatool.com/general-coding/im-starting-to-worry-about-myself/</link>
		<comments>http://byatool.com/general-coding/im-starting-to-worry-about-myself/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 17:22:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Closures]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/18/im-starting-to-worry-about-myself/</guid>
		<description><![CDATA[So the &#8220;dynamic&#8221; linq query so far isn&#8217;t just enough to stop. Oh no, now that I can have methods pass back expressions, what about a dictionary of order by expressions to allow an even more dynamic feel? Huh? How&#8217;s about that kids? I hate myself too. //Enum created for the dictionary key public enum [...]]]></description>
			<content:encoded><![CDATA[<p>So the &#8220;dynamic&#8221; linq query so far isn&#8217;t just enough to stop. Oh no, now that I can have methods pass back expressions, what about a dictionary of order by expressions to allow an even more dynamic feel? Huh? How&#8217;s about that kids? I hate myself too.</p>
<pre><span style="color: #009900;">//Enum created for the dictionary key</span>
<span style="color: #3333ff;">public enum</span> <span style="color: #00cccc;">OrderByChoice</span>
{
  FirstName,
  LastName,
  UserName
}

<span style="color: #009900;">//dictionary of expressions</span>
<span style="color: #3333ff;">private static</span> <span style="color: #00cccc;">Dictionary</span>&lt;<span style="color: #000000;"><span style="color: #00cccc;">OrderByChoice</span>, </span><span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">String</span>&gt;&gt;&gt;  GetOrderByList()
{
<span style="color: #3333ff;">  if</span>(orderByList == <span style="color: #3333ff;">null</span>)
  {
    orderByList = <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">Dictionary</span>&lt;OrderByChoice,<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">String</span>&gt;&gt;&gt;();

    orderByList.Add(<span style="color: #00cccc;">OrderByChoice</span>.FirstName, currentUser =&gt; currentUser.FirstName);
    orderByList.Add(<span style="color: #00cccc;">OrderByChoice</span>.LastName, currentUser =&gt; currentUser.LastName);
    orderByList.Add(<span style="color: #00cccc;">OrderByChoice</span>.UserName, currentUser =&gt; currentUser.UserName);
  }

<span style="color: #3333ff;">  return </span>orderByList;
}</pre>
<p>That&#8217;s right, I know. Silly, but I think it&#8217;s cool. Now mind you, I could have methods that return expressions instead of the expressions themselves, but I wanted to show the expressions.</p>
<p>And for the method call:</p>
<pre><span style="color: #3333ff;">public static</span> <span style="color: #00cccc;">IList</span>&lt;<span style="color: #00cccc;">User</span>&gt;<span style="color: #00cccc;"> </span>GetUserList(<span style="color: #00cccc;">OrderByChoice </span>orderBy)
{
<span style="color: #00cccc;">  <span style="color: #3333ff;">return </span></span>GetUserList(currentUser =&gt; <span style="color: #3333ff;">true</span>, GetOrderByList()[orderBy]).ToList();
}</pre>
<p>And now you have an even more dynamicish call.</p>
<p>I FORGOT THE USINGS!!!!</p>
<pre> <span style="color: #3333ff;">using </span>System;
 <span style="color: #3333ff;">using </span>System.Collections.Generic;
 <span style="color: #3333ff;">using </span>System.Linq;
 <span style="color: #3333ff;">using </span>System.Linq.Expressions;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/im-starting-to-worry-about-myself/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

