<?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>Progressive Digressive &#187; Development</title>
	<atom:link href="http://www.marcuswhitworth.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marcuswhitworth.com</link>
	<description>Marcus Whitworth&#039;s tech blog – .NET, C#, Silverlight, WPF, Flex…etc, etc</description>
	<lastBuildDate>Sat, 06 Feb 2010 09:50:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Dynamic Linq with Expression Trees</title>
		<link>http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/</link>
		<comments>http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 19:28:16 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[expression trees]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://www.marcuswhitworth.com/?p=106</guid>
		<description><![CDATA[I recently came across a project requirement for the client to create custom validation rules for retrieving data from the SQL database. The rules needed be applied against any number of member applications, and can be altered by the client at any point. From the client perspective, the administration of the rules would look something [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across a project requirement for the client to create custom validation rules for retrieving data from the SQL database.  The rules needed be applied against any number of member applications, and can be altered by the client at any point.</p>
<p>From the client perspective, the administration of the rules would look something like the following:</p>
<p><img class="aligncenter size-full wp-image-110" title="Rules Administration" src="http://www.marcuswhitworth.com/wp-content/uploads/2009/12/Screen-shot-2009-12-02-at-15.19.47.png" alt="Rules Administration" width="477" height="125" /></p>
<p>Simple enough concept.  Historically, given such a requirement, you'd likely end up with some fudged SQL string (if querying a database), and/or a whole lot of conditional logic.  The application already uses Linq2SQL, so I didn't want to go backwards by introducing some extensive SQL string generation.</p>
<p>The first Linq based option I came across was the <a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" target="_blank">Dynamic Linq Query Library</a>, which essentially enables you to use SQL-like strings as expressions in your conditional clauses.  Nice, but it still smelled a bit like the SQL string concatenation of old.</p>
<p>The second option I looked at was <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" target="_blank">PredicateBuilder</a>.  It's a neat solution for the right problem, but it assumes that you know the properties/columns you're querying against at compile time.</p>
<p>So, I decided to have a play around with Expression Trees, in an effort to dynamically generate my conditional expressions at runtime.  I hadn't really delved much into this area before, so I figured it'd be a good learning experience, even if it didn't end up working out.  To cut to the chase, the method I ended up with for parsing my rules into Lambda Expressions looks like the following:</p>
<pre  class="brush:csharp">public static Expression&lt;Func&lt;T, bool&gt;&gt; GetExpression&lt;T&gt;(
	string propertyName, string operatorType, string propertyValue)
{
	var isNegated = operatorType.StartsWith("!");
	if (isNegated)
		operatorType = operatorType.Substring(1);

	var parameter = Expression.Parameter(typeof (T), "type");
	var property = Expression.Property(parameter, propertyName);

	// Cast propertyValue to correct property type
	var td = TypeDescriptor.GetConverter(property.Type);
	var constantValue = Expression.Constant(td.ConvertFromString(propertyValue), property.Type);

	// Check if specified method is an Expression member
	var operatorMethod = typeof(Expression).GetMethod(operatorType, new[] { typeof(MemberExpression), typeof(ConstantExpression) });

	Expression expression;

	if (operatorMethod == null)
	{
		// Execute against type members
		var method = property.Type.GetMethod(operatorType, new[] {property.Type});
		expression = Expression.Call(property, method, constantValue);
	}
	else
	{
		// Execute the passed operator method (e.g. Expression.GreaterThan)
		expression = (Expression) operatorMethod.Invoke(null, new object[] {property, constantValue});
	}

	if (isNegated)
		expression = Expression.Not(expression);

	return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(expression, parameter);
}</pre>
<p>Which can be executed with a call like the following:</p>
<pre class="brush:csharp">foreach (var rule in rules)
{
	applicants = applicants.Where(
		GetExpression&lt;Applicant&gt;(
			rule.MemberName, // The Applicant property
			rule.Operator, // Equals, Contains, GreaterThan, etc
			rule.Value)
		);
}</pre>
<p>The rules are looped over, and Linq does its magic by combining all the separate rules with AND statements in the resulting query.</p>
<p>A few things to note:</p>
<ul>
<li>Operators are just the method names to be called, against the Expression type, or any other CLR or custom type.  If you put a ! in front of the operator, it negates the method result (!Contains would read 'Does Not Contain').</li>
<li>Reflection isn't necessary for building an expression, I just use it here to enable runtime operator implementation, against both the Expression class and potentially any other class.</li>
<li>If you wanted to combine your rules with OR statements, it could be done easily enough with logic similar to what PredicateBuilder uses.</li>
<li>There is possibly/probably better ways to do this - if you know one, speak up!</li>
</ul>
<p>Is this necessarily any better than using the Dynamic Linq library?  A bit - it doesn't really give me any compile-time type safety benefits, but I could potentially handle certain exceptions that I perhaps wouldn't be able to detect using Dynamic Linq, such as casting the value to the correct type.  On top of that, I like it more, it <em>feels</em> more robust, and doesn't require external libraries to work.  If nothing else, it gave me an excuse to get my head around some lower-level Expression Tree stuff :)</p>
		<br />
		<a href="http://www.dotnetkicks.com/kick/?title=Dynamic Linq with Expression Trees&url=http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F12%2Fdynamic-linq-with-expression-trees%2F"> 
		<img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F12%2Fdynamic-linq-with-expression-trees%2F" border="0" alt="Kick It on DotNetKicks.com" /> </a>
		<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=Dynamic+Linq+with+Expression+Trees+www.bit.ly/4LSICB" title="Post to Twitter"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://buzz.yahoo.com/submit?submitUrl=http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/&amp;submitHeadline=Dynamic+Linq+with+Expression+Trees" title="Post to Yahoo Buzz"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-buzz-micro3.png" alt="Post to Yahoo Buzz" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/&amp;title=Dynamic+Linq+with+Expression+Trees" title="Post to Delicious"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/&amp;title=Dynamic+Linq+with+Expression+Trees" title="Post to Digg"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/&amp;title=Dynamic+Linq+with+Expression+Trees" title="Post to Reddit"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a> <a target="_blank" class="tt" href="http://stumbleupon.com/submit?url=http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/&amp;title=Dynamic+Linq+with+Expression+Trees" title="Post to StumbleUpon"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-su-micro3.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ReSharper 5 &#8211; and I thought 4 was good</title>
		<link>http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/</link>
		<comments>http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 12:12:27 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.marcuswhitworth.com/?p=91</guid>
		<description><![CDATA[Reading this preliminary list of the upcoming features of ReSharper 5, it makes me wonder how any .NET developer could ever be without a tool like this.  Among the favorites would have to be Project Refactorings, and Call/Value Tracking.  Genius. Having used the refactoring-and-[insert feature here]-anaemic Flex Builder 3 at length on a few recent [...]]]></description>
			<content:encoded><![CDATA[<p>Reading <a href="http://blogs.jetbrains.com/dotnet/2009/10/resharper-50-overview/" target="_blank">this preliminary list of the upcoming features</a> of ReSharper 5, it makes me wonder how any .NET developer could ever be without a tool like this.  Among the favorites would have to be Project Refactorings, and Call/Value Tracking.  Genius.</p>
<p>Having used the refactoring-and-<em>[insert feature here]</em>-anaemic <a href="http://www.adobe.com/products/flex/features/flex_builder/" target="_blank">Flex Builder 3</a> at length on a few recent projects, life just seems to get better and better in Visual Studio land.</p>
		<br />
		<a href="http://www.dotnetkicks.com/kick/?title=ReSharper 5 &#8211; and I thought 4 was good&url=http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F10%2Fresharper-5-and-i-thought-4-was-good%2F"> 
		<img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F10%2Fresharper-5-and-i-thought-4-was-good%2F" border="0" alt="Kick It on DotNetKicks.com" /> </a>
		<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=ReSharper+5+%E2%80%93+and+I+thought+4+was+good+www.bit.ly/8j6mmq" title="Post to Twitter"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://buzz.yahoo.com/submit?submitUrl=http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/&amp;submitHeadline=ReSharper+5+%E2%80%93+and+I+thought+4+was+good" title="Post to Yahoo Buzz"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-buzz-micro3.png" alt="Post to Yahoo Buzz" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/&amp;title=ReSharper+5+%E2%80%93+and+I+thought+4+was+good" title="Post to Delicious"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/&amp;title=ReSharper+5+%E2%80%93+and+I+thought+4+was+good" title="Post to Digg"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/&amp;title=ReSharper+5+%E2%80%93+and+I+thought+4+was+good" title="Post to Reddit"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a> <a target="_blank" class="tt" href="http://stumbleupon.com/submit?url=http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/&amp;title=ReSharper+5+%E2%80%93+and+I+thought+4+was+good" title="Post to StumbleUpon"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-su-micro3.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.marcuswhitworth.com/2009/10/resharper-5-and-i-thought-4-was-good/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a custom Silverlight 3 Smooth Streaming player</title>
		<link>http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/</link>
		<comments>http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 18:36:39 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://www.marcuswhitworth.com/?p=64</guid>
		<description><![CDATA[When it comes to video delivery, I come from a Flash background.  I've worked on numerous streaming video projects over the years, all of which were created with Flash &#38; Actionscript on the client side. Having been through the process several times, I know all the hurdles I'm going to have to clear well in [...]]]></description>
			<content:encoded><![CDATA[<p>When it comes to video delivery, I come from a Flash background.  I've worked on numerous streaming video projects over the years, all of which were created with Flash &amp; Actionscript on the client side. Having been through the process several times, I know all the hurdles I'm going to have to clear well in advance.</p>
<p>Documentation for coding a Silverlight 3 player against IIS Smooth Streaming is a little sparse.  IIS.net has several articles on the server setup, but I couldn't find anywhere obvious regarding the client connection.</p>
<p>Unlike progressive video playback, you can't just point the MediaElement.source at the video path then call play().  After a bit of searching, <a href="http://chris.59north.com/post/Playing-Smooth-Streaming-videos-in-Silverlight.aspx" target="_blank">most people were talking about</a> some <em>AdaptiveStreamingSource</em> class, which isn't available in the base SL toolkit, but rather only found in <em>SmoothStreaming.dll</em> within the template players generated from Expression Encoder!</p>
<p>Per <a href="http://forums.silverlight.net/forums/t/121952.aspx" target="_blank">some handy forum posts</a>, the steps required are:</p>
<ol>
<li>With Expression Encoder installed, go to <em>C:\Program Files\Microsoft Expression\Encoder 3\Templates\en</em>, select any template, and copy the SmoothStreaming.xap file.</li>
<li>Rename your copied .xap file to .zip, unzip, and take out the <em>SmoothStreaming.dll</em> and <em>PlugInMssCtrl.dll</em> files.</li>
<li>Reference these assemblies in your project, and you can then start using <em>AdaptiveStreamingSource.</em></li>
</ol>
<p>So, once you can finally access the required assemblies, you can then invoke your IIS Smooth Streaming service with something along the lines of the following:</p>
<pre class="brush:csharp">var mediaPath = "testClip_h1080p.ism/manifest";
var source = new AdaptiveStreamingSource
{
   ManifestUrl = new Uri(mediaPath, UriKind.RelativeOrAbsolute),
   MediaElement = streamElement // the xaml MediaElement
};
source.StartPlayback();</pre>
<p>Make sure you put the trailing '/manifest' after your stream path.</p>
<p>Simple enough, once you've figured out the basics!  Not exactly sure what MS were thinking by not including the SmoothStreaming assemblies in the SL3 toolkit?  Surely they realise not everyone wants to use a templated player.  Or have I missed something here?</p>
		<br />
		<a href="http://www.dotnetkicks.com/kick/?title=Creating a custom Silverlight 3 Smooth Streaming player&url=http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F09%2Fcreating-a-custom-silverlight-3-smooth-streaming-player%2F"> 
		<img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F09%2Fcreating-a-custom-silverlight-3-smooth-streaming-player%2F" border="0" alt="Kick It on DotNetKicks.com" /> </a>
		<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=Creating+a+custom+Silverlight+3+Smooth+Streaming+player+www.bit.ly/64Sj2a" title="Post to Twitter"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://buzz.yahoo.com/submit?submitUrl=http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/&amp;submitHeadline=Creating+a+custom+Silverlight+3+Smooth+Streaming+player" title="Post to Yahoo Buzz"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-buzz-micro3.png" alt="Post to Yahoo Buzz" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/&amp;title=Creating+a+custom+Silverlight+3+Smooth+Streaming+player" title="Post to Delicious"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/&amp;title=Creating+a+custom+Silverlight+3+Smooth+Streaming+player" title="Post to Digg"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/&amp;title=Creating+a+custom+Silverlight+3+Smooth+Streaming+player" title="Post to Reddit"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a> <a target="_blank" class="tt" href="http://stumbleupon.com/submit?url=http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/&amp;title=Creating+a+custom+Silverlight+3+Smooth+Streaming+player" title="Post to StumbleUpon"><img class="nothumb" src="http://www.marcuswhitworth.com/wp-content/plugins/tweet-this/icons/tt-su-micro3.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.marcuswhitworth.com/2009/09/creating-a-custom-silverlight-3-smooth-streaming-player/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
