<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-7806605514179331942</atom:id><lastBuildDate>Wed, 11 Nov 2009 07:19:58 +0000</lastBuildDate><title>Ramblings from Rhys</title><description>A .Net blog with ramblings on code and other assorted things from a joe average developer.</description><link>http://rhysc.blogspot.com/</link><managingEditor>noreply@blogger.com (RhysC)</managingEditor><generator>Blogger</generator><openSearch:totalResults>228</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/RamblingsFromRhys" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-314562906599730144</guid><pubDate>Fri, 06 Nov 2009 05:24:00 +0000</pubDate><atom:updated>2009-11-05T21:24:34.976-08:00</atom:updated><title>Functional .Net : Tuple</title><description>&lt;p&gt;Tuple's really don't have a lot to do with functional programming, they are a common concept in many language that for some reason are only making their way in the .Net on version 4.0. One could argue that you could have always easily constructed your own Tuple class but unnecessary duplication of such a simple type has obviously become apparent to the BCL team. This is good. Simple classes like this should be present in the framework. :)&lt;/p&gt;  &lt;p&gt;So what is a Tuple? It is basically a container of a finite list of objects; a Point could be described as a list of 2 values Tuple&amp;lt;int, int&amp;gt; where the int values could be the X and The Y coordinates of a point. A Date could be described as Turple&amp;lt;int,int,int&amp;gt; with the values being Year, Month &amp;amp; Day. It is not a list in that your would typically enumerate through the items but you would reference them by index. You may be ask why is this different to an Array eg int[3]? Well with a Tuple the list length is set at compile time and the type of each index point is also set. Tuple&amp;lt;int,string,DateTime&amp;gt; forces you to always have the DateTime as the 3rd item. This is all pretty under-whelming to be honest, but like anything cool its the simplicity that is its strength and also why it pops up in functional styled programming a lot. Future demos are likely to include Tuples :)&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-314562906599730144?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/11/functional-net-tuple.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-2639667999051972327</guid><pubDate>Thu, 29 Oct 2009 11:26:00 +0000</pubDate><atom:updated>2009-10-29T04:26:03.307-07:00</atom:updated><title>Functional .Net : Currying</title><description>&lt;p&gt;Currying is another functional technique that is possible to achieve with C#. The technique basically allows the rewriting of a function that takes in multiple arguments to one that takes in one argument and returns a function which may in turn take more arguments, the basic premise being able to build up composite function by splitting functions down and reducing the number of parameters dealt with. I will be honest and say that&amp;#160; I have found the language you use (C#, F#, Haskell etc) would be more influential to your predisposition&amp;#160; in using this technique, as it is with many of the function patterns and IMO C# does not lend itself nearly as well as F# for example. That being said it still can be done so lets look at a basic example. For starters currying is something that is not catered for explicitly out the box in C# but can easily be done using extension methods eg:&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;public static Func&amp;lt;TArg1, Func&amp;lt;TArg2, TResult&amp;gt;&amp;gt; Curry&amp;lt;TArg1, TArg2, TResult&amp;gt;(this Func&amp;lt;TArg1, TArg2, TResult&amp;gt; func)&lt;br /&gt;{&lt;br /&gt;    return a1 =&amp;gt; a2 =&amp;gt; func(a1, a2);&lt;br /&gt;}&lt;br /&gt;public static Func&amp;lt;TArg1, Action&amp;lt;TArg2&amp;gt;&amp;gt; Curry&amp;lt;TArg1, TArg2&amp;gt;(this Action&amp;lt;TArg1, TArg2&amp;gt; action)&lt;br /&gt;{&lt;br /&gt;    return a1 =&amp;gt; a2 =&amp;gt; action(a1, a2);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;These extension method now allow you to take a 2 parameter delegate and split it into a one parameter argument return an Action or Func that take one argument. This can obviously be extended and helps facilitate the separation and composition of functions.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now from my understanding of currying it is a specific form of partial application, being that currying splits its functions down to single argument delegates while partial application make no such claim, i.e. a three argument function be be reduced to a single argument function returning a 2 argument delegate. As this is purely academia I don't really care, the principle is the same.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A trivial example of currying (I'm lazy I stole it from Matt P and it is using the extension method above) :&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;Func&amp;lt;int, int, int&amp;gt; multiply = (x, y) =&amp;gt; x * y;&lt;br /&gt;var curriedMultiply = multiply.Curry();&lt;br /&gt;var curriedMultiplyThree = curriedMultiply(3);&lt;br /&gt;var curriedMultiplyResult = curriedMultiplyThree(15);&lt;br /&gt;Console.WriteLine(&amp;quot;Result of 3 * 15 = {0}&amp;quot;, curriedMultiplyResult);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Unfortunately the verbosity of C# when approaching this style of coding very quickly begins to put me off.&amp;#160; The equivalent in F# is much more readable, but hey its what the language is strong at so it really should be a nicer experience. Either way its good to know the facilities are there if one day I do ever need to use it.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Basically the take away from this post is the extension methods at the top, with out these there will be no curry love.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Links forwarding (yeah this was a lazy post):&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a title="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/04/26/functional-c-forward-functional-composition.aspx" href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/04/26/functional-c-forward-functional-composition.aspx"&gt;http://codebetter.com/blogs/matthew.podwysocki/archive/2009/04/26/functional-c-forward-functional-composition.aspx&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://mikehadlow.blogspot.com/2008/03/currying-in-c-with-oliver-sturm.html"&gt;http://mikehadlow.blogspot.com/2008/03/currying-in-c-with-oliver-sturm.html&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a title="http://stackoverflow.com/questions/411572/proper-currying-in-c" href="http://stackoverflow.com/questions/411572/proper-currying-in-c"&gt;http://stackoverflow.com/questions/411572/proper-currying-in-c&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-2639667999051972327?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/10/functional-net-currying.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-5943157083714340029</guid><pubDate>Tue, 13 Oct 2009 16:19:00 +0000</pubDate><atom:updated>2009-10-13T09:19:04.002-07:00</atom:updated><title>Functional .Net : Closures</title><description>&lt;p&gt;One of the more commonly used functional techniques that can be used in C# is the use of Closures, a technique that if your are currently using lambdas, you may be using them inadvertently. My understanding of closure may be different to others as there seems to be so many subtlety different definitions especially when comparing languages. Anyway, in my mind the comments of Javascript closure best align with my understanding (&lt;a title="http://www.jibbering.com/faq/faq_notes/closures.html" href="http://www.jibbering.com/faq/faq_notes/closures.html"&gt;http://www.jibbering.com/faq/faq_notes/closures.html&lt;/a&gt;)&lt;/p&gt;  &lt;p&gt;&lt;em&gt;A closure is a delegate that has references to a variable not passed to it and in an scope outside the delegates immediate scope.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Like any delegate its definition and execution are not the same thing. You can define a closure and never use it or just call it later &lt;/p&gt;  &lt;p&gt;A simple closure example I can think of is:&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;    var timesToRepeat = 100;&lt;br /&gt;    //Declare the Action&lt;br /&gt;    Action&amp;lt;string&amp;gt; print = text =&amp;gt; //text (string) is the only parameter &lt;br /&gt;        {&lt;br /&gt;            //using varb declared outside of Action&lt;br /&gt;            for (int i = 0; i &amp;lt; timesToRepeat; i++)&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine(text);&lt;br /&gt;            }&lt;br /&gt;        };&lt;br /&gt;    timesToRepeat = 3;//Lets modify the variable &lt;br /&gt;    print(&amp;quot;Hello!&amp;quot;);//Call the action/evaluate the expression&lt;br /&gt;    //Prints:    &lt;br /&gt;    //Hello!    &lt;br /&gt;    //Hello!    &lt;br /&gt;    //Hello!&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Note that the timeToRepeat variable is declared outside of the declaration of the lambda statement. Think about this; the Action 'print' can be passed out side of this scope, it could be passed to another class which does not have visibility of the locally declared variable. The 'print' expression is bound to that variable declared outside of its scope. This obviously has ramification in terms of holding reference to that object. Please also note that the expression 'print', like all delegates is evaluated when it is called, not when it is declared; Stepping over the above code will not print when declaring the 'print' Action but at the last line when it is called. One last thing to note is that the variable timetoRepeat is modified after defining the print Action and this is carried when we call 'print' in the last line; &amp;quot;Hello!&amp;quot; is printed 3 times, not 100 times as the variable would imply when the closure was declared.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You may have been using closures with out knowing it. Javascript and the associated libraries like jQuery use this technique a lot, as do many open source library such as TopShelf, MassTransit etc.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-5943157083714340029?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/10/functional-net-closures.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-2420280278426451685</guid><pubDate>Mon, 12 Oct 2009 17:00:00 +0000</pubDate><atom:updated>2009-10-12T10:00:05.467-07:00</atom:updated><title>Functional .Net : First Class Functions</title><description>&lt;p&gt;One thing I notice in .Net is that many developers do not think of functions as first class citizens. I guess in the OO world classes or more appropriate the instance representations are the real hero's, however, in my mind, functions deserve much more appreciation than they perhaps get. &lt;/p&gt;  &lt;p&gt;Since .Net 1.0 delegates have been around and I still think many developers do not fully understand how they work. I have previously made a post with regard to delegates showing how they can be used in a real world way to save code duplication &lt;a href="http://rhysc.blogspot.com/2009/08/aop-with-delegates.html" target="_blank"&gt;here&lt;/a&gt;. I guess one of the first steps to being comfortable with functional programming is being comfortable with functions as first class citizens; The best way for a typical C# developer to do this is get comfortable with delegates. Before I continue on with my Functional programming journey I want others following with me to be on the same page. Please be sure you understand what a method &amp;amp; delegate are; I feel I describe them reasonably well on the previous mentioned &lt;a href="http://rhysc.blogspot.com/2009/08/aop-with-delegates.html" target="_blank"&gt;post&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-2420280278426451685?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/10/functional-net-first-class-functions.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-1315332804976980746</guid><pubDate>Mon, 12 Oct 2009 16:12:00 +0000</pubDate><atom:updated>2009-10-12T09:12:54.775-07:00</atom:updated><title>Functional .Net: The Beginning</title><description>&lt;p&gt;Of late I have (along with a few colleagues and friends) started to make a bit more of a concerted effort to up skill in the area of functional programming. I admit that my knowledge is&amp;#160; of functional programming is high level (at best) although have in advertently been using several of the core concepts due to the language feature I am exposed by the C# language I use on a day to day basis.&lt;/p&gt;  &lt;p&gt;What has spurred me on is the talks from Dr Erik Miejer on channel 9 (the first of 13 can be found &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Lecture-Series-Erik-Meijer-Functional-Programming-Fundamentals-Chapter-1/" target="_blank"&gt;here&lt;/a&gt;). The talks plan on tackling Functional programming by working through the benchmark Functional book: &lt;a href="http://www.cs.nott.ac.uk/%7Egmh/book.html"&gt;Programming in Haskell&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I am keen to see how the series progresses, I am only up to episode 2 but am already seeing value, more in the &amp;quot;why&amp;quot; as opposed to the &amp;quot;how&amp;quot;, which is fine for this early stage of my journey.&lt;/p&gt;  &lt;p&gt;I also want a bit of commercial return on investment with relation to what I can do in my day to day job with functional programming. As I have mentioned C# actually handles several of the Functional paradigms (although perhaps not as elegantly as F# and the like) and thanks to .Net resident Functional voice-to-the-masses, a bunch of Functional programming samples in C# can be found &lt;a href="http://code.msdn.microsoft.com/FunctionalCSharp" target="_blank"&gt;here&lt;/a&gt; to &lt;a href="http://code.msdn.microsoft.com/FunctionalCSharp/Release/ProjectReleases.aspx?ReleaseId=1658" target="_blank"&gt;download&lt;/a&gt;; Cheers &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/default.aspx" target="_blank"&gt;Matt&lt;/a&gt;! Along with just raw C# code he has a bunch of Wiki links to highlight what each example is actually doing; you may be surprised that you are inadvertently using some of these techniques!&lt;/p&gt;  &lt;p&gt;Any way I will keep you posted as to how I progress as I move forward on the beginning on what is hopefully a fruitful journey!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-1315332804976980746?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/10/functional-net-beginning.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-3924693900537197662</guid><pubDate>Thu, 24 Sep 2009 15:08:00 +0000</pubDate><atom:updated>2009-09-24T08:08:28.324-07:00</atom:updated><title>TDD Mind Shift</title><description>&lt;p&gt;Ok so I am not the fastest adopter, however recent situations have almost forced me into a new way of thinking. Recently I was going over a fellow colleagues code and was about to add a test to one of his existing fixtures. He had previously mentioned that he felt there were a lot of tests, I told him not to worry, often when you are new to TDD it seems like a lot of extra code... I had underestimated his comments, he was right there was a ton of tests, thousands upon thousands of lines of tests, in one fixture.&amp;#160; I had clearly not been doing my job and should have been helping him out.&lt;/p&gt;  &lt;p&gt;The test were broken down into one fixture per class under test, using regions to separate out test groupings, typically for each method under test. There was some very basic common set up however there was still a lot of set up in each test. It became quite clear that there were certain common set ups that were occurring.&amp;#160; Although the test per class is common it is usually not the best way of keeping your tests together, especially if you are being thorough in your testing. What my refactoring produced was akin to something I have been following for awhile but not really embraced, context based tests.&amp;#160; The tests were broken up so those with common set ups were grouped in their own fixtures; i.e. those with the same defined stubs. Although in this case it was done to make maintenance easier it highlighted for me the benefits of this approach.&lt;/p&gt;  &lt;p&gt;Fixtures become specific to the context in which they apply, not blindly and solely to the class they are testing. This will lead to lead to multiple test fixtures per class and multiple test fixtures per method. This is what initially turned me off the approach; fixtures would be come to fine grained, however I have changed tact. Although it may not always be appropriate I can see it being beneficial for many situations. For example you may emulate the Repository having no records in it (via a stubbed method call) and run a bunch of test with that context. This encourages you to test not just method calls but to think it terms of a given scenario, something that I see unit sometimes missing in the pursuit of just achieving code coverage. The fixture tend to have test that are very small and very clear as to what they are asserting, often only a couple of lines per test.&lt;/p&gt;  &lt;p&gt;Another thing that originally put me off the notion of Context specification and BDD was the perception of tooling... RSpec etc are not required, it is the thought patterns that I think are more important. Setting up a specification can be done using basic setup method with each fixture defining a specific context. Test inheritance can be very helpful here too.&lt;/p&gt;  &lt;p&gt;Although Context specification and BDD are not the same thing I believe they are a movement in&amp;#160; the same direction; moving away for blind testing to defining scenarios that we need to test. Test become closer to one of the goals of being &amp;quot;readable documentation&amp;quot;.&lt;/p&gt;  &lt;p&gt;If you do what to read up a bit more check out:&lt;/p&gt;  &lt;p&gt;Article introducing BDD styled tests with the notion of Context Specifications : &lt;a href="http://www.code-magazine.com/article.aspx?quickid=0805061&amp;amp;page=1"&gt;http://www.code-magazine.com/article.aspx?quickid=0805061&amp;amp;page=1&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;A nice coding example showing one way of thinking in a context spec way: &lt;a href="http://www.lostechies.com/blogs/rssvihla/archive/2009/05/21/context-spec-style-testing-and-my-approach-to-bdd.aspx"&gt;http://www.lostechies.com/blogs/rssvihla/archive/2009/05/21/context-spec-style-testing-and-my-approach-to-bdd.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;MSpec with Boo looks to be very cool too: (requires git client): &lt;a href="http://github.com/olsonjeffery/machine.specifications.boo"&gt;http://github.com/olsonjeffery/machine.specifications.boo&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Although the shift for me has not been great it has been significant, I would encourage you to at least investigate to consider if some of the principles can be applied in your testing.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-3924693900537197662?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/09/tdd-mind-shift.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-924361269123086707</guid><pubDate>Tue, 15 Sep 2009 13:47:00 +0000</pubDate><atom:updated>2009-09-15T06:47:33.357-07:00</atom:updated><title>When Easier is Better</title><description>&lt;p&gt;Most of the people I currently or have worked with know that I have a strong preference for NHibernate for my persistence mechanism. I typically use a repository pattern, often with services hiding the repositories from the outside world. This is great as I get enterprise scalable domain-driven solutions up and running pretty quick and helps me focus on fixing business problem, not spinning my wheels with infrastructure details. However some time having DTO's, services, repositories, mapping files, anti-corruption/translation layers etc are just overkill. This is where is would typically say &amp;quot;Use Linq2Sql&amp;quot;, if someone asked me what they should use, but its probably not what I would use. I like the idea of having the flexibility of moving from a simple domain to a complex domain with out too much problem. Enter Castle Active Record&lt;/p&gt;  &lt;p&gt;Active record is in no way a new concept (&lt;a href="http://martinfowler.com/eaaCatalog/activeRecord.html" target="_blank"&gt;PEAA p160&lt;/a&gt;) but is heavily under used in the .Net realm. AR is a great pattern when you are fleshing out a domain. You can very quickly start building up relationships have screens up and running for client very quickly. This is great for spikes but also for writing real code. The database can be generated from the code (Castle AR sits on top of NHibernate) so it is a great fix for fast moving agile project, especially in the initial sprints. The thing I like most about it is that if I decide I want a more complex domain, all is not lost; I remove the Castle attributes and references wrap a repository pattern around it and I am done. It really is that easy. All my existing domain unit tests should still pass. In a matter of hours you could switch from a developed 2 tier app to an enterprise ready scalable architecture.&lt;/p&gt;  &lt;p&gt;Basically AR is great if you are lazy (or need results now).&lt;/p&gt;  &lt;p&gt;To prove all of this I plan on presenting Castle AR in an up coming Perth Alt.Net meeting. Originally I had wanted to show NHibernate however I think the progression from AR to NH will help show both of there benefits. This may also allow us to show the limits of both... time will tell.&lt;/p&gt;  &lt;p&gt;As a side note the castle stack (as well as NHibernate) has had a &amp;quot;proper&amp;quot; release this year so if you haven't had a look in a while check it out : &lt;a title="http://www.castleproject.org/castle/download.html" href="http://www.castleproject.org/castle/download.html"&gt;http://www.castleproject.org/castle/download.html&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Links - Articles&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.castleproject.org/ActiveRecord/gettingstarted/index.html"&gt;http://www.castleproject.org/ActiveRecord/gettingstarted/index.html&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/hammett/articles/76697.aspx" target="_blank"&gt;All you wanted to know about Castle ActiveRecord - Part I&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/hammett/articles/76809.aspx" target="_blank"&gt;All you wanted to know about Castle ActiveRecord - Part II&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Links - Videos&lt;/p&gt;  &lt;p&gt;&lt;a href="http://archive.oredev.org/topmenu/video/altnet/ayenderahien.4.71552e2411fa881a5cb800021937.html" target="_blank"&gt;Ayende Rahien - Using Active Record to write less code (Oredev 2008)&lt;/a&gt;&amp;#160;&lt;strong&gt;&amp;lt;&amp;lt; Watch me I'm great!&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.infoq.com/presentations/eini-verissimo-castle-active-record" target="_blank"&gt;Ayende &amp;amp; Hammet - Painless Persistence with Castle ActiveRecord (JAOO)&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-924361269123086707?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/09/when-easier-is-better.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-2741298461797503606</guid><pubDate>Tue, 01 Sep 2009 15:53:00 +0000</pubDate><atom:updated>2009-09-01T08:53:24.788-07:00</atom:updated><title>The Build XML Divorce - Part II</title><description>&lt;p&gt;OK, so as I continue to play with Rake I am very quickly seeing what is happening. I am in effect building up a bunch of reusable scripts that can manage task dependencies but really are just orchestrating other applications and system admin tasks. The syntax is nice, Ruby is a very clean fluent&amp;#160; language, however it is becoming abundantly clear that all I am really doing I reorganising my build flow from PowerShell and MSBuild to Rake and MSBuild... It quickly dawns on me that I probably have not understood how &lt;a href="http://codebetter.com/blogs/james.kovacs/archive/2008/06/27/introducing-psake.aspx" target="_blank"&gt;PSake&lt;/a&gt; really works. I have briefly looked into PSake only because James Kovacs (who I hold in very high regard) was the author. I quickly pushed it to the back of my to do list as it look more like a pet project that's only intention was to add to the variants of Make. The problem was I didn't really understand what it was supposed to do. At the time, in my mind all PSake provided was a means to have dependant task hierarchy written in PowerShell... that's it... buts that's all it needs to be! It &lt;em&gt;&lt;strong&gt;should&lt;/strong&gt;&lt;/em&gt; be calling out to MSBuild or csc.exe to build assemblies, it &lt;strong&gt;&lt;em&gt;should&lt;/em&gt;&lt;/strong&gt; be calling out to your test runners and analysis tools. The (R/B/M/Ps)ake tool is (IMCO) just a way to facilitate tasks and control their dependencies.&lt;/p&gt;  &lt;p&gt;Ok so why the big rant? Well it was becoming obvious to me that what I was trying to do things in Rake last night were things that I could easily do in PowerShell. Not only easily but arguably much more appropriate to be done in PowerShell; things like file and directory manipulations. My build process is really pretty basic and can been done completely in an XML based tool like Nant or MSBuild. Its what I do after the most rudimentary clean/build/test that requires a bit more muscle, this is where I have been using PowerShell any way, so using PSake just makes sense. PSake is just PowerShell with a nice clean API to declare tasks with dependencies. Anything you can do in PowerShell you can do in PSake.&lt;/p&gt;  &lt;p&gt;This is good news. So the next step is to refactor my PowerShell bootstrapper scripts into PSake tasks, pull some of my MSBuild task into PSake task and keep the MSBuild file down to the bare minimum that MSBuild does well.. namely: Build. One thing that I would have thought 6 months ago was that Rake/Ruby would soooo much cleaner sexier code... but no, I actually think the PowerShell code is very nice and very well suited to these types of task. Sure its got a few bugs to iron out, but my affection for PowerShell continues.&lt;/p&gt;  &lt;p&gt;Sorry Rake, its been a fun 3 days, but its over... its not you, its me.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-2741298461797503606?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/09/build-xml-divorce-part-ii.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-8486196149219812111</guid><pubDate>Sun, 30 Aug 2009 15:33:00 +0000</pubDate><atom:updated>2009-08-30T08:38:17.452-07:00</atom:updated><title>The Build XML Divorce</title><description>&lt;p&gt;Like many .Net Dev's is have been using NAnt and MSBuild a lot of the last few year to speed up my own local build and to create a suite of task for my build server to do when I check code in. If you have been doing the same I am sure you will run into issues as soon as you surpass the most basic of clean-&amp;gt;build-&amp;gt;test-&amp;gt;analyse type scripts. For some reason I like to build deployable versioned&amp;#160; packages for each environment when I deploy to Test. We only deploy to test every day or 2 and I want to know that version x on Test will have the exact same complied code as version x on UAT and Prod... it sounds obvious, however its no surprise that this is not always the case in may software departments. Having these versioned packages ready to go also means when it is time to push to UAt or prod it a matter of seconds before they could be live (not hours or days like some places i have worked at). Doing this more detailed versioned pre-deployment packaging meant my XML based build soon became messy and I turned to PowerShell to bootstrap some of the processes and loop thru things like swapping out config's etc. This is fine but it was becoming a little confusing&amp;#160; for the other Dev's who had not been as involved in the process as any of us would have liked (especially as a bat file was kicking the whole thing off for local builds). It also means they now have to know MSBuild and PowerShell...&lt;/p&gt;  &lt;p&gt;Ok so this blog post is not going to be anything ground breaking for those out there that are au fait with the ruby community, however I have had an itch to check out (properly) rake for a while now. I have finally got a home project that I am sinking my teeth into and I thought this is a great opportunity to bring rake in to the folds.. finally! &lt;/p&gt;  &lt;p&gt;Right so a quick brief on &lt;a href="http://rake.rubyforge.org/" target="_blank"&gt;Rake&lt;/a&gt;: Its loosely based on Make, Its a build tool written in Ruby, its much cleaner than the XML based options &amp;amp; you are writing real code, so you can do what you want (including loops; which in ruby is oh-so-clean)!&lt;/p&gt;  &lt;p&gt;here is a super simple skeleton rakefile.rb script below. The rake file should be somewhere&amp;#160; in your solution directory structure. Just calling rake from the cmd line in this directory will call the default task.&lt;/p&gt;  &lt;pre class="rb" name="code"&gt;task :default =&amp;gt; [&amp;quot;build:test&amp;quot;]&lt;br /&gt;&lt;br /&gt;namespace :build do&lt;br /&gt; desc &amp;quot;Clean Solution&amp;quot;&lt;br /&gt; task :clean do&lt;br /&gt;   puts &amp;quot;Cleaning...&amp;quot;&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; desc &amp;quot;Build Solution&amp;quot;&lt;br /&gt; task :buildsln =&amp;gt; :clean do&lt;br /&gt;   puts &amp;quot;Building...&amp;quot;&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt;desc &amp;quot;Test Solution&amp;quot;&lt;br /&gt; task :test =&amp;gt; :buildsln do&lt;br /&gt;   puts &amp;quot;Testing...&amp;quot;&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;namespace :deploy do&lt;br /&gt; desc &amp;quot;Publish Soln&amp;quot;&lt;br /&gt; task :publish do&lt;br /&gt;   puts &amp;quot;Publishing...&amp;quot;&lt;br /&gt; end&lt;br /&gt;end&lt;/pre&gt;&lt;p&gt;So say this is in &amp;quot;c:\rhysc\rakefile.rb&amp;quot;, I just open a cmd window change the dir to &amp;quot;c:\rhysc&amp;quot; and type rake and the follow will be printed:&lt;/p&gt;&lt;pre class="rb" name="code"&gt;Cleaning...&lt;br /&gt;Building...&lt;br /&gt;Testing...&lt;/pre&gt;&lt;p&gt;Right so lets look at the above rakefile.rb document:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;First we define our default task, the thing that will run if the rake command is not given any parameters. This is saying the Test task in the build namespace is the default task to run.&lt;/li&gt;&lt;li&gt;next we define a namespace (build); this is standard ruby&lt;/li&gt;&lt;li&gt;next we document our task. If we type &amp;quot;rake -T&amp;quot; we get to see the list of available task with their descriptions. Personally I think this is fantastic&lt;/li&gt;&lt;li&gt;next we define a task! these task are pretty silly as they only print to the console what they should be doing but it helps show the basic structure&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Note the build and test task have the =&amp;gt; notation. this show dependencies i.e. test depends on build which depend son clean; so calling test will mean the tasks that are run (in order) are clean, build then test&lt;/p&gt;&lt;p&gt;Also note that we have quote marks in the default dependency ([&amp;quot;build:test&amp;quot;]). My knowledge of ruby is poor at best (i'll get there!), but for some reason this is required when using a namespace. If test was not in a name space the line could read: &lt;/p&gt;&lt;pre class="rb" name="code"&gt;task :default =&amp;gt; [:test]&lt;/pre&gt;&lt;p&gt;To call the publish task with all the build tasks we would just call :&lt;/p&gt;&lt;pre class="rb" name="code"&gt;rake &amp;quot;build:test&amp;quot; &amp;quot;deploy:publish&amp;quot;&lt;/pre&gt;&lt;p&gt;Clearly this is a very light taste of what rake does. I intend on posting more scripts as I continue to build real scripts* to incorporate into my code base however for now the link below may be a good starting place... as well as reading the &lt;a href="http://rake.rubyforge.org/" target="_blank"&gt;doc's&lt;/a&gt;...&amp;#160; I'm so looking forward to losing this XML bride ;)&lt;/p&gt;&lt;p&gt;Links: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Great place to start- &lt;a title="http://railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial" href="http://railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial"&gt;http://railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Some posts by Tobin Harris (.Net) - &lt;a title="http://www.tobinharris.com/past/tags/rake" href="http://www.tobinharris.com/past/tags/rake"&gt;http://www.tobinharris.com/past/tags/rake&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Derick Bailey chimes in (.Net) - &lt;a title="http://www.lostechies.com/blogs/derickbailey/archive/2009/04/08/how-a-net-developer-learned-ruby-and-rake-to-build-net-apps-in-windows.aspx" href="http://www.lostechies.com/blogs/derickbailey/archive/2009/04/08/how-a-net-developer-learned-ruby-and-rake-to-build-net-apps-in-windows.aspx"&gt;http://www.lostechies.com/blogs/derickbailey/archive/2009/04/08/how-a-net-developer-learned-ruby-and-rake-to-build-net-apps-in-windows.aspx&lt;/a&gt;&lt;/li&gt;&lt;li&gt;and the Alt.Net godfather's posts - &lt;a title="http://codebetter.com/blogs/david_laribee/archive/2008/08/25/omg-rake.aspx" href="http://codebetter.com/blogs/david_laribee/archive/2008/08/25/omg-rake.aspx"&gt;http://codebetter.com/blogs/david_laribee/archive/2008/08/25/omg-rake.aspx&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;(you obviously need Ruby installed.. its &lt;a href="http://rubyinstaller.org/" target="_blank"&gt;click once&lt;/a&gt; so its pretty painless)&lt;/p&gt;&lt;p&gt;*don't worry work colleagues; I don't intend on inflicting this onto you... yet.. we'll keep to our Bat File/MSBuild/PS cocktail for now ;)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-8486196149219812111?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/08/build-xml-divorce.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-1253340926972114154</guid><pubDate>Thu, 27 Aug 2009 08:28:00 +0000</pubDate><atom:updated>2009-08-27T01:33:55.565-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Rx</category><category domain="http://www.blogger.com/atom/ns#">events</category><category domain="http://www.blogger.com/atom/ns#">.net 4</category><title>.Net - Rx</title><description>Rx in .Net 4.0 is looking pretty sweet. It also tackles something that has bugged me for quite awhile: the notion of having to explicitly unhook from an event...&lt;br /&gt;&lt;br /&gt;It basically is Linq to events, for more info see some cool stuff here:&lt;br /&gt;&lt;a href="http://www.infoq.com/news/2009/07/Reactive-Framework-LINQ-Events"&gt;InfoQ Article&lt;/a&gt; and Jafar Husains blog posts &lt;a href="http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html"&gt;here&lt;/a&gt; and &lt;a href="http://themechanicalbride.blogspot.com/2009/07/developing-with-rx-part-1-extension.html"&gt;here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-1253340926972114154?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/08/net-rx.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-8765310997100777775</guid><pubDate>Sat, 15 Aug 2009 10:19:00 +0000</pubDate><atom:updated>2009-08-15T03:19:48.249-07:00</atom:updated><title>AOP with Delegates</title><description>&lt;p&gt;In the past I have made mentions of the notion of &lt;a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_blank"&gt;Aspect Orientated Programming&lt;/a&gt; (AOP) in regards to reducing the noise that can occur when cross cutting concerns, like logging, invade business logic. Unfortunately most of the posts I have made have been in reference to tools and the assistance they can offer. Such tools like &lt;a href="http://www.postsharp.org/" target="_blank"&gt;PostSharp&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/dd140045.aspx" target="_blank"&gt;Unity&lt;/a&gt;, &lt;a href="http://www.castleproject.org/container/documentation/v1rc3/usersguide/interceptors.html" target="_blank"&gt;Castle&lt;/a&gt; etc provide some &amp;quot;magic&amp;quot; to eliminate the code clutter. Unfortunately many of the people I talk just do not implement tools like this at the place they work and want a &lt;a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object" target="_blank"&gt;POCO&lt;/a&gt; option to deliver such results. Well this is actually simpler than many people realise and also points to the issue of the misunderstanding of delegates, anonymous methods and lambdas ; as well as the huge amount of code reuse they can provide.&lt;/p&gt;  &lt;p&gt;Firstly I will show an example of &amp;#8220;typical&amp;#8221; business code that has a lot of business noise. Secondly the code will be show how it could be written if we were use AOP and later on a clean version that mixes POCO with other forms of AOP &lt;/p&gt;  &lt;pre class="c#" name="code"&gt;class AopEnabledSampleService : ITransferable //from &lt;a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming#Motivation_and_basic_concepts" target="_blank"&gt;Wiki&lt;/a&gt;&lt;br /&gt;{&lt;br /&gt;    void Transfer(Account fromAcc, Account toAcc, int amount)&lt;br /&gt;    {&lt;br /&gt;        if (fromAcc.getBalance() &amp;lt; amount)&lt;br /&gt;        {&lt;br /&gt;            throw new InsufficientFundsException();&lt;br /&gt;        }&lt;br /&gt;        fromAcc.withdraw(amount);&lt;br /&gt;        toAcc.deposit(amount);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;class NoAopSampleService : ITransferable&lt;br /&gt;{&lt;br /&gt;    private string OP_TRANSFER = &amp;quot;Transfer&amp;quot;;&lt;br /&gt;    private Database database = new Database();&lt;br /&gt;    private Logger systemLog;&lt;br /&gt;&lt;br /&gt;    void Transfer(Account fromAccount, Account toAccount, int amount)&lt;br /&gt;    {&lt;br /&gt;        if (!getCurrentUser().canPerform(OP_TRANSFER))&lt;br /&gt;        {&lt;br /&gt;            throw new SecurityException();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if (amount &amp;lt; 0)&lt;br /&gt;        {&lt;br /&gt;            throw new NegativeTransferException();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if (fromAccount.getBalance() &amp;lt; amount)&lt;br /&gt;        {&lt;br /&gt;            throw new InsufficientFundsException();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        Transaction tx = database.newTransaction();&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            fromAccount.withdraw(amount);&lt;br /&gt;            toAccount.deposit(amount);&lt;br /&gt;&lt;br /&gt;            tx.commit();&lt;br /&gt;            systemLog.logOperation(OP_TRANSFER, fromAccount, toAccount, amount);&lt;br /&gt;        }&lt;br /&gt;        catch (Exception e)&lt;br /&gt;        {&lt;br /&gt;            tx.rollback();&lt;br /&gt;            throw e;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    //...more code&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;It is quite clear that the AOP code is much cleaner to look at however there is a lot that is potentially happening that we do not know about. You have to know that the AOP injection or interception is catering for all of the things that the second example dealt with. This is a fundamental problem with AOP: it is not explicit. This obviously can make it very hard to debug and can be confusing to the developer maintaining the code. One way you can get around this by marking up methods or classes with attributes; this at least gives the user of the code a hint as to what is going on. Many of the AOP providers allow for this. However sometime you are just shifting the noise from inside the method to an attribute. How you deal with this is up to you and your team, however I will later on offer some ideas how to manage this.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;What the purpose of this post was is to show how we can achieve the functionality of the verbose code above with reduced noise, yet still be maintainable and somewhat explicit. What we will eventually be using is lambdas to achieve the same functionality. Many .Net Dev's use lambdas on a semi regular basis but many do not know how to write a basic API that uses them or even what is really going on when they are using a lambda. Bare with me now while we have a code school moment and cover methods, delegates, anonymous methods, lambdas (closures will be covered in another post). If you are comfortable with all of these then I don't really know why you are reading this post, you should know how to solve this problem already.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Method&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Right we all know what a method is; its a function, something that does something, typically a command or a query. You can pass in parameters and you can get something back from a method. The way we typically use a method is in the named sense i.e. 5.ToString(); we are calling the ToString Method on the integer object 5. The name of the method is &amp;#8220;ToString&amp;#8221; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Delegate&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A delegate is to a method what a class is to an object. A class defines an object as a delegate defines a method. Typically most code will never need to define a delegate for a given method unless it is passing the method around like an object... read that again; you can pass methods around like objects. This is where delegates become powerful and this is where the notion of delegates is often misunderstood and often not even known! We will cover more of this later... but for now here is how you define a delegate and what a method would look like that adheres to a delegate.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;public class UsingDelegates&lt;br /&gt;{&lt;br /&gt;    public delegate void MyDelegate();&lt;br /&gt;&lt;br /&gt;    public void Main()&lt;br /&gt;    {&lt;br /&gt;        UseADelegate(this.MyMethod);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void MyMethod()&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;quot;This is My Method!&amp;quot;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void UseADelegate(MyDelegate myDelegate)&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;quot;Before using my delegate&amp;quot;);&lt;br /&gt;        myDelegate();&lt;br /&gt;        Console.WriteLine(&amp;quot;After using my delegate&amp;quot;);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;/*Output is:&lt;br /&gt;Before using my delegate&lt;br /&gt;This is My Method!&lt;br /&gt;After using my delegate*/&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In this code we expose the public method Main which then calls the UseADelegate method passing in &lt;em&gt;&lt;strong&gt;the address&lt;/strong&gt;&lt;/em&gt; of the MyMethod method. Note that the parameter passed in to the UseADelegate method does not have the typical parenthesis associated with the method, that is because we want to pass the method as a delegate, not the returned value of the method; This is hugely significant. You will also notice that the UseADelagate method takes in a variable of type MyDelegate. We have defined MyDelegate as a delegate at the start of the class. When you define a delegate you are defining &lt;em&gt;&lt;strong&gt;a signature&lt;/strong&gt;&lt;/em&gt; of a method. The name does not matter (except for readability), the only things that matters are A) whatever can use it must be able to access it (an appropriate accessor) and B) the return type and parameter types are consistent with the methods that you intend to use as the delegate. To me this is similar to classes using interfaces, you don't care what the name of the classes that implements the interface is it just has to implement what the interface says to implement. Delegates are similar, however they are not explicit. A method does not says it implements a delegate in the same way a class says it implements an interface. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The syntax for defining a delegate is &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;p&gt;[accessor] delegate [return type] [Custom Delegate Name] ([parameter list]);&lt;/p&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;e.g. public delegate List&amp;lt;Customers&amp;gt; CustomerFilterDelegate(string filter); &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now any method that returns a list of customers and takes in one string parameter is compliant with this delegate. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Right, now that I have told how to define a delegate I am going to throw a spanner in the works and tell you to never do so... sorry. The reason is that now .Net has given us reusable delegates in the form of Func&amp;lt;&amp;gt; and Action&amp;lt;&amp;gt;. Action specifies a delegate with a return type of void so each of its generic parameters are indicators to the parameters in its signature it is defining. Func is used the same however the last generic argument is the return type.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can now define any reasonable delegate signature with these two generic delegate types. For example the delegate we defined above would now be used as Func&amp;lt;string,List&amp;lt;Customers&amp;gt;&amp;gt; instead of CustomerFilterDelegate. See Framework Design Guidelines for &lt;a href="http://blogs.msdn.com/brada/archive/2009/01/26/framework-design-guidelines-avoiding-custom-delegates.aspx" target="_blank"&gt;more info&lt;/a&gt;. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;example of the above code rewritten to be guideline compliant&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;public class UsingDelegatesCorrectly&lt;br /&gt;{&lt;br /&gt;    public void Main()&lt;br /&gt;    {&lt;br /&gt;        UseAnAction(this.MyMethod);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void MyMethod()&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;quot;This is My Method!&amp;quot;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void UseAnAction(Action myDelegate)&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;quot;Before using my delegate&amp;quot;);&lt;br /&gt;        myDelegate();&lt;br /&gt;        Console.WriteLine(&amp;quot;After using my delegate&amp;quot;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Anonymous Delegates&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;An anonymous delegate is a method without a name, i.e. it has a body but no name... hmm. As we have mentioned the name of a method has no relevance to whether it adheres to a delegate definition, it is its signature that counts. Previously we were only using the method name as a effective pointer for the address body. What many people don&amp;#8217;t know is that you can create a method body &lt;em&gt;&lt;strong&gt;without&lt;/strong&gt;&lt;/em&gt; a name, commonly known as &amp;#8220;anonymous methods&amp;#8221;, &amp;#8220;anonymous delegate&amp;#8221; or &amp;#8220;inline methods&amp;#8221; e.g.:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;Action myDelegate = delegate()&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;#8221;Hello, World!&amp;#8221;);&lt;br /&gt;    };&lt;br /&gt;myDelegate();//writes &amp;#8220;Hello, World!&amp;#8221; to the console&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can use an anonymous delegate anywhere you would typically use a named delegate, however you define the method at the point you wish to use it. The syntax for defining and anonymous delegate is &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;p&gt;var x = delegate([parameter list]){[body of method including the return statement]};&lt;/p&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Note that the return type is not declared, it is inferred by the presence and type of the return value in the body of the anonymous method. If there is no return value the delegate is considered to have a return type of void. Below we show how the code above would have been written using anonymous delegates:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;public class UsingAnonymousDelegates&lt;br /&gt;{&lt;br /&gt;    public void Main()&lt;br /&gt;    {&lt;br /&gt;        UseADelegate(delegate()&lt;br /&gt;                         { Console.WriteLine(&amp;quot;This is My Method!&amp;quot;); }&lt;br /&gt;                     );&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void UseADelegate(Action myDelegate)&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;quot;Before using my delegate&amp;quot;);&lt;br /&gt;        myDelegate();&lt;br /&gt;        Console.WriteLine(&amp;quot;After using my delegate&amp;quot;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This case show that we do not have to define a delegate signature (the .Net built in Action type is suitable) and we do not even need to create a named method! &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Lambdas&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Anonymous delegates were great when they came out, it saved a lot of code rewriting and promoted better code reuse; however it was ugly. The majority of the signature still had to be declared and worst of all it was reasonably easy to write anonymous delegates but almost impossible to read them, making maintenance a PITA. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Introducing Lambdas: &lt;em&gt;&lt;strong&gt;Lambdas are exactly the same as anonymous delegates in functionality&lt;/strong&gt;&lt;/em&gt; however they have a very different and more readable syntax. Lambdas basically allow the writer of the code to infer a lot about the method signature without explicitly doing so. The reason this can be done is because often the signature is already defined so the lambda can make use of it. Enough chat, lets see what the previous anonymous delegate would look like as a lambda:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;Action myDelegate = () =&amp;gt; Console.WriteLine(&amp;quot;This is My Method!&amp;quot;);&lt;br /&gt;myDelegate();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Ok so not a huge difference; we have dropped the key word &amp;quot;delegate&amp;quot; and added an arrow looking thing. Perhaps I should show something a little more complex. Firstly lets define a more realistic anonymous delegate using the method from the first example:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;Action&amp;lt;Account, Account, int&amp;gt; transfer = delegate(Account fromAccount, Account toAccount, int amount)&lt;br /&gt;                                      {&lt;br /&gt;                                          if (fromAccount.getBalance() &amp;lt; amount)&lt;br /&gt;                                          {&lt;br /&gt;                                             throw new InsufficientFundsException();&lt;br /&gt;                                          }&lt;br /&gt;                                          fromAccount.withdraw(amount);&lt;br /&gt;                                          toAccount.deposit(amount);&lt;br /&gt;                                       };&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;as a lambda:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;Action&amp;lt;Account, Account, int&amp;gt; transfer = (fromAccount,toAccount, amount) =&amp;gt;&lt;br /&gt;                                         {&lt;br /&gt;                                              if (fromAccount.getBalance() &amp;lt; amount)&lt;br /&gt;                                              {&lt;br /&gt;                                                  throw new InsufficientFundsException();&lt;br /&gt;                                              }&lt;br /&gt;                                              fromAccount.withdraw(amount);&lt;br /&gt;                                              toAccount.deposit(amount);&lt;br /&gt;                                          };&lt;br /&gt;            &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As you can see the method body is the same, it is just the definition of the parameters that is different and that is because the types are inferred. Again this may not seem like much at the moment but the heavily reduced noise has allowed for much more readable framework usage. I would hate to think how my current test would look in RhinoMocks if I was not using lambdas!&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Couple of things I should mention:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;when using a lambda expression that takes in no parameters use the empty parameters to signal this is the case e.g. ()=&amp;gt;//method body&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;if the method body is a one liner you do not need the curly brackets{}, but you do if there is more than one line!&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;You do not need the parameter brackets when defining the parameter name if there is only one parameter, you do if there is more than one&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;if the return statement is a single statement without curly braces you do not even need the return key word!&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;(a) =&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;bob&amp;quot;;&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;can be written as&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;a =&amp;gt; &amp;quot;bob&amp;quot;;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Just to keep things consistent here is the Console.WriteLine example using lambdas:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;public class UsingLambdas&lt;br /&gt;{&lt;br /&gt;    public void Main()&lt;br /&gt;    {&lt;br /&gt;        UseADelegate(&lt;br /&gt;            () =&amp;gt;&lt;br /&gt;                Console.WriteLine(&amp;quot;This is My Method!&amp;quot;)&lt;br /&gt;             );&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private void UseADelegate(Action myDelegate)&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&amp;quot;Before using my delegate&amp;quot;);&lt;br /&gt;        myDelegate();&lt;br /&gt;        Console.WriteLine(&amp;quot;After using my delegate&amp;quot;);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Using Delegation to Achieve AOP-like Coding&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Alright, the whole point to this post was to show how you can use plain .net without any other libraries to do AOP like activities. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Firstly using Lambdas is not as clean as interception, but it is a lot cleaner than copy and paste (right click inheritance) I see so often. I want to help create better code too so here are some thought on where to use AOP and where to use delegation:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Use delegation when you want to be specific and and explicit about your intentions (e.g. transactions) &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Use interception/injection based AOP for things are are truly behind the scenes (e.g. logging) &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Use attribute based (i.e. explicit) AOP when you want the developer maintaining your code to know that some aspect is taken care of (e.g. security) but you do not want it polluting the method body&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Below is an example of what the first example could look like if using a combination of lambdas and AOP:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;public class SampleService : BaseService, ITransferable&lt;br /&gt;{&lt;br /&gt;    [SecurityCheck]&lt;br /&gt;    void Transfer(Account fromAcc, Account toAcc, int amount)&lt;br /&gt;    {&lt;br /&gt;        TransactionWrapper(() =&amp;gt;&lt;br /&gt;                    {&lt;br /&gt;                        if (fromAcc.getBalance() &amp;lt; amount)&lt;br /&gt;                        {&lt;br /&gt;                            throw new InsufficientFundsException();&lt;br /&gt;                        }&lt;br /&gt;&lt;br /&gt;                        fromAcc.withdraw(amount);&lt;br /&gt;                        toAcc.deposit(amount);&lt;br /&gt;                    });&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;//BASE CLASS&lt;br /&gt;internal abstract class BaseService &lt;br /&gt;{&lt;br /&gt;    protected void TransactionWrapper(Action wrappedDelgate)&lt;br /&gt;    {&lt;br /&gt;        Transaction tx = database.newTransaction();&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            wrappedDelgate();&lt;br /&gt;            tx.commit();&lt;br /&gt;        }&lt;br /&gt;        catch (Exception e)&lt;br /&gt;        {&lt;br /&gt;            tx.rollback();&lt;br /&gt;            throw e;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Note &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;The logging is no where to be seen. I personally hate seeing logging code, it should be hidden away. To me it is pure noise. This would have been taken care of by the AOP framework of choice. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Security is kept a subtle as possible without leaving it off the radar. This is not always possible but if I can I keep it out of the method body and as an attribute. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;The transaction is dealt with by a separate method that takes in a delegate. This method can now be reused allowing any other method to take advantage of the pre existing transaction handling. this can now be pushed into a base class, or if a standard .net transaction is being created, a static method that anything can use.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Personally I like the last example the most. However to implement this it does require a reasonably detailed knowledge of AOP so interception can be done&amp;#160; using attributes or not and it does require a basic understanding of delegation. Hopefully this post has helped with the later. Next time you start to see repeated code in your code base think if you could use delegation to clean your code up and star making it more reusable.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Rhys&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-8765310997100777775?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/08/aop-with-delegates.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-2900720820341012263</guid><pubDate>Tue, 11 Aug 2009 00:39:00 +0000</pubDate><atom:updated>2009-08-10T17:48:24.169-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Dependency Injection</category><category domain="http://www.blogger.com/atom/ns#">Best Practices</category><category domain="http://www.blogger.com/atom/ns#">Design By Contract</category><title>Explicit interfaces</title><description>Further to our teams discussions with Greg Fox and following on from &lt;a href="http://abstractcode.com/abstractblog/"&gt;Colin Scotts&lt;/a&gt; &lt;a href="http://abstractcode.com/abstractblog/archive/2009/07/09/173.aspx"&gt;blog post&lt;/a&gt; I thought i would  highlight this:&lt;br /&gt;&lt;br /&gt;It is a &lt;span style="font-weight: bold;"&gt;compile-time error&lt;/span&gt; for an explicit interface member implementation to include&lt;span style="font-weight: bold;"&gt; access modifiers&lt;/span&gt;, and it is a compile-time error to include the modifiers &lt;span style="font-family:courier new;"&gt;abstract, virtual, override, or static&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, &lt;span style="font-weight: bold; font-style: italic;"&gt;they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For more information see the &lt;a href="http://msdn.microsoft.com/en-us/library/aa664591%28VS.71%29.aspx"&gt;MSDN documentation&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;that’s all, interesting tho...&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-2900720820341012263?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/08/explicit-interfaces.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-4879608045975775786</guid><pubDate>Sat, 08 Aug 2009 04:52:00 +0000</pubDate><atom:updated>2009-08-07T22:01:56.832-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unit Testing</category><category domain="http://www.blogger.com/atom/ns#">Rhino Mocks</category><category domain="http://www.blogger.com/atom/ns#">TDD</category><title>Arguments in Stubs and mocks</title><description>Below is an informal email to work mates. Please note I am not the boss, I am a lowly contractor at the bottom of the heap, The devs I work with have a refreshingly open communication channel and I tend to have a bit more experience in Testing/TDD&lt;br /&gt;&lt;br /&gt;*****&lt;br /&gt;Hey Guys&lt;br /&gt;I think I have unfortunately let some bad habits of mine slip over to you guys.&lt;br /&gt;&lt;br /&gt;Some basic rules of thumb:&lt;br /&gt;&lt;br /&gt;-Stubs should be used by default to isolate dependencies, use mocks when you are mandating that the SUT is incorrect if it does not interact with the given dependency.&lt;br /&gt;-I will often declare in a test method set up the class level dependency to be a mock (with associated verify in the tear down), however that does not need to have expectations. You can always use the stub method on a mock, meaning failure to call that method will not fail the test.&lt;br /&gt;-Use correct arguments and return values. Returning null and using the .IgnoreArguments() method should be last resorts and are generally a sign (if it is my code) of laziness or haste. Don’t do it unless it actually makes sense for the test.&lt;br /&gt;-When returning null from a stub or mock the SUT should be handling it properly. I.e. what if that dependency actually did pass back a null? Is that even valid? Should we be handling it?&lt;br /&gt;&lt;br /&gt;The major problem I find is the ignore argument method, I abuse it far too much when mocking, and I see it creeping it others work (not just ours but external code too!). Note: Ignore Arguments on stubs is not so bad, as a stub should not fail a test&lt;br /&gt;&lt;br /&gt;RhinoMocks has the ability to specify argument placeholders that do not have to be the exact reference type that is being used e.g.:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: courier new;"&gt;timesheetService.&lt;br /&gt;     Mock(s =&gt; s.SaveNewTimesheet(&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;         Arg&lt;timesheetcreationdto&gt;.Is.Equal(creationDto)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    ))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    .Return(new TimesheetDetailsDto());&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;Which is much better than:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;timesheetService.&lt;br /&gt;     Mock(s =&gt; s.SaveNewTimesheet(null))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    .IgnoreArguments()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    .Return(null);&lt;/span&gt;&lt;/span&gt;&lt;timesheetcreationdto&gt;&lt;br /&gt;&lt;br /&gt;Be sure to override the equals method to accurately reflect the equality tho, otherwise the default of reference equality will still cause the test to fail!&lt;br /&gt;&lt;/timesheetcreationdto&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-4879608045975775786?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/08/arguments-in-stubs-and-mocks.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-2549870585747692966</guid><pubDate>Fri, 07 Aug 2009 03:24:00 +0000</pubDate><atom:updated>2009-08-07T21:26:03.029-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Code Generation</category><category domain="http://www.blogger.com/atom/ns#">Testing</category><category domain="http://www.blogger.com/atom/ns#">Pex</category><category domain="http://www.blogger.com/atom/ns#">Best Practices</category><category domain="http://www.blogger.com/atom/ns#">Design By Contract</category><category domain="http://www.blogger.com/atom/ns#">Microsoft</category><title>Coding Guidelines</title><description>Last night I presented to the Perth .Net Community on an upcoming tool called PEX.  There were a couple of mentions in the talk of "allowable exceptions" backed up by mentions of the .Net Framework Guidelines.&lt;br /&gt;I was asked by a few people afterward what the book was and whether I had presumably made these guidelines up ;)&lt;br /&gt;I was under the impression that this book was widely read, so it is clearly not as common knowledge as i may have thought.&lt;br /&gt;&lt;a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=dp_ob_title_bk"&gt;Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (2nd Edition)&lt;/a&gt; &lt;br /&gt;is a must read for .Net devs that are writing code that is consumable by others (ie anything that uses public or protected accessors)&lt;br /&gt;&lt;br /&gt;I would highly recommend this as it also gives a lot of background as to "why" behind the recommendations. It is also nice to read the comment from authors of certain .Net framework as they point out many things including their mistakes.&lt;br /&gt;&lt;br /&gt;The books is made available online for free (not sure if it is in its entirety) at MSDN &lt;a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The allowable exception was in reference to section 7.3.5 (page 237) or a cut down version &lt;a href="http://msdn.microsoft.com/en-us/library/ms229007.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Oh, the links to the Pex stuff are here:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/devlabs/cc950525.aspx"&gt;Pex&lt;/a&gt; or &lt;a href="http://research.microsoft.com/en-us/projects/pex/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://pex.codeplex.com/"&gt;Community Extensions&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://research.microsoft.com/en-us/projects/stubs/"&gt;Stubs framework&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://research.microsoft.com/en-us/projects/specsharp/"&gt;Spec# (Pronounced "Speck Sharp")&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://research.microsoft.com/en-us/projects/contracts/"&gt;Code Contracts&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Thanks to everyone who came (especially those who bought beers afterwards) ;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-2549870585747692966?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/08/coding-guidelines.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-6595835866895344272</guid><pubDate>Fri, 24 Jul 2009 07:46:00 +0000</pubDate><atom:updated>2009-07-24T00:55:26.501-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">JavaScript</category><category domain="http://www.blogger.com/atom/ns#">Unit Testing</category><category domain="http://www.blogger.com/atom/ns#">Testing</category><category domain="http://www.blogger.com/atom/ns#">Books</category><category domain="http://www.blogger.com/atom/ns#">NHibernate</category><category domain="http://www.blogger.com/atom/ns#">IronPython</category><category domain="http://www.blogger.com/atom/ns#">Python</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><category domain="http://www.blogger.com/atom/ns#">TDD</category><title>Manning Book Reviews</title><description>Thought i'd let you guys know of some books i have been reading that are pretty good, They are all from Manning which I am really starting to like that publisher more and more.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.manning.com/bibeault/"&gt;JQuery In Action&lt;/a&gt;: Within about 90 minutes of reading this book you will understand the fundamentals of jQuery and be ready to do basic, but powerful, jQuery code. If you are using javascript natively seriously consider switching to jQuery and get this book. jQuery also has a test framework (QUnit) and a great suite of UI plugins (jQuery UI)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.artofunittesting.com/"&gt;Art Of Unit Testing&lt;/a&gt;: Easily the best unit testing book I have read (and I have read a few). Great for newbies and those still getting to grips with how to test anything more than the most trivial of examples. It is the book i would recommend to people looking to learn to do TDD well. Note the examples are in C# but they really dont require indepth knowledge of .Net, in the same way all the other books are in java and I havent written a line of a coffee flavoured code in a decade. In saying that the tools are all .Net based but i am sure there are Python, Ruby and Java equivilents avaliable for most.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.manning.com/kuate/"&gt;NHibernate in Action&lt;/a&gt;: Pretty much the same as the Hibernate book but shows all the .Net stuff you can do. its also a bit more up to date that the original Hibernate book (which has since had a second release). .Net devs nusing NH need* this book.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.manning.com/foord/"&gt;IronPython in Action&lt;/a&gt;: not a bad book... it does exactly what it intends, it teaches .Net devs about python on the CLR. The question is: Do you care? For me it was something of interest, i doubt I'll ever use it in production. As a side note for the .Net kids i think the path of C# =&gt; Boo =&gt; Python =&gt;Ruby is the one to take for the typical C# developer**. It keeps the "barrier to entry" low for the next step so you are picking up one new thing at a time (ie new syntax, dynamic language contrainst, DSLs and other scripty weirdness) and by the end of the process you have 4 languages under your belt in about the same time it would take to do the C# =&gt; Ruby jump.&lt;br /&gt;&lt;br /&gt;&lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_0"&gt;that's&lt;/span&gt; all&lt;br /&gt;&lt;br /&gt;Rhys&lt;br /&gt;&lt;br /&gt;*OK no one needs anything, especially as the NH doc a pretty good, but you will be severely hindered without it.&lt;br /&gt;** VB.Net &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;devs&lt;/span&gt;; you know you will never learn another language, you have had years to do so!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-6595835866895344272?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/07/manning-book-reviews.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-5497569249985770945</guid><pubDate>Thu, 23 Jul 2009 02:41:00 +0000</pubDate><atom:updated>2009-07-23T23:47:57.137-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unity</category><category domain="http://www.blogger.com/atom/ns#">Logging</category><category domain="http://www.blogger.com/atom/ns#">AOP</category><title>Unity with config free AOP</title><description>At the current place of work i have managed to introduce the notion of IoC and DI. As the team was using EntLib i investigated Unity and found it to be a suitable replacement for Windsor or SM considering how we were going to be using it.&lt;br /&gt;We have just started up a new project and i have asked one of the lads to investigate AOP with unity, what we found was a pretty simple solution for our initial requirement, logging the service calls using Unity&lt;br /&gt;&lt;br /&gt;Below is the 3 files that make up the spike. it very trivial, but there is some pretty average info on how Unity work, there is sample (like this) but with little explanation as to what is going on. David Hayden is probably the first port of call for more info (note you will need to reference Unity 1.2 and Unity Interception).&lt;br /&gt;Note we are using the TransparentProxyInterceptor not the InterfaceInterceptor, which i believe is broken as it does not handle inheritance, which in an OO world is not good enough.&lt;br /&gt;&lt;br /&gt;  &lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;class Program&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        static void Main(string[] args)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            IUnityContainer container = new UnityContainer();&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;            container.RegisterType&lt;italker,&gt;&amp;lt;ITalker, Talker&amp;gt;();&lt;br /&gt;&lt;/italker,&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;            container.AddNewExtension&lt;interception&gt;&lt;/interception&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;interception&gt;Interception&lt;/interception&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;interception&gt;();&lt;br /&gt;&lt;/interception&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;            container.Configure&lt;interception&gt;&lt;/interception&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;interception&gt;Interception&lt;/interception&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;interception&gt;().&lt;br /&gt;&lt;/interception&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;                SetDefaultInterceptorFor&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;italker&gt;ITalker&lt;/italker&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;italker&gt;(new TransparentProxyInterceptor()).&lt;/italker&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                AddPolicy("Logging").&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                AddMatchingRule(new TypeMatchingRule(typeof(ITalker))).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                AddCallHandler(typeof(LoggerHandler));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;         &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Console.WriteLine("This is the start");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            container.Resolve&lt;italker&gt;&lt;/italker&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;italker&gt;ITalker&lt;/italker&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;italker&gt;().Talk();&lt;/italker&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Console.WriteLine("This is the end");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Console.ReadLine();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    public interface ITalker&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        void Talk();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    public class Talker : ITalker&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        public void Talk()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Console.WriteLine("helllllllooooo!");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    public class LoggerHandler : ICallHandler&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        public LoggerHandler()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Order = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        public int Order { get; set; }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate&lt;br /&gt;etNext)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Console.WriteLine("** I'm in!**");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            var result = getNext().Invoke(input, getNext);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            Console.WriteLine("** Out I go :) **");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            return result;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;italker, talker=""&gt;&lt;interception&gt;&lt;interception&gt;&lt;italker&gt;&lt;italker&gt;&lt;br /&gt;---------------------------------------------------------&lt;br /&gt;Which returns :&lt;br /&gt;This is the start&lt;br /&gt;** I'm in!**&lt;br /&gt;helllllllooooo!&lt;br /&gt;** Out I go :) **&lt;br /&gt;This is the end&lt;br /&gt;---------------------------------------------------------&lt;br /&gt;*Sorry about the formatting, this is done with out WLW*&lt;/italker&gt;&lt;/italker&gt;&lt;/interception&gt;&lt;/interception&gt;&lt;/italker,&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-5497569249985770945?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/07/unity-with-config-free-aop.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-5756251240853655429</guid><pubDate>Tue, 16 Jun 2009 17:10:00 +0000</pubDate><atom:updated>2009-06-16T10:10:08.747-07:00</atom:updated><title>Many to Many joins- Revisited</title><description>&lt;p&gt;I do a Google search a year later &amp;amp; I find &lt;a href="http://rhysc.blogspot.com/2008/02/nhibernate-many-to-many-collections.html" target="_blank"&gt;my own post&lt;/a&gt; and I don't like it.    &lt;br /&gt;There are several issues with this post: &lt;/p&gt;  &lt;p&gt;Firstly sending objects over the wire was always a bad idea, this post was trying to dodge that as the team did not want to move to DTOs, which i maintain would have saved us time in the end. The real fix is to map the entities to DTO's and send those over the wire specific to the service calls needs.   &lt;br /&gt;Secondly MANY-MANY joins are not cool. There are very few places where Many-Many actually exists. Hiding the join in the Entity should have been done, not eliminating the mapping and the joining classes. Redoing this i would have kept the joining class and mapping as a one-many &amp;lt;-&amp;gt; many-one relationship.&lt;/p&gt;  &lt;p&gt;eg to expose a customers favourite restaurants   &lt;br /&gt;&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;public class Customer&lt;br /&gt;{&lt;br /&gt;//stuff&lt;br /&gt;public IEnummerable&amp;lt;Restaurant&amp;gt; GetFavouriteRestaurants()&lt;br /&gt;{&lt;br /&gt;    foreach(var customerRestaurant in CustomerRestaurants)&lt;br /&gt;    {&lt;br /&gt;        if(customerRestaurant.IsValid())//some check if required&lt;br /&gt;            yield return customerRestaurant.Restaurant; &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This now hides the notion of a CustomerRestaurant entity from the outside world as it can be contained with the realms of the domain entity classes (that being Customer and Restaurant)&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Well, i guess its good to review ones work, I'm not happy that this was a decision I made, however acknowledging ones mistake is an opportunity for growth&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-5756251240853655429?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/06/many-to-many-joins-revisited.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-7019239128535989913</guid><pubDate>Tue, 02 Jun 2009 14:17:00 +0000</pubDate><atom:updated>2009-06-02T07:17:03.938-07:00</atom:updated><title>Gallio : Why? When? How?</title><description>&lt;p&gt;In a time were TDD and Continuous Integration is becoming common place &lt;a href="http://www.gallio.org" target="_blank"&gt;Gallio&lt;/a&gt; is a great tool to have in the tool belt. I have been a fan of the related MbUnit for several years but only in the last 6 months have I really seen the light in the separation of the Gallio project and why it is such a good thing.&lt;/p&gt;  &lt;p&gt;Lets back the truck up a bit and shed some light on what exactly (in my mind) Gallio really is then we can talk about why you would want to use it.&lt;/p&gt;  &lt;p&gt;Gallio is basically an interop facility that act act as a generic test runner. Sure it can be much more than that but at the end of the day 99.99% of people that would be using it&amp;#160; will be using it as a&amp;#160; means to execute tests. Gallio actually is a project that has &lt;a href="http://www.gallio.org/History.aspx" target="_blank"&gt;broken away&lt;/a&gt; from the MbUnit project to provide an neutral test runner for other test frameworks.&lt;/p&gt;  &lt;p&gt;So what the hell is a test runner? Firstly we would need to look at how we would normally run a (unit) test. Firstly we would typically choose a test framework to write tests in; the common API's that fall in to this category are &lt;a href="http://www.nunit.org/index.php" target="_blank"&gt;NUnit&lt;/a&gt;, &lt;a href="http://www.mbunit.com/" target="_blank"&gt;MbUnit&lt;/a&gt;, &lt;a href="http://www.codeplex.com/xunit" target="_blank"&gt;XUnit.Net&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms182486.aspx" target="_blank"&gt;MSTest&lt;/a&gt;. These allow us to write classes and methods with attributes that describe &lt;em&gt;what &lt;/em&gt;and &lt;em&gt;how &lt;/em&gt;we wish to test the system under test (SUT). Writing these test does not run the tests, we still need something to kick the process off. This is where our test runners come in to play. TestDriven.Net, ReSharper, Visual Studio test window and the various separate GUI that come with the frameworks (e.g. Nunit GUI Runner and Icarus for MbUnit) allow us to select what tests we wish to execute. Unfortunately there is some degree of coupling present here, i.e. the Visual studio test runner may or may not run your given test framework, NUnit GUI surely doesn't run XUnit tests. There is also the very large issue of being able to run these from the command line or a script; this is pretty important for continuous integration. This is where Gallio fits.&lt;/p&gt;  &lt;p&gt;Gallio provide a neutral system that provides a &amp;quot;neutral system for .NET that provides a common object model, runtime services and tools (such as test runners) that may be leveraged by any number of test frameworks.&amp;quot; What this means is Gallio can sneak in between your chosen test runner and the test API, providing an abstraction between the two. When I first understood this is was underwhelmed... who cares? Well apparently I do!&lt;/p&gt;  &lt;p&gt;You see at my current place of work we, like many .Net teams, use MSTest as our test framework. Being the good kids we are we were keen to get CI up and running and with out TFS properly installed (at the time) we decide to use TeamCity as&amp;#160; our build server. Its a great tool and I have no regrets in using it. Unfortunately trying to get MSTest test to run from a script is a little fiddly and requires an install of a version of visual studio that has MSTest on it on the machine that want to run the test script. Obviously we want or build server to run the test for the solution too, so now we had to install Visual Studio onto our build server... this is not cool. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It takes up a lot of space, we had to fight to get a VM created for us to have a build server and&amp;#160; installing VS took up most of the space we were given &lt;/li&gt;    &lt;li&gt;We had just used up one of our licences of Visual Studio. VS is not cheap. Sure, I work for a huge company that haemorrhages cash, but wasting money is still wasting money. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Enter Gallio. With a minor adjustment* of our build script I can now use Gallio to run my MSTest tests from my MSBuild script. This is pretty cool. What this means is now I have a test framework agnostic build script. If we converted all of our tests to MbUnit I would not have to change my build scripts; MbUnit is supported by Gallio so I am covered. This also means I have nice reports generated for me without crazy MSTest stuff spewing all over my hard drive. The reports are very clean, configurable and human readable. I can show my department manager (who may or may not be technical) the test reports for all of our projects and he can see what state they are in. Having a clean readable report seriously helps in promoting our good work, something an MSBuild log file or nasty MSTest XML would not do so well.&lt;/p&gt;  &lt;p&gt;OK so who should be interested in Gallio?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;People who &amp;quot;do&amp;quot; CI&lt;/strong&gt;: Having a free test runner on the build server may be saving you cash and is a big benefit, I would say however having a neutral runner means easier maintenance and is the biggest win here. The build scripts will all use the same syntax. Gallio works with the above mention test frameworks but also integrates with MSBuild, NAnt, NCover, PowerShell, CC.Net and TeamCity.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;People who use (or potentially &lt;em&gt;may &lt;/em&gt;use) more than one test framework&lt;/strong&gt;: Having Gallio in the mix means running NUnit from visual studio is very simple. Pick your poison; TD.Net, ReSharper and VS can all now run that or any other Gallio supported framework.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;People who want good consistent Test Reports: &lt;/strong&gt;This is certainly my opinion, but I really like the Gallio reports. They are clear, easy to navigate and if you are using multiple frameworks you can now have a consistent format to display your reports.&lt;/p&gt;  &lt;p&gt;Something to get you started - an MSBuild template for using the &lt;a href="http://www.gallio.org/api/html/T_Gallio_MSBuildTasks_Gallio.htm" target="_blank"&gt;Gallio.MSbuildTasks&lt;/a&gt; assembly:&lt;/p&gt;  &lt;pre class="xml" name="code"&gt;&amp;lt;Project xmlns=&amp;quot;http://schemas.microsoft.com/developer/msbuild/2003&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;!-- This is needed by MSBuild to locate the Gallio task --&amp;gt;&lt;br /&gt;    &amp;lt;UsingTask AssemblyFile=&amp;quot;[Path-to-assembly]\Gallio.MSBuildTasks.dll&amp;quot; TaskName=&amp;quot;Gallio&amp;quot; /&amp;gt;&lt;br /&gt;    &amp;lt;!-- Specify the tests assemblies --&amp;gt;&lt;br /&gt;    &amp;lt;ItemGroup&amp;gt;&lt;br /&gt;        &amp;lt;TestAssemblies Include=&amp;quot;[Path-to-test-assembly1]/TestAssembly1.dll&amp;quot; /&amp;gt;&lt;br /&gt;        &amp;lt;TestAssemblies Include=&amp;quot;[Path-to-test-assembly2]/TestAssembly2.dll&amp;quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/ItemGroup&amp;gt;&lt;br /&gt;    &amp;lt;Target Name=&amp;quot;RunTests&amp;quot;&amp;gt;&lt;br /&gt;        &amp;lt;Gallio &lt;br /&gt;            Assemblies=&amp;quot;@(TestAssemblies)&amp;quot;&amp;gt;&lt;br /&gt;            IgnoreFailures=&amp;quot;true&amp;quot; &lt;br /&gt;            ReportDirectory=&amp;quot;%(ReportDirectory)&amp;quot;&lt;br /&gt;            ReportTypes=&amp;quot;html&amp;quot;&lt;br /&gt;            ShowReports=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;            &amp;lt;!-- This tells MSBuild to store the output value of the task's ExitCode property&lt;br /&gt;                 into the project's ExitCode property --&amp;gt;&lt;br /&gt;            &amp;lt;Output TaskParameter=&amp;quot;ExitCode&amp;quot; PropertyName=&amp;quot;ExitCode&amp;quot;/&amp;gt;&lt;br /&gt;        &amp;lt;/Gallio&amp;gt;&lt;br /&gt;        &amp;lt;Error Text=&amp;quot;Tests execution failed&amp;quot; Condition=&amp;quot;'$(ExitCode)' != 0&amp;quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/Target&amp;gt;&lt;br /&gt;&amp;lt;/Project&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Hopefully this helps shed some light on the Gallio project and how it may fit into your build and test process.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;*The minor adjustment is actually cleaning up the script which is also a good thing. It is much clearer what is happening. The MSTest hacks involve small amounts of wand waving.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-7019239128535989913?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/06/gallio-why-when-how.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-3818159246325245481</guid><pubDate>Mon, 25 May 2009 15:57:00 +0000</pubDate><atom:updated>2009-05-25T08:57:16.987-07:00</atom:updated><title>MassTransit Host &amp; Setup</title><description>&lt;p&gt;[&lt;a href="http://rhysc.blogspot.com/2009/05/getting-started-with-masstransit.html" target="_blank"&gt;Intro&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;The glue that ties all of MassTransit's moving pieces has to be done&amp;#160; on starting you application. We need configure the service to know what to start up, how to find it and in what context to run it.&lt;/p&gt;  &lt;p&gt;MassTransit has split the host service into a separate project, namely &lt;a href="http://code.google.com/p/topshelf/" target="_blank"&gt;TopShelf&lt;/a&gt;. you will see TopShelf being used to set up our MassTransit programs in the entry points of our application, typically with the Program.Main(string[] args) method.&lt;/p&gt;  &lt;p&gt;The basic set up steps for creating a runner configuration are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Describe the Service&lt;/li&gt;    &lt;li&gt;Instruct how the service will be run&lt;/li&gt;    &lt;li&gt;Configure the the service&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Once you have done this, the TopShelf Runner can host the service.&lt;/p&gt;  &lt;p&gt;Describing the Service means give the service a name, a display name and a description. The display name and description are visible from the Service Control Manager while the service name is intend for console line interactions&lt;/p&gt;  &lt;p&gt;Instructing how the service will be run: Define any known dependencies (MSMQ, IIS, SqlServer etc), any actions that should be performed prior to running the service/host and also how the service is to be run: i.e what credentials will the service run under. We can also use the UseWinFormHost&amp;lt;T&amp;gt; where we can supply the name of the WinForm that is the host. This is great for demos, but I am not sure if it is intended for production use... Chris and Dru may care to comment on this; either way its handy when getting to terms with the stack.&lt;/p&gt;  &lt;p&gt;Next we need to configure the service(s) we are hosting. In here we can define some delegate for certain event in service life (WhenStarted, WhenStopped etc) and we can also weave some of our IoC voodoo majic by defining our service locator. Again the authors have decided to use Castle Windsor for the sample, however I &lt;em&gt;believe&lt;/em&gt; you can use any of the &lt;a href="http://www.codeplex.com/CommonServiceLocator" target="_blank"&gt;CommonServiceLocator&lt;/a&gt; Containers. As this method need to returns something that implements IServiceLocator, using the DefaultMassTransitContainer type makes life a little easier as it does a fair bit of the plumbing for you including setting the current service locator to itself.&lt;/p&gt;  &lt;pre class="xml" name="code"&gt;[STAThread]&lt;br /&gt;private static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;//from Starbucks.Barista.Program.Main(string[] args) - Modified for readability&lt;br /&gt;var cfg = RunnerConfigurator.New(configurator =&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        //Describe the Service        &lt;br /&gt;        configurator.SetServiceName(&amp;quot;StarbucksBarista&amp;quot;);&lt;br /&gt;        configurator.SetDisplayName(&amp;quot;Starbucks Barista&amp;quot;);&lt;br /&gt;        configurator.SetDescription(&amp;quot;A Mass Transit sample service for making orders of coffee.&amp;quot;);&lt;br /&gt;&lt;br /&gt;        //Instruct How the service will be run        &lt;br /&gt;        configurator.DependencyOnMsmq();&lt;br /&gt;        configurator.RunAsFromInteractive();&lt;br /&gt;        configurator.BeforeStart(a =&amp;gt; { });&lt;br /&gt;&lt;br /&gt;        //Configure the service(s)        &lt;br /&gt;        configurator.ConfigureService&amp;lt;BaristaService&amp;gt;(serviceConfigurator =&amp;gt;&lt;br /&gt;            {&lt;br /&gt;                serviceConfigurator.CreateServiceLocator(() =&amp;gt;&lt;br /&gt;                    {&lt;br /&gt;                         //Use MassTransit's built in Container (Castle Windsor specific), described &lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-end-points.html" target="_blank"&gt;earlier&lt;/a&gt;                        &lt;br /&gt;                        IWindsorContainer container = new DefaultMassTransitContainer(&amp;quot;Starbucks.Barista.Castle.xml&amp;quot;);&lt;br /&gt;&lt;br /&gt;                        //Add the components to the container                        &lt;br /&gt;                        container.AddComponent(&amp;quot;sagaRepository&amp;quot;, typeof(ISagaRepository&amp;lt;&amp;gt;), typeof(InMemorySagaRepository&amp;lt;&amp;gt;));&lt;br /&gt;                        container.AddComponent&amp;lt;DrinkPreparationSaga&amp;gt;();&lt;br /&gt;                        container.AddComponent&amp;lt;BaristaService&amp;gt;();&lt;br /&gt;&lt;br /&gt;                        //Tracing - not super important in this context                       &lt;br /&gt;                        Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));&lt;br /&gt;                        StateMachineInspector.Trace(new DrinkPreparationSaga(CombGuid.Generate()));&lt;br /&gt;&lt;br /&gt;                        //Return the Current ServiceLocator, which has been assigned in the DefaultMassTransitContainer ctor&lt;br /&gt;                        return ServiceLocator.Current;&lt;br /&gt;                    });&lt;br /&gt;                //Define delegates (specifically service methods) to fire on given ServiceConfigurator events&lt;br /&gt;                serviceConfigurator.WhenStarted(baristaService =&amp;gt; baristaService.Start());&lt;br /&gt;                serviceConfigurator.WhenStopped(baristaService =&amp;gt; baristaService.Stop());&lt;br /&gt;            });&lt;br /&gt;    });&lt;br /&gt;Runner.Host(cfg, args);&lt;br /&gt;}&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-3818159246325245481?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/masstransit-host-setup.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-1462586691577451985</guid><pubDate>Sun, 24 May 2009 16:43:00 +0000</pubDate><atom:updated>2009-05-24T09:43:58.654-07:00</atom:updated><title>MassTransit End Points</title><description>&lt;p&gt;[&lt;a href="http://rhysc.blogspot.com/2009/05/getting-started-with-masstransit.html"&gt;Intro&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;Many people will be familiar with the notion of an &amp;quot;End Point&amp;quot; especially those who use WCF or other web service frameworks. An end point is &amp;quot;the entry point to a service, a process, or a queue or topic destination&amp;quot;. My WCF background has had the ABC drilled into me (Address, Binding and Contract) as the 3 things that basically define an end point. MT is pretty much the same. Also like WCF, the endpoints are a configuration aspects of the solution so it seems valid to put this information in a config file. The MT boys are clearly Castle fans (although other IoC frameworks can be used) and they have chosen in most of the samples to use Castle Windsor to configure the endpoints. &lt;/p&gt;  &lt;p&gt;SIDE NOTE: For those unaware of Castle Windsor (an &lt;a href="http://martinfowler.com/bliki/InversionOfControl.html" target="_blank"&gt;IoC&lt;/a&gt; implementation) it allows you to write loosely coupled code and specify the concrete implementation detail via config, a little bit like the example of the Asp.Net Membership Provider which is a &lt;a href="http://www.martinfowler.com/eaaCatalog/plugin.html" target="_blank"&gt;plug in pattern&lt;/a&gt;. Using MT without understanding IoC may prove to be difficult... in fact I would say you are almost certainly biting off more than you can chew. Look in to the &lt;a href="http://www.castleproject.org/" target="_blank"&gt;Castle stack&lt;/a&gt;, it really is great OSS framework to help pick up good habits.&lt;/p&gt;  &lt;p&gt;Moving on...&lt;/p&gt;  &lt;p&gt;The defining of the endpoints should not be confused with the Castle implementation. It is just as easy to do this in code. Anyway Lets walk through a typical castle config file for MT:&lt;/p&gt;  &lt;p&gt;From the Starbucks &lt;a href="http://masstransit.pbworks.com/starbucks" target="_blank"&gt;Sample&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="xml" name="code"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; ?&amp;gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;  &amp;lt;facilities&amp;gt;&lt;br /&gt;    &amp;lt;facility id=&amp;quot;masstransit&amp;quot;&amp;gt;&lt;br /&gt;      &amp;lt;bus id=&amp;quot;customer&amp;quot;&lt;br /&gt;                 endpoint=&amp;quot;msmq://localhost/mt_client&amp;quot;&amp;gt;&lt;br /&gt;        &amp;lt;subscriptionService endpoint=&amp;quot;msmq://localhost/mt_subscriptions&amp;quot; /&amp;gt;&lt;br /&gt;        &amp;lt;managementService heartbeatInterval=&amp;quot;3&amp;quot; /&amp;gt;&lt;br /&gt;      &amp;lt;/bus&amp;gt;&lt;br /&gt;      &amp;lt;transports&amp;gt;&lt;br /&gt;        &amp;lt;transport&amp;gt;MassTransit.Transports.Msmq.MsmqEndpoint, MassTransit.Transports.Msmq&amp;lt;/transport&amp;gt;&lt;br /&gt;      &amp;lt;/transports&amp;gt;&lt;br /&gt;    &amp;lt;/facility&amp;gt;&lt;br /&gt;  &amp;lt;/facilities&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;First and foremost this is a castle config. The name of the file &amp;quot;Starbucks.Customer.Castle.xml&amp;quot; is a pretty good hint and I know &amp;quot;facilities&amp;quot; is a castle concept. MassTransit have embraced the concept of facilities which you can investigate &lt;a href="http://www.castleproject.org/container/documentation/trunk/advanced/extendingit.html" target="_blank"&gt;here&lt;/a&gt;. MassTransit have their own Facility, namely the MassTransit.WindsorIntegration.MassTransitFacility which helps us get up and running with out having to know about all the plumbing. In this MassTransit specific facility we define the Bus and the Transports. The transports child node is equivalent to our &amp;quot;Binding&amp;quot;; it is essential so we know what transport mechanism to use. You will see standard .Net notation for expressing a type in XML, i.e: &amp;quot;Fully.Qualified.Namspace.TypeName, Assembly.Name&amp;quot;. This type must implement the interface MassTransit.IEndpoint. Currently there are adapters for MSMQ, NMS, Amazon SQS and WCF. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The other child node in the facility defines the Bus. Here we give the Bus a identifier and its end point. These are both mandatory. The end point will be the URI the bus will receive communication from, when the application publishes a message. The Id indicates that there can be multiple buses configured, which there can. The bus also can have several child nodes specifically:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;controlBus&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;dispatcher&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;subscriptionService&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;managementService&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The Control Bus&lt;/strong&gt; is involved in managing the disparate system. For example the Starbucks example uses a control bus to manage the interaction amongst the server side consumers: the Cashier and the Barrister. For more info on a Control bus see page 540 in &lt;a href="http://www.amazon.com/exec/obidos/redirect?tag=enterpriseint-20&amp;amp;path=tg/detail/-/0321200683"&gt;Enterprise Integration Patterns&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The Dispatcher &lt;/strong&gt;is a means to control the use of threads. High volume message interaction can be handled using multithreading specifically with the attributes maxThreads and readThreads, both of which are self explanatory integer values.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The Subscription Service &lt;/strong&gt;is the common service that provides an endpoint for subscriptions. The only value required is the end point attribute.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;The Management Service&lt;/strong&gt; allows for specifying a heartbeat monitor for checking the health of your services queue. The samples use the SubscriptionManagerGUI to show the queues&amp;#160; that are being listened to and the health of the subscriptions.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I do not believe any of these bus child nodes are mandatory, from looking into the code the only requirements are that the bus has must have an id &amp;amp; end point and the facility has a defined transport. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;There are a couple of notes for new comers to Castle and MassTransit. Like most config files the XML file that is shown above should have its build action as &amp;quot;Content, Copy Always&amp;quot;. The Queues that each service uses also need to be set up (e.g. in MSMQ) before they can be used. Luckily the exception handling in MassTransit is pretty good and will let you know that and endpoint is not set up if it is required, just be sure to read the queue name correctly. I spent a about 15 minutes trying to figure out why a sample subscription was failing when the exception was saying I had&amp;#160; not set up &amp;quot;mt_server1&amp;quot;. I thought it was saying &amp;quot;mt_server&amp;quot;. If in doubt read the exception! We will cover how the castle config is tied up in the Host And End points post.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;End points&amp;#160; and their configuration may be a bit tricky for new comers, but if you break each piece down it becomes more manageable.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-1462586691577451985?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/masstransit-end-points.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-8733664574970227577</guid><pubDate>Thu, 21 May 2009 08:21:00 +0000</pubDate><atom:updated>2009-05-21T01:26:47.083-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.net</category><category domain="http://www.blogger.com/atom/ns#">Oracle</category><title>OLE DB oracle drivers</title><description>I have had issues in the past with standard .Net OLE drivers for Oracle with reagrd to transactions, switching to the oracle drivers fixed the issue, however i have now found the reason why... the M$ one explicitly does not support nested transactions!&lt;br /&gt;&lt;br /&gt;Microsoft's OLE DB Provider for Oracle:"...At this time, the provider does not support nested transactions, which is how it would expose save points."&lt;br /&gt;&lt;br /&gt;first link here: http://tinyurl.com/qm6fpq&lt;br /&gt;&lt;br /&gt;Note navigating directly to the expert exchnge site will not show the answer, google have forced them to show answers at the bottom of the page, hence to indirect link.&lt;br /&gt;This post is much more for me to find this link again.&lt;br /&gt;&lt;br /&gt;Basically if you are using .Net and Oracle, use the Oracle drivers&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-8733664574970227577?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/ole-db-oracle-drivers.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-8733644994763125022</guid><pubDate>Tue, 19 May 2009 17:18:00 +0000</pubDate><atom:updated>2009-05-19T10:18:07.443-07:00</atom:updated><title>MassTransit Publishers</title><description>&lt;p&gt;[&lt;a href="http://rhysc.blogspot.com/2009/05/getting-started-with-masstransit.html" target="_blank"&gt;Intro&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;So we feel we have something that the world needs to know about, we have messages to publish. This is what kicks off the events that make up the Pub/Sub system. The IT division have told you they are sick of modifying the HR application to call a growing number of web service to let those services know about&amp;#160; new or updated employee information. You decide this may be a good candidate for some Pub/Sub love. We will start with New Employees, firstly we would need to create a suitable message to publish, say &amp;quot;NewEmployeeNotificationMessage&amp;quot;. This has all the relevant info in the message. As part of the creation process all we need to do is create a message of the given type and publish it.&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;var message = CreateNewEmployeeMessage();&lt;br /&gt;_serviceBus.Publish(message);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;That is it. Well.... its not, but as far as the publishing code goes that all there is too it, there is a little bit of infrastructure set up that goes on at start up, but to publish a message is really that simple.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;There are times where you may want to know of a response if a subscriber sends one, this can be done by setting a response address in a delegate as part of the publish eg:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;_bus.Publish(message, x=&amp;gt; x.SetResponseAddress(_bus.Endpoint.Uri));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If a response is expected then the service publishing the message should also be a consumer of the response message type, see the &lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-consumerssubscribers.html" target="_blank"&gt;consumers post&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The bus is a MassTransit.IServiceBus that is injected in to the service. We will cover setting up the bus later on in the series.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;*this may be a bit over the top example. If you a re building enterprise wide service and integrating system perhaps MT is a little too light weight, judge for yourself. Personally I am angling at using for intra component messaging.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-8733644994763125022?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/masstransit-publishers.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-69071405279517266</guid><pubDate>Tue, 19 May 2009 17:16:00 +0000</pubDate><atom:updated>2009-05-19T10:16:17.500-07:00</atom:updated><title>MassTransit Consumers/Subscribers</title><description>&lt;p&gt;[&lt;a href="http://rhysc.blogspot.com/2009/05/getting-started-with-masstransit.html" target="_blank"&gt;Intro&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;A messaging system does make a lot of sense if no one or nothing is listening, consuming or subscribing to those sent messages. If you are interested in a particular event that a message represents then you subscribe to that event. &lt;/p&gt;  &lt;p&gt;Continuing on with the idea of a new employee at a company, lets assume that head office have decided that all staff members must do a new online intranet based safety course and any new employees must do the safety course as part of the induction. We can create this online application, send out the notifications to all existing staff, but how do we ensure all new staff do the course? well we know that HR publish a New Employee Notification when an employee joins the company so we decide to subscribe to the message so our application notifies the new employee and his supervisor that this course must be completed as part of their induction.&lt;/p&gt;  &lt;p&gt;Ok, so how do we do this in MassTransit?&lt;/p&gt;  &lt;p&gt;Well one option is to create a consumer, a service that subscribes to the message and acts on it when it happens.&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;public class NewEmployeeService : Consumes&amp;lt;NewEmployeeNotificationMessage&amp;gt;.All&lt;br /&gt;{&lt;br /&gt;    private IServiceBus _serviceBus;&lt;br /&gt;    private UnsubscribeAction _unsubscribeToken;&lt;br /&gt;    public void Consume(NewEmployeeNotificationMessage message)&lt;br /&gt;    {&lt;br /&gt;        //Notify user and supervisor of course requirement&lt;br /&gt;    }&lt;br /&gt;    public void Dispose()&lt;br /&gt;    {&lt;br /&gt;        _serviceBus.Dispose();&lt;br /&gt;    }&lt;br /&gt;    public void Start(IServiceBus bus)&lt;br /&gt;    {&lt;br /&gt;        _serviceBus = bus;&lt;br /&gt;        _unsubscribeToken = _serviceBus.Subscribe(this);&lt;br /&gt;    }&lt;br /&gt;    public void Stop()&lt;br /&gt;    {&lt;br /&gt;        _unsubscribeToken();&lt;br /&gt;    }        &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A couple of things to note here:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The NewEmployeeService implements the &amp;quot;Consumes&amp;lt;T&amp;gt;.All&amp;quot; interface. This means we are subscribing to any message published of type T, in this case NewEmployeeNotificationMessage. By doing so we must implement Consume(T message), this is the method that will be called when the message arrives.&amp;#160; Start and stop are methods we have defined that get call when the host starts up the hosting service (we will cover this is later posts). More importantly and something that may not be obvious is the unsubscribeToken. When subscribing to the bus the subscribe method returns an UnsubscribeAction delegate that can be called when the subscription is no longer required. Therefore calling this delegate on the stopping of the service would be a good idea :)&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A service can subscribe to many&amp;#160; messages by specifying and implementing more of the consume interfaces, as it is not a base class you are not limited to a single inheritance. So you may want to define the class as :&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;public class NewEmployeeService : &lt;br /&gt;    Consumes&amp;lt;NewEmployeeNotificationMessage&amp;gt;.All,&lt;br /&gt;    Consumes&amp;lt;EmployeeChangedLocationNotificationMessage&amp;gt;.All&lt;br /&gt;{&lt;br /&gt;    //...etc&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;It is also worth while to note that the message can be &lt;strong&gt;&lt;em&gt;responded&lt;/em&gt;&lt;/strong&gt; to:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c#" name="code"&gt;CurrentMessage.Respond(responseMessage);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This will send the message back to the response address specified by the client, see the Starbucks example: CashierSaga.ProcessNewOrder(..) and OrderDrinkForm. NB: The OrderDrinkForm also implements the consume interface for the response message, otherwise it will not know what to do with the message&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-69071405279517266?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/masstransit-consumerssubscribers.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-2860773913421931687</guid><pubDate>Tue, 19 May 2009 17:13:00 +0000</pubDate><atom:updated>2009-05-19T10:14:41.505-07:00</atom:updated><title>MassTransit Messages</title><description>&lt;p&gt;[&lt;a href="http://rhysc.blogspot.com/2009/05/getting-started-with-masstransit.html" target="_blank"&gt;Intro&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;Messages are the backbone of MassTransit, without them there would not really be a need for the solution. Messages IMO should be a Verb. &amp;quot;Customer&amp;quot; is not a suitable message name as it has no intent, &amp;quot;NewCustomerCreated&amp;quot; is therefore a more suitable name. As far as MassTransit goes a message just needs to be a class that is marked as [Serializable]. For most scenarios is have encountered I actually want to track a specific message, i.e. I want to know its identity (which we will cover soon), so I have my message implement the interface &amp;quot;MassTransit.CorrelatedBy&amp;lt;T&amp;gt;&amp;quot; which gives the message a Correlation Id so I can track it. It is probably a good time to mention that messages are &lt;a href="http://masstransit.pbworks.com/Messages" target="_blank"&gt;Immutable&lt;/a&gt; dumb &lt;a href="http://en.wikipedia.org/wiki/Data_Transfer_Object" target="_blank"&gt;DTO's&lt;/a&gt;. I have worked on several systems now that try to ignore this and every time it has ended in trouble. The message is a trigger, it should never be the entity you manipulating.&lt;/p&gt;  &lt;p&gt;An Example from the MassTransit Pub/Sub Sample is below:&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;[Serializable]&lt;br /&gt;public class RequestPasswordUpdate :&lt;br /&gt;    CorrelatedBy&amp;lt;Guid&amp;gt;&lt;br /&gt;{&lt;br /&gt;    private readonly string _newPassword;&lt;br /&gt;    private readonly Guid _correlationId;&lt;br /&gt;       public RequestPasswordUpdate(string newPassword)&lt;br /&gt;    {&lt;br /&gt;        _correlationId = Guid.NewGuid();&lt;br /&gt;        _newPassword = newPassword;&lt;br /&gt;    }&lt;br /&gt;    public string NewPassword&lt;br /&gt;    {&lt;br /&gt;        get { return _newPassword; }&lt;br /&gt;    }&lt;br /&gt;    public Guid CorrelationId&lt;br /&gt;    {&lt;br /&gt;        get { return _correlationId; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Using the correlation Id means that&amp;#160; later on when I want to listen for associated messages I can. This will be covered in [Consumers/Publishers]&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-2860773913421931687?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/masstransit-messages.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7806605514179331942.post-6010515970340302527</guid><pubDate>Tue, 19 May 2009 17:11:00 +0000</pubDate><atom:updated>2009-05-25T08:59:41.196-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">MassTransit</category><category domain="http://www.blogger.com/atom/ns#">MSMQ</category><category domain="http://www.blogger.com/atom/ns#">ESB</category><title>Getting started with MassTransit</title><description>&lt;p&gt;Ok so I continue to play with MassTransit and I really like it. Unfortunately I still think there is a small barrier to entry that is stopping people from using it. The guys who have written it have done a great job of building an easy to use stack, but as it grows it may feel a bit like you don't know where to start.What I aim to do here is break the whole thing down to easy to understand pieces (theory) and then get the pieces together (practice!)&lt;/p&gt;  &lt;p&gt;MassTransit leans on the concept of &lt;a href="http://www.enterpriseintegrationpatterns.com/PublishSubscribeChannel.html" target="_blank"&gt;Publish/Subscribe&lt;/a&gt; or Pub/Sub. The idea being I can raise an event by sending a message (publishing) and any number of consumers that are interested in that message can listen in and consume that message (subscribing). This means as new subscriber become known the publisher itself does not have to be aware of its existence, the Bus (MT) will deal with it, providing a nice sense of loose coupling.&lt;/p&gt;  &lt;p&gt;An example could be a new person starts at your place of work. His new boss goes in to the HR system and creates a new employee request*. This goes off to HR where it is actioned and a new Employee notification* is made. A slew of process are now kickoff that HR has no idea about , nor do they care. These could include the new employees desk set up, Identification preparation, security clearance checks, various inductions bookings... who knows? If a department or application are interested in that message they just subscribe to it.&lt;/p&gt;  &lt;p&gt;*These are the potential published message&lt;/p&gt;  &lt;p&gt;Right, lets delve in to the specifics:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-messages.html" target="_blank"&gt;Messages&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-consumerssubscribers.html" target="_blank"&gt;Consumers/Subscribers&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-publishers.html" target="_blank"&gt;Publishers&lt;/a&gt;&lt;/p&gt; &lt;p&gt; &lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-end-points.html" target="_blank"&gt;EndPoints&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://rhysc.blogspot.com/2009/05/masstransit-host-setup.html" target="_blank"&gt;Host &amp;amp; Set up&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Sagas&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;For background on MassTransit see my previous intro post &lt;a href="http://rhysc.blogspot.com/2008/08/masstranist-101-what-is-masstransit.html" target="_blank"&gt;here&lt;/a&gt; and some &lt;a href="http://rhysc.blogspot.com/search?q=masstransit" target="_blank"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7806605514179331942-6010515970340302527?l=rhysc.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://rhysc.blogspot.com/2009/05/getting-started-with-masstransit.html</link><author>noreply@blogger.com (RhysC)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item></channel></rss>
