<?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/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-7349901047715470998</atom:id><lastBuildDate>Wed, 01 May 2013 08:50:23 +0000</lastBuildDate><category>sloppycode</category><category>simplicity</category><category>thankgoditsover</category><category>hiro ioc codeproject dotnet</category><category>linfu aop redesign notes dotnet</category><category>mono.cecil</category><category>dlr</category><category>hiro ioc codeproject dynamic</category><category>enoughisenough</category><category>refactoring</category><category>vacation</category><category>dynamiclanguages</category><category>hiro linfu ioc dotnet codeproject</category><category>general</category><category>writersblock</category><category>linfu adaptiveobjectmodels dotnet myxaml dynamicobject reflection</category><category>writethisblock</category><category>mvc mvp mvwithoutthep patterns eureka</category><category>dbc</category><category>notinventedhere</category><category>wishlist</category><category>cecil</category><category>ohthehumanity</category><category>csharp</category><category>ceciltheuglywife</category><category>linfu</category><category>clr</category><category>futurefeatures</category><category>hiro ioc codeproject</category><category>orm</category><category>kiss</category><category>design</category><category>linfu aop notes dotnet</category><category>nottheoldhagagain</category><category>whew aop linfu thankgoditstuesday</category><category>reinventingthewheel prototypes aop linfu redesign design</category><category>nih</category><category>linfu redesign rewrite versiontwo</category><category>linfu ioc versiontwo csharp</category><category>mono</category><category>eureka linfu aop byjovewehaveit greatscott omg dotnet</category><category>architecture</category><category>dotnet</category><category>writing</category><category>linfu ioc versiontwo design dotnet csharp</category><category>codeproject</category><category>reinventingthewheel prototypes aop linfu</category><category>ide</category><title>The Bytecode Artist</title><description>The musings of a metaprogrammer and his love of IL, and all things written on the Common Language Runtime.</description><link>http://www.bytecodeartist.net/</link><managingEditor>noreply@blogger.com (Philip Laureano)</managingEditor><generator>Blogger</generator><openSearch:totalResults>47</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/plaureano" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="plaureano" /><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-7349901047715470998.post-1460679472500523148</guid><pubDate>Wed, 17 Apr 2013 17:52:00 +0000</pubDate><atom:updated>2013-04-20T09:54:11.466+10:00</atom:updated><title>Building your own System.Reflection API from scratch, Part II: Round Tripping, Starting with Byte Zero</title><description>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://farm6.staticflickr.com/5029/5727866566_3363698efe.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://farm6.staticflickr.com/5029/5727866566_3363698efe.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;span style="font-family: times, times new roman, serif;"&gt;Reading and writing assemblies are a vital part of any .NET metaprogramming library.&lt;/span&gt;&lt;/div&gt;&lt;b&gt; &lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;b&gt;Duality at the heart of Tao&lt;/b&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;From the very beginning, I wanted to make sure that Tao was capable of both reading and writing assemblies. After being overwhelmed &lt;a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm"&gt;after reading parts of the ECMA-335 spec&lt;/a&gt;, I realized that the only way to effectively create Tao is to allow it to be able to both read and write in small portions that I could incrementally understand. PEVerify was ultimately useless in this case since it only deals with validating entire assemblies rather than just validating the small binary subsets that are contained in each assembly.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The Goal&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;In an ideal scenario, Tao should be able to read an assembly (or part of an assembly), load it into memory, and then write it back to the disk and the bytes should look exactly the same as the bytes that it used to load it into memory. Given that even the slightest byte read or written in the wrong position could mean the difference between a valid and an invalid assembly, I needed to use an approach that will prevent me from both regressive bugs, and act as a guide when I read or write the incorrect bytes from an assembly. The problem is that since no such tools exist, what’s the best approach for incrementally round tripping an assembly?&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Testing the seemingly untestable&lt;/b&gt;&lt;br /&gt;Writing your own System.Reflection API from scratch can be very difficult, and there’s absolutely no room for mistakes. That’s why I chose to write it &lt;a href="http://dannorth.net/introducing-bdd/"&gt;using a specification-driven approach&lt;/a&gt; since I didn’t have the time to test everything manually once the code was written. I needed an automated approach to testing since there was so many unwritten features that I still had to test. I also needed Tao to easily evolve as my understanding of the .NET assembly metadata increased over time. The idea was that I would use a hex editor to sample small byte arrays from an assembly and use those sample arrays as input to the code under test. Any particular round tripping feature in Tao would be considered “done” if what was read into memory could be serialized back to disk and match the sample array, byte-for-byte. At the time, &lt;a href="https://www.simple-talk.com/blogs/2011/03/15/anatomy-of-a-net-assembly-pe-headers/"&gt;there was so much that I didn’t know about the PE Format&lt;/a&gt;, so I wanted to start small, and use the tests as a guide to build my knowledge of the format over time. However, if you have a bunch of bytes in memory that I need to verify against a set of bytes on disk, how do you ensure that both sets of bytes match each other? &lt;br /&gt;&lt;br /&gt;That’s where Tao’s hash function comes in handy:&lt;script src="https://gist.github.com/philiplaureano/5422630.js"&gt;&lt;/script&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;These two extension methods form the heart of Tao’s self-verification system. It allowed me to incrementally read, write, and ultimately verify every single byte from a .NET assembly as my understanding of .NET assembly formats grew over time. The only problem I had at that point was that the test feedback was still too coarse. The hash functions allow you to verify a block of data, but how do you make it fail fast and track down the exact byte location if it writes the incorrect byte?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Divide and conquer&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;In order to effectively test Tao, we’ll need to use a hash function that tracks down an invalid byte at the very point where the byte is written. Each test should be able to immediately fail fast and fail hard if it encounters an invalid byte being read or written to disk. Tao cannot afford to wait until an entire chunk of bytes is written before it can be verified, simply because there’s currently no way to pinpoint the exact location of a byte mismatch if the chunk gets too large. For example, if you want to do a random write of a byte array with two million elements and have it fail at the instant that it writes the first invalid byte, how do you find the exact point where the mismatch occurred with just a hashing function performed on the entire byte array?&lt;br /&gt;&lt;br /&gt;As it turns out, if you have two side-by-side arrays of two million bytes each that Tao needs to diff, the simplest way to track down the differences between these two arrays using a hashing function is to eliminate the parts that they have in common. The bytes that will remain are the bytes that differ, and in this case, we only need to find the first byte position where there is a mismatch. One of the most effective ways to eliminate the common parts of these two byte arrays is to recursively halve and compare the two arrays into smaller chunks and compare each successively smaller chunk until the first mismatching byte is found. This approach allows Tao to compare even very large sets of data often using fewer than fifty comparisons, and aside from the recursive function call, the implementation speaks for itself:&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/philiplaureano/5423009.js"&gt;&lt;/script&gt;&lt;br /&gt;Each call to &lt;i&gt;getMismatchPosition&lt;/i&gt; recursively halves and compares the streams until the stream comparison size is only a single byte in length. Once the mismatch byte has been found, the comparison ends, and the local function returns the position where the mismatch occurred. Now that we have a way to pinpoint the exact byte mismatch position in any given stream, the next issue is finding a way to immediately fail a test at the very instant that any piece of Tao's code attempts to write an invalid chunk of bytes to a stream. In this case, this is where Tao's TracerStream class is immensely useful:&lt;br /&gt;&lt;script src="https://gist.github.com/philiplaureano/5423127.js"&gt;&lt;/script&gt;There's nothing particularly interesting about the StreamDecorator base class other than the fact that it wraps (or decorates) an existing stream so that we don't have to reimplement an entire stream in the TracerStream class if it just intercepts the write calls. What is interesting, however, is that the TracerStream class compares every chunk that is being written (just as it is being written) and immediately fails the write operation if the bytes being written don't match the bytes in the original stream. It's a very simple and effective way to incrementally verify parts of a binary, and it's very useful for verifying .NET assemblies. Now that we have written the basic tools for verifying byte streams in an assembly, the next task is to actually find samples that Tao can parse into memory, write to disk, and verify whether or not it matches the original bytes written into memory. Given this task, exactly what tools do we need to actually grab these byte samples, and start roundtripping these samples in Tao's tests?&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Finding binary samples using the right toolset&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;One of the best tools for analyzing and sampling raw .NET assemblies is &lt;a href="http://www.ntcore.com/exsuite.php"&gt;a tool called CFF Explorer&lt;/a&gt;. When I first started writing Tao, I used CFF Explorer’s hex editor to create binary dumps in C# that I could use as the expected byte arrays in Tao’s unit tests. For example, when I needed to &lt;a href="https://github.com/philiplaureano/Tao/blob/master/src/Tests/MSDosHeaderTests.n"&gt;test roundtriping MS-DOS headers&lt;/a&gt; with Tao, I used the sample bytes from CFF Explorer to verify that the DosHeaderWriter class was writing the correct bytes to its given output stream: &lt;script src="https://gist.github.com/philiplaureano/5423458.js"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Closing the loop&lt;/b&gt;&lt;br /&gt;In a nutshell, the above test case demonstrates how Tao began, and shows how I designed Tao for roundtripping from the very beginning. The idea was that in order to roundtrip an assembly, we need to have the tools to sample small parts of the assembly so that we can, in turn, write small tests that have expectations and assertions that determine what an assembly should look like in memory, despite the fact that we might yet not understand everything about the .NET assembly format. In the next series of posts, we'll dive into the .NET format with the knowledge that each one of these successive read, write, and roundtripping tests in Tao will ensure that it will always be reading and writing the correct assemblies, without fear of having any bug regressions. Fewer regressions means that it will be easier to work with the .NET assembly format and we can just move on to working on the next part of reading/writing/roundtripping an assembly. It's a simple idea with some big design reprecussions, and hopefully, these series of posts will show you the details of how I created Tao, and help you understand how I did it, as well as understand why I did it.  &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Coming up in the next post&lt;/b&gt;&lt;br /&gt;In the next post in this series, we'll talk about how to build the simplest possible assembly from scratch with ILASM and parse it with Tao. We'll also dive deeper into the CLR metadata format and explore the CLR metadata tables, and I'll briefly talk about how those tables bind an assembly together, and show you how Tao roundtrips those tables. Lastly, I'll also talk about some experimental uses for being able to directly manipulate those tables, and how they might just change the way you look at .NET assembly manipulation. One half of the post will talk about the tables, and the other half will talk about some of the crazy ideas which inspired me to write Tao in the first place, so stay tuned!   &lt;br /&gt;&lt;br /&gt;</description><link>http://www.bytecodeartist.net/2013/04/building-your-own-systemreflection-api_25.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-7510950721463849524</guid><pubDate>Tue, 09 Apr 2013 21:54:00 +0000</pubDate><atom:updated>2013-04-21T06:32:37.057+10:00</atom:updated><title>Building your own System.Reflection API from scratch, Part I: Choosing Nemerle</title><description>&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-hNknlTc1p7A/UVd5QG5B3PI/AAAAAAAAA9E/2Jt13GaEfMs/s1600/reinvention-e1342865771762.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="285" src="http://1.bp.blogspot.com/-hNknlTc1p7A/UVd5QG5B3PI/AAAAAAAAA9E/2Jt13GaEfMs/s400/reinvention-e1342865771762.jpg" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Sometimes you have to reinvent a better&amp;nbsp;light bulb&amp;nbsp;to understand how it works.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;b&gt;Introduction&amp;nbsp;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;About three years ago, I decided to take a break from &lt;a href="http://www.github.com/philiplaureano/linfu"&gt;working on LinFu&lt;/a&gt;. Although I was happy with some of the work that I was doing with &lt;a href="https://github.com/jbevain/cecil"&gt;Cecil&lt;/a&gt;&amp;nbsp;and IL rewriting, I wanted to understand the underlying abstractions that represent your every day .NET assembly. Even though there was some &lt;a href="https://www.google.com.au/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=1&amp;amp;cad=rja&amp;amp;ved=0CC8QFjAA&amp;amp;url=http%3A%2F%2Fwww.infoq.com%2Farticles%2FSimon-Cropp&amp;amp;ei=4HdXUbGpOIaEiAfOnIGACQ&amp;amp;usg=AFQjCNFlBi_IFIGkL75udZqbX-nkTwc7eA&amp;amp;sig2=lA_IDDSY9cpS0HiTco2-fQ"&gt;good IL rewriting work being done&lt;/a&gt; by other bytecoders like &lt;a href="https://twitter.com/SimonCropp"&gt;Simon Cropp&lt;/a&gt;, they only focus mostly on making small and surgical changes to assemblies, such as implementing the INotifyPropertyChanged interface, or making all public methods on a POCO class virtual.&lt;br /&gt;&lt;br /&gt;For me, being able to make small changes to the IL wasn't enough. I wanted to understand how to manipulate .NET assemblies so that I could make some "big" changes, such as:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Type cloning&lt;/li&gt;&lt;li&gt;Dead type elimination&lt;/li&gt;&lt;li&gt;Code migrations&lt;/li&gt;&lt;li&gt;Modifying signed .NET BCL assemblies at runtime&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Unfortunately, at the time of this post, there are currently no assembly manipulation tools that are capable of doing those things, and even the author of Cecil says that &lt;a href="https://groups.google.com/forum/?fromgroups=#!searchin/mono-cecil/cloning/mono-cecil/Yt9xQedjw54/p1WXl8LnTP8J"&gt;it doesn't support type cloning&lt;/a&gt;:&lt;br /&gt;&lt;blockquote class="tr_bq"&gt;&lt;span style="background-color: white; color: #222222; font-family: Arial, Helvetica, sans-serif; font-size: 13.333333015441895px;"&gt;There's no easy way to move one type from a module to&lt;/span&gt;&lt;span style="background-color: white; color: #222222; font-family: Arial, Helvetica, sans-serif; font-size: 13.333333015441895px;"&gt;another, as it involves taking a lot of decisions about what to do&lt;/span&gt;&lt;span style="background-color: white; color: #222222; font-family: Arial, Helvetica, sans-serif; font-size: 13.333333015441895px;"&gt;with references. Also Mono.Merge is completely dead&lt;/span&gt;&lt;/blockquote&gt;Given that there are no tools that are capable of doing what I wanted to do with assemblies, and given that I wanted to master assembly manipulation, I decided to take the next logical step: I was going to build my own reflection API from scratch.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The Tao of Metaprogramming&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;In this series of small posts, I'll talk about some of the design decisions as well as share some of my design notes that I have &lt;a href="http://github.com/philiplaureano/Tao"&gt;as I continue to build Tao&lt;/a&gt;, which is my own reflection metaprogramming API.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Choosing Nemerle&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;When I first starting this project over two years ago, I needed to use a language with built-in support for Design by Contract features since I was essentially going to create a library that builds .NET assemblies from scratch, and since I was starting with nothing, I needed a language that was robust enough to be fault-intolerant enough to tell me where I was failing, and why I was failing. Those days were some challenging days for me because all I had was the &lt;a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm"&gt;CLR Metadata Specification&lt;/a&gt; as a reference, and there were no programs at all (including PEVerify) that would tell me what or where my mistakes were being made.&lt;br /&gt;&lt;br /&gt;Essentially, I was flying blind, and I relied heavily on &lt;a href="https://github.com/rsdn/nemerle"&gt;Nemerle&lt;/a&gt; to be able to explicitly state my assumptions as runtime assertions. For example, &lt;a href="https://gist.github.com/philiplaureano/5279046"&gt;here's&amp;nbsp;how&lt;/a&gt; you can use Design by Contract macros and Non-nullable type macros in Nemerle to write more reliable code. The [NotNull] macro ensures that NullReferenceExceptions will be all but impossible, and the Design by Contract syntax extensions ensure that the code is always in a valid state, and those extensions are invaluable when you need to build an API that has no room for mistakes. In reading or writing .NET executables, even a single byte in the wrong position can give you an invalid assembly. The world of compiling and decompiling can a very cold and unforgiving world, and I needed the best tools I could find to make sure that my API was doing exactly what I intended it to do.&lt;br /&gt;&lt;br /&gt;Needless to say, building your own reflection API from scratch can be a very daunting task. Even today, as I look back on the work that I have already done with Tao, it's hard to imagine being able to get this far without the DbC language features that Nemerle has to offer, and in hindsight, I'm glad that I made that choice.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Coming up in the next post&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In the next post, I'll talk about some of the challenges of reading a raw .NET portable executable and turning it into something meaningful that a program can understand. For example, what does the format of a .NET assembly look like? How is it different from say, an unmanaged DLL/EXE file? More importantly, how do you actually write tests that ensure that the bytes that you're reading into memory are exactly the same as the ones loaded from the disk? Those questions were just some of the issues that I had to solve, and in the next post, I'll tell you exactly how I solved them, as well as talk about some of the tools I had to (re)invent in order to solve those problems. Meanwhile, stay tuned!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;</description><link>http://www.bytecodeartist.net/2013/04/building-your-own-systemreflection-api.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-hNknlTc1p7A/UVd5QG5B3PI/AAAAAAAAA9E/2Jt13GaEfMs/s72-c/reinvention-e1342865771762.jpg" height="72" width="72" /><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-5133624147200476013</guid><pubDate>Sun, 08 May 2011 07:59:00 +0000</pubDate><atom:updated>2011-05-08T17:59:18.763+10:00</atom:updated><title>Introduction to IL Rewriting with Cecil, Part 1–Rewriting FizzBuzz and the Art of Redirecting Method Calls</title><description>&lt;p&gt;&lt;a href="http://lh4.ggpht.com/__BgjkW_AfhY/TcZMptdTNtI/AAAAAAAAAOM/DFn_h-VkBlw/s1600-h/lesson1%5B3%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="lesson1" border="0" alt="lesson1" src="http://lh3.ggpht.com/__BgjkW_AfhY/TcZMqYG3uBI/AAAAAAAAAOQ/eGDCq85GGVg/lesson1_thumb%5B1%5D.png?imgmax=800" width="485" height="311" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;h6&gt;&lt;/h6&gt;&lt;h6&gt;The simplest possible code example that anyone can learn from.&lt;/h6&gt;&lt;h4&gt;Introduction&lt;/h4&gt;&lt;p&gt;When I first started learning IL rewriting and &lt;a href="https://github.com/jbevain/cecil"&gt;Cecil&lt;/a&gt; about several years ago, one of the difficulties that I struggled with was the fact that there were &lt;a href="http://stackoverflow.com/questions/1513319/mono-cecil-documentation-and-tutorials"&gt;very few practical examples&lt;/a&gt; on how to take an existing assembly and modify it at runtime. In many ways, I was stranded in heavily undocumented territory, and needless to say, this lack of documentation made it very difficult to learn how to do anything useful with Cecil.&lt;/p&gt;&lt;h4&gt;Meanwhile, in the Year 2011…&lt;/h4&gt;&lt;p&gt;It’s now 2011, and I think it’s safe to say that for many people, IL rewriting (much less Cecil) is still a &lt;a href="http://ayende.com/Blog/archive/2007/05/30/IL-Weaving--Mystery-wrapped-in-an-Enigma-containing-Frustration.aspx"&gt;“big mystery wrapped in an enigma containing frustration”&lt;/a&gt;. Indeed, Cecil is an incredible library that can let you do some incredible things, but at the same time, it can be very frustrating since the learning curve is still steep and there are still no practical guides for using it. As a user, there must be some sample code out there that shows how to do the most basic tasks with Cecil, right?&lt;/p&gt;&lt;h4&gt;Establishing the Feedback Loop&lt;/h4&gt;&lt;p&gt;In order to learn any skill (such as IL rewriting), we need to establish a simple feedback loop that allows users to easily experiment with the tools they are given so that they know:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;What went wrong if it doesn’t work &lt;/li&gt;&lt;li&gt;Where to fix it if it breaks &lt;/li&gt;&lt;li&gt;How to see the results of their experiments without getting mired in the implementation details of the tests themselves &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In this case, we’ll need to set up a basic environment that will let users experiment and learn how to modify assemblies at runtime with Cecil. We will need:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;A test fixture that loads a sample assembly and gives users the chance to modify it before reloading the modified assembly into memory (An NUnit base fixture) &lt;/li&gt;&lt;li&gt;A way to display/diagnose any invalid assembly errors that occur due to making changes to the original assembly (PEVerify) &lt;/li&gt;&lt;li&gt;To make it easy to change so that we can experiment with different approaches to modifying IL, thus “closing” the feedback loop &lt;/li&gt;&lt;/ol&gt;&lt;h4&gt;Lost in Bytecode&lt;/h4&gt;&lt;p&gt;Given these requirements, where would we even begin? It’s not every day that one decides to randomly parse .NET assemblies and learn how to change the underlying bytecode that ultimately defines their behavior. This can seem like a daunting task for even the most intelligent of budding interlopers, but fortunately for my readers, most of the work has already been done for you &lt;a href="https://github.com/philiplaureano/Cecil.Tutorials"&gt;in these examples&lt;/a&gt;. All you need to do is sit back and scroll down the page, as I proceed to tell you the “ins” and “outs” about Cecil, and the practical lessons learned from rewriting IL. With that in mind, let’s get started!&lt;/p&gt;&lt;h4&gt;&lt;/h4&gt;&lt;h4&gt;A WriteLine for Another WriteLine&lt;/h4&gt;&lt;p&gt;One of the simplest things that you can possibly do with Cecil is to swap a single static method call for another static method call with the same parameters and the same return type. (It’s a fairly simple operation since both methods have the same signature, and you don’t need to add any additional instructions to make it happen). In this case, I opted to swap all calls to Console.WriteLine() with calls to FakeConsole.WriteLine():&lt;/p&gt;&lt;script src="https://gist.github.com/961185.js?file=gistfile1.cs"&gt;&lt;/script&gt;  &lt;p&gt;As you can see from the example above, I used a simple LINQ query to identify all the call instructions that needed to be modified. More experienced Cecil users will probably notice that I decided to rewrite all method calls to point to FakeConsole.WriteLine() instead of individually checking to make sure that the method call I was replacing was indeed Console.WriteLine(). Indeed, that was an intentional move, given that the FizzBuzz.Print() method doesn’t make any other external calls to any other methods besides Console.WriteLine(). &lt;/p&gt;&lt;p&gt;Assuming that I somehow created an instruction that caused an invalid modified assembly, however, how would I be able to know what went wrong, much less know how to fix it?&lt;/p&gt;&lt;h4&gt;PEVerify, how do I love &lt;strike&gt;and hate&lt;/strike&gt; thee…&lt;/h4&gt;&lt;p&gt;As it turns out, there is &lt;a href="http://msdn.microsoft.com/en-us/library/62bwd2yd(v=vs.80).aspx"&gt;a tool called PEVerify.exe&lt;/a&gt; that can tell you whether or not the assemblies that you modify with Cecil are valid or invalid. For example, if I were to remove all the IL instructions out of the FizzBuzz.Print() method, PEVerify would give me the following error message:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/__BgjkW_AfhY/TcZMq1E6y3I/AAAAAAAAAOU/15WQkC-UwR4/s1600-h/peverify1%5B4%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="peverify1" border="0" alt="peverify1" src="http://lh5.ggpht.com/__BgjkW_AfhY/TcZMroOoeYI/AAAAAAAAAOY/TcE3hpjaVuY/peverify1_thumb%5B2%5D.png?imgmax=800" width="485" height="156" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;h6&gt;(Believe me, it’s much prettier when it’s zoomed out)&lt;/h6&gt;&lt;p&gt;PEVerify will examine any given assembly and be able to tell you whether or not the compiler (or in this case, &lt;em&gt;you&lt;/em&gt;, the human compiler) made any mistakes in creating the assembly. It can be a very useful tool, and that’s why I modified the sample test fixture to run PEVerify right after the user modifies the sample assembly. If you don’t already have PEVerify installed, make sure you &lt;a href="http://stackoverflow.com/questions/1915182/where-can-i-download-peverify-exe-tool"&gt;download it&lt;/a&gt; and configure the Lesson1 app.config file to point to where PEVerify is installed:&lt;/p&gt;&lt;script src="https://gist.github.com/961203.js?file=gistfile1.xml"&gt;&lt;/script&gt;&lt;br /&gt;Once PEVerify has been configured as part of the tests, the rest is up to your imagination.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Exploring Method Replacement and Beyond&lt;/h4&gt;&lt;p&gt;Now that the basic IL rewriting setup has been laid out for you, the onus is on you to explore the possibilities with Cecil and IL rewriting, even if it means that you have to start with some small, basic steps. In the next installment in this series, I’ll show you how to use PEVerify and Cecil to keep the stack balanced so that you can do things like swap static method calls for instance method calls, and even do things like install runtime hooks so you can change your code as your application is running. Stay tuned!&lt;/p&gt;</description><link>http://www.bytecodeartist.net/2011/05/introduction-to-il-rewriting-with-cecil.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/__BgjkW_AfhY/TcZMqYG3uBI/AAAAAAAAAOQ/eGDCq85GGVg/s72-c/lesson1_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-6733261898992208376</guid><pubDate>Wed, 04 May 2011 01:18:00 +0000</pubDate><atom:updated>2011-05-04T14:08:28.704+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">codeproject</category><title>Dynamically Intercepting Thrown Exceptions with LinFu.AOP 2.0</title><description>&lt;p&gt;&lt;a href="http://lh6.ggpht.com/__BgjkW_AfhY/TcCklRZIVLI/AAAAAAAAAN4/0QTo611dmt0/s1600-h/exception%5B4%5D.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="exception" border="0" alt="exception" src="http://lh5.ggpht.com/__BgjkW_AfhY/TcCkmRgPliI/AAAAAAAAAN8/lSXHZS2VGvQ/exception_thumb%5B2%5D.jpg?imgmax=800" width="493" height="263" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;h6&gt;&lt;font style="font-weight: normal"&gt;A screenshot of LinFu dynamically catching a thrown exception.&lt;/font&gt;&lt;/h6&gt;&lt;h4&gt;On Error, Resume Interception&lt;/h4&gt;&lt;p&gt;Another useful thing that LinFu.AOP allows you to do is to intercept (and rethrow) exceptions within your applications at runtime. LinFu makes it so easy, in fact, that all you have to do is add the following lines to your CSProj file:&lt;/p&gt;&lt;script src="https://gist.github.com/954549.js"&gt;&lt;/script&gt;&lt;br /&gt;&lt;h4&gt;Exceptionally Simple&lt;/h4&gt;To use LinFu.AOP's dynamic exception handling capabilities, all you need to do is make the following call to handle all exceptions being thrown in your application: &lt;br /&gt;&lt;script src="https://gist.github.com/954554.js?file=gistfile1.cs"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Try/Catch Me, If You Can&lt;/h4&gt;The call to ExceptionHandlerRegistry.SetHandler tells LinFu to hook the SampleExceptionHandler into your application so that all exceptions that will be thrown will automatically be handled by the given exception handler. Under normal circumstances (where interception is disabled), the call to account.Deposit() will cause the app to crash, but &lt;a href="http://github.com/philiplaureano/LinFu.AOP.Examples/tree/CatchingThrownExceptions"&gt;as this example shows&lt;/a&gt;, LinFu.AOP was able to intercept the thrown exception before it could crash the rest of the app.&lt;p&gt;&lt;/P&gt;What makes this even more interesting, however, is the IExceptionHandlerInfo instance that describes the context from which the exception was thrown:&lt;br /&gt;&lt;script src="https://gist.github.com/954572.js?file=gistfile1.cs"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;More Information Than You Can Throw An Exception At&lt;/h4&gt;&lt;br /&gt;The IExceptionHandlerInfo interface has enough information to describe the method that caused the exception, as well as having properties such as ShouldSkipRethrow that allow you to decide whether or not LinFu should just swallow the exception and keep running the program as if an exception was never thrown. The ReturnValue property, in turn, allows you to alter the return value of a given method in case you want to resume the method and provide an alternate return value as if no exceptions were ever thrown. &lt;br /&gt;&lt;br /&gt;As you can see, LinFu.AOP makes it really easy to transparently handle exceptions in your applications, and if this post saves at least a few developers a few headaches from having to manually diagnose their applications, then I'd consider it to be a gratifying success.&lt;br /&gt;&lt;br /&gt;Enjoy!&lt;br /&gt;&lt;br /&gt;EDIT: You can get the code examples for LinFu.AOP's dynamic exception handling &lt;a href="http://github.com/philiplaureano/LinFu.AOP.Examples/tree/CatchingThrownExceptions"&gt;here&lt;/a&gt;.</description><link>http://www.bytecodeartist.net/2011/05/dynamically-intercepting-thrown.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/__BgjkW_AfhY/TcCkmRgPliI/AAAAAAAAAN8/lSXHZS2VGvQ/s72-c/exception_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-683302266063776539</guid><pubDate>Thu, 28 Apr 2011 01:30:00 +0000</pubDate><atom:updated>2011-05-04T14:08:41.874+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">codeproject</category><title>Intercepting Console.WriteLine and Other Third-Party Method Calls with LinFu.AOP 2.0</title><description>&lt;a href="http://lh3.ggpht.com/__BgjkW_AfhY/TbjDLCN6W-I/AAAAAAAAANo/QBT-uVrkmHs/s1600-h/ConsoleInterceptor5.png"&gt;&lt;img alt="ConsoleInterceptor" border="0" height="247" src="http://lh4.ggpht.com/__BgjkW_AfhY/TbjDLpJJ_NI/AAAAAAAAANs/u6DSMW2IXXo/ConsoleInterceptor_thumb3.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="ConsoleInterceptor" width="448" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h4&gt;Worth a thousand words&lt;/h4&gt;In case you are wondering, yes, that is a screenshot of LinFu.AOP intercepting calls to Console.WriteLine() at runtime.&lt;br /&gt;One of the more useful things that LinFu.AOP can do is intercept calls to third-party assemblies that aren’t necessarily under your control. In fact, LinFu makes it so easy that all you have to do to make the interception happen is add the reference to LinFu like the following lines to your CSProj file just like I did with my SampleLibrary.csproj file:&lt;br /&gt;&lt;span style="color: teal;"&gt;   &lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;&lt;span style="color: teal;"&gt;&lt;span style="color: black;"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;br /&gt;&amp;lt;PostWeaveTaskLocation&amp;gt;$(MSBuildProjectDirectory)\$(OutputPath)\..\..\..\lib\LinFu.Core.dll&amp;lt;/PostWeaveTaskLocation&amp;gt;&lt;br /&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;br /&gt;&amp;lt;UsingTask TaskName="PostWeaveTask" AssemblyFile="$(PostWeaveTaskLocation)" /&amp;gt;&lt;br /&gt;&amp;lt;Target Name="AfterBuild"&amp;gt;&lt;br /&gt;&amp;lt;PostWeaveTask TargetFile="$(MSBuildProjectDirectory)\$(OutputPath)$(MSBuildProjectName).dll" InterceptAllMethodCalls="true" /&amp;gt;&lt;br /&gt;&amp;lt;/Target&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="color: teal;"&gt;&lt;br /&gt;&lt;h4&gt;&lt;span style="color: #333333;"&gt;‘Automagically’ Delicious&lt;/span&gt;&lt;/h4&gt;&lt;div&gt;&lt;span style="color: black; font-family: Verdana;"&gt;Once you reload and rebuild the solution, LinFu.AOP will automatically modify your code after the build runs so that you can intercept it at runtime. LinFu does this by adding hooks to your code so you can change it as the program is running. In this case, I casted the modified BankAccount class to an IModifiableType instance so that I could add my custom ConsoleInterceptor instance:&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color: teal;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } &lt;/style&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;// Create the BankAccount class just like normal...&lt;/pre&gt;&lt;pre class="brush: csharp;"&gt;var account = new BankAccount(100);&lt;/pre&gt;&lt;pre class="brush: csharp;"&gt;// Notice how LinFu.AOP automatically implements IModifiableType so you can intercept/replace method calls at runtime&lt;br /&gt;var modifiableType = account as IModifiableType;&lt;br /&gt;if (modifiableType != null)&lt;br /&gt;modifiableType.MethodCallReplacementProvider = new WriteLineMethodReplacementProvider();&lt;br /&gt;&lt;br /&gt;account.Deposit(100);&lt;/pre&gt;The WriteLineMethodReplacementProvider class, in turn, determines the method calls that should be intercepted at runtime:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public class WriteLineMethodReplacementProvider : IMethodReplacementProvider&lt;br /&gt;{&lt;br /&gt;public bool CanReplace(object host, IInvocationInfo info)&lt;br /&gt;{&lt;br /&gt;var declaringType = info.TargetMethod.DeclaringType;&lt;br /&gt;if (declaringType != typeof(System.Console))&lt;br /&gt;return false;&lt;br /&gt;&lt;br /&gt;// We're only interested in replacing Console.WriteLine()&lt;br /&gt;var targetMethod = info.TargetMethod;&lt;br /&gt;return targetMethod.Name == "WriteLine";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public IInterceptor GetMethodReplacement(object host, IInvocationInfo info)&lt;br /&gt;{&lt;br /&gt;return new ConsoleInterceptor();&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;h4&gt;Choosing which methods to intercept&lt;/h4&gt;As you can see from the example above, this class ensures that only calls to Console.WriteLine() are ever intercepted. The ConsoleInterceptor itself is responsible for replacing and intercepting the Console.WriteLine() method itself:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public class ConsoleInterceptor : IInterceptor&lt;br /&gt;{&lt;br /&gt;public object Intercept(IInvocationInfo info)&lt;br /&gt;{&lt;br /&gt;var targetType = info.TargetMethod.DeclaringType;&lt;br /&gt;var target = info.Target;&lt;br /&gt;var targetMethod = info.TargetMethod;&lt;br /&gt;var arguments = info.Arguments;&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Intercepted method named '{0}'", targetMethod.Name);&lt;br /&gt;&lt;br /&gt;// Call the original WriteLine method&lt;br /&gt;targetMethod.Invoke(null, arguments);&lt;br /&gt;&lt;br /&gt;// Console.WriteLine doesn't have a return value so it's OK to return null)&lt;br /&gt;return null;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;The most interesting part about the code example is how LinFu.AOP adds the method call hooks without touching a single line of the source code. All of the IL rewriting is done behind the scenes so you won’t have to worry about the gory details of using an AOP framework in your legacy code. The beauty of this approach is that it allows you to intercept any method call, even if that method call is a part of the .NET base class libraries. &lt;br /&gt;You can find the LinFu.AOP.Examples library &lt;a href="https://github.com/philiplaureano/LinFu.AOP.Examples"&gt;here at Github&lt;/a&gt;.&lt;br /&gt;NOTE: Please intercept BCL method calls responsibly.</description><link>http://www.bytecodeartist.net/2011/04/intercepting-consolewriteline-and-other.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/__BgjkW_AfhY/TbjDLpJJ_NI/AAAAAAAAANs/u6DSMW2IXXo/s72-c/ConsoleInterceptor_thumb3.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-39650941617513329</guid><pubDate>Wed, 27 Apr 2011 09:48:00 +0000</pubDate><atom:updated>2011-05-04T14:09:02.217+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">codeproject</category><title>Beyond Duck Typing with LinFu.DynamicObject: Creating Types that can Change at Runtime</title><description>&lt;h4&gt;A Post-Easter Egg&lt;/h4&gt;One of the hidden features that LinFu.DynamicObject has is the ability to dynamically add properties and methods to itself using a shared type definition at runtime. In other words, you can have two or more LinFu.DynamicObject instances share the same DynamicType, and any changes you make to that type will be propagated to all LinFu.DynamicObject instances that share that same type:&lt;br /&gt;&lt;pre&gt;using LinFu.Reflection.Extensions;&lt;br /&gt;using NUnit.Framework;&lt;br /&gt;&lt;br /&gt;namespace LinFu.Reflection.Tests&lt;br /&gt;{&lt;br /&gt;[TestFixture]&lt;br /&gt;public class DynamicTypeTests&lt;br /&gt;{&lt;br /&gt;[Test]&lt;br /&gt;public void ShouldBeAbleToShareTheSameDynamicType()&lt;br /&gt;{&lt;br /&gt;var typeSpec = new TypeSpec() { Name = "Person" };&lt;br /&gt;&lt;br /&gt;// Add an age property &lt;br /&gt;typeSpec.AddProperty("Age", typeof(int));&lt;br /&gt;&lt;br /&gt;// Attach the DynamicType named 'Person' to a bunch of dynamic objects&lt;br /&gt;var personType = new DynamicType(typeSpec);&lt;br /&gt;var first = new DynamicObject();&lt;br /&gt;var second = new DynamicObject();&lt;br /&gt;&lt;br /&gt;first += personType;&lt;br /&gt;second += personType;&lt;br /&gt;&lt;br /&gt;// Use both objects as persons&lt;br /&gt;IPerson firstPerson = first.CreateDuck&amp;lt;IPerson&amp;gt;();&lt;br /&gt;IPerson secondPerson = second.CreateDuck&amp;lt;IPerson&amp;gt;();&lt;br /&gt;&lt;br /&gt;firstPerson.Age = 18;&lt;br /&gt;secondPerson.Age = 21;&lt;br /&gt;&lt;br /&gt;Assert.AreEqual(18, firstPerson.Age);&lt;br /&gt;Assert.AreEqual(21, secondPerson.Age);&lt;br /&gt;&lt;br /&gt;// Change the type so that it supports the INameable interface&lt;br /&gt;typeSpec.AddProperty("Name", typeof(string));&lt;br /&gt;INameable firstNameable = first.CreateDuck&amp;lt;INameable&amp;gt;();&lt;br /&gt;INameable secondNameable = second.CreateDuck&amp;lt;INameable&amp;gt;();&lt;br /&gt;&lt;br /&gt;firstNameable.Name = "Foo";&lt;br /&gt;secondNameable.Name = "Bar";&lt;br /&gt;&lt;br /&gt;Assert.AreEqual("Foo", firstNameable.Name);&lt;br /&gt;Assert.AreEqual("Bar", secondNameable.Name);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;h4&gt;Evolving Ducks&lt;/h4&gt;Most of the code above is self-explanatory, and the most interesting part about this code is the fact that it has two DynamicObject instances that share the same DynamicType instance. Once the Age property was added to the DynamicType definition, both first and second DynamicObjects automatically ‘inherited’ the additional Age property that was added to the DynamicType at runtime. Another interesting piece of code was the duck typing call to the IPerson interface, which wasn’t possible until after the Age property was added:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;// Use both objects as persons&lt;br /&gt;IPerson firstPerson = first.CreateDuck&lt;iperson&gt;();&lt;br /&gt;IPerson secondPerson = second.CreateDuck&lt;iperson&gt;();&lt;br /&gt;&lt;br /&gt;firstPerson.Age = 18;&lt;br /&gt;secondPerson.Age = 21;&lt;br /&gt;&lt;br /&gt;Assert.AreEqual(18, firstPerson.Age);&lt;br /&gt;Assert.AreEqual(21, secondPerson.Age);&lt;/iperson&gt;&lt;/iperson&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see from the example above, LinFu.DynamicObject is smart enough to change its definition every time the attached DynamicType definition changes, and that’s why it was also able to duck type itself to the INameable interface:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;// Change the type so that it supports the INameable interface // Change the type so that it supports the INameable interface&lt;br /&gt;            typeSpec.AddProperty("Name", typeof(string));&lt;br /&gt;            INameable firstNameable = first.CreateDuck&amp;lt;INameable&amp;gt;();&lt;br /&gt;            INameable secondNameable = second.CreateDuck&amp;lt;INameable&amp;gt;();&lt;br /&gt;&lt;br /&gt;            firstNameable.Name = "Foo";&lt;br /&gt;            secondNameable.Name = "Bar";&lt;br /&gt;&lt;br /&gt;            Assert.AreEqual("Foo", firstNameable.Name);&lt;br /&gt;            Assert.AreEqual("Bar", secondNameable.Name);&lt;br /&gt;&lt;/pre&gt;Pretty straightforward, isn't it? (LinFu.DynamicObject has had this feature for well over 4 years, but I never got around to &lt;a href="http://github.com/philiplaureano/LinFu.DynamicObject"&gt;publishing it&lt;/a&gt; until now). Enjoy!</description><link>http://www.bytecodeartist.net/2011/04/beyond-duck-typing-with.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-1424244181117874103</guid><pubDate>Tue, 26 Apr 2011 00:40:00 +0000</pubDate><atom:updated>2011-05-04T14:09:18.700+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">codeproject</category><title>Duck Typing with LinFu &amp; C# 4.0’s Dynamic Keyword</title><description>&lt;h4&gt;The Lame Duck&lt;/h4&gt;&lt;p&gt;When C# 4.0 came out with the &lt;em&gt;dynamic&lt;/em&gt; keyword, I was pretty excited over the prospect of having Ruby-like features finally being baked in to the C# language itself, but as &lt;a href="http://twitter.com/davetchepak"&gt;one of my twitter followers&lt;/a&gt; and friends pointed out in &lt;a href="http://www.davesquared.net/2009/06/dynamic-disappointment.html"&gt;one of their posts from a few years ago&lt;/a&gt;, C# 4.0 still lacks duck typing support, and the .NET BCL doesn’t seem to have anything that could do something similar to the following code:&lt;/p&gt;&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; ICanAdd&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Add(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SomethingThatAdds&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; ICanAdd _adder;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; SomethingThatAdds(ICanAdd adder)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;_adder = adder;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; FirstNumber { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; SecondNumber { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; AddNumbers()&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _adder.Add(FirstNumber, SecondNumber);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;There has to be some way to construct an object at runtime and map it to an &lt;em&gt;ICanAdd&lt;/em&gt; interface, but the problem is that the current .NET Base Class Libraries don’t seem to have a solution for this problem. As Dave Tchepak pointed out in his post, the following dynamic code will fail miserably at runtime:&lt;/p&gt;&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Dynamic : DynamicObject&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;Dictionary&amp;lt;String, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt; members = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; TrySetMember(SetMemberBinder binder, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;members[binder.Name] = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; TryGetMember(GetMemberBinder binder, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; result)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; members.TryGetValue(binder.Name, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; result);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #008000"&gt;// ..and the test would look like:&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[Test]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CannotUseDynamicAdderForAnythingUseful()&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;dynamic adder = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dynamic();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;adder.Add = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Func&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;((first, second) =&amp;gt; first + second);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;var somethingThatCanAdd = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SomethingThatAdds(adder); &lt;span style="color: #008000"&gt;/* Fails here at runtime */&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;somethingThatCanAdd.FirstNumber = 10;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;somethingThatCanAdd.SecondNumber = 20;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;Assert.That(somethingThatCanAdd.AddNumbers(), Is.EqualTo(30));&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;   .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The problem is that the runtime isn’t smart enough to figure out that there has to be a duck-typing cast to the ICanAdd interface in order to use the &lt;em&gt;SomethingThatCanAdd&lt;/em&gt; class, and that’s where LinFu’s DynamicObject comes in handy.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;If it walks and quacks like a duck, then it’s all good&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="https://github.com/philiplaureano/LinFu.DynamicObject"&gt;LinFu.DynamicObject&lt;/a&gt; is flexible enough that it can let you build object instances at runtime and then ‘strongly’ duck type those object instances to any interface that matches the intended duck type. In this case, we need to find a way to build up something that can map to an &lt;em&gt;ICanAdd&lt;/em&gt; interface instance so that it can be used by the &lt;em&gt;SomethingThatAdds&lt;/em&gt; class:&lt;/p&gt;&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;[Test]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CanCreateADynamicAdder()&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;var adder = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DynamicObject();&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;CustomDelegate addBody = &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] args)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;{&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a = (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;)args[0];&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b = (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;)args[1];&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a + b;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #008000"&gt;// Map LinFu's DynamicObject to an ICanAdd interface&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;var linfuDynamicObject = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DynamicObject(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;());&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;var returnType = &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;var parameterTypes = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Type[] { &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;), &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;) };&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;linfuDynamicObject.AddMethod("&lt;span style="color: #8b0000"&gt;Add&lt;/span&gt;", addBody, returnType, parameterTypes);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #008000"&gt;// If it looks like a duck...&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;Assert.IsTrue(linfuDynamicObject.LooksLike&amp;lt;ICanAdd&amp;gt;());&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;&lt;span style="color: #008000"&gt;// ...then it must be a duck, right?&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;var somethingThatCanAdd = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SomethingThatAdds(adder.CreateDuck&amp;lt;ICanAdd&amp;gt;());&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;somethingThatCanAdd.FirstNumber = 10;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;somethingThatCanAdd.SecondNumber = 20;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;Assert.AreEqual(somethingThatCanAdd.AddNumbers(), 30);&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&amp;nbsp;&lt;font face="Verdana"&gt;The call to the &lt;em&gt;DynamicObject.CreateDuck()&lt;/em&gt; method does all the heavy lifting for you so you don’t have to worry about the details of how to make the object behave like a duck. It just works, and that’s the power that LinFu offers.&lt;/font&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;font face="Verdana"&gt;(EDIT: You can grab the source code and examples for LinFu.DynamicObject &lt;a href="https://github.com/philiplaureano/LinFu.DynamicObject"&gt;here at Github&lt;/a&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;font face="Verdana"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9b0dbc35-6901-42e8-9e19-93d9697cdb16" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/linfu" rel="tag"&gt;linfu&lt;/a&gt;,&lt;a href="http://technorati.com/tags/dynamicobject" rel="tag"&gt;dynamicobject&lt;/a&gt;,&lt;a href="http://technorati.com/tags/csharp4" rel="tag"&gt;csharp4&lt;/a&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/font&gt;</description><link>http://www.bytecodeartist.net/2011/04/duck-typing-with-linfu-c-40s-dynamic.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-3064656714818020077</guid><pubDate>Sun, 24 Apr 2011 11:28:00 +0000</pubDate><atom:updated>2011-04-24T21:28:42.129+10:00</atom:updated><title>A CQRS and an AOP expert walk into an Oredev bar…</title><description>&lt;h4&gt;&lt;a href="http://lh6.ggpht.com/__BgjkW_AfhY/TbQJZ13WwOI/AAAAAAAAANg/XRcG3xwtn8o/s1600-h/5166054200_220bec62b0_b%5B3%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="5166054200_220bec62b0_b" border="0" alt="5166054200_220bec62b0_b" src="http://lh4.ggpht.com/__BgjkW_AfhY/TbQJaUAqvbI/AAAAAAAAANk/VZUdmCsrY38/5166054200_220bec62b0_b_thumb%5B1%5D.jpg?imgmax=800" width="451" height="313" /&gt;&lt;/a&gt;&lt;/h4&gt;  &lt;h4&gt;A mind-bending experience&lt;/h4&gt;  &lt;p&gt;&lt;a href="http://plaureano.blogspot.com/2010/11/redev-2010-part-1.html"&gt;Back in November 2010 of last year&lt;/a&gt;, I remember that &lt;a href="https://twitter.com/#!/gregyoung"&gt;a stranger&lt;/a&gt; walked up to me at the Green Lion Inn Bar after the Oredev conference and told me that my notion of aspects, and of object-oriented design was actually a limited version of some of the message passing capabilities of some old language named &lt;a href="http://en.wikipedia.org/wiki/Smalltalk"&gt;Smalltalk&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;He asked me to imagine for a moment that everything in OOD is a message. In original OO terminology, he said, there were methods, and messages. Whenever you call a method (in today’s terminology), what you are implicitly doing is sending a message to an object telling it to execute the code associated with that message (the method). &lt;/p&gt;  &lt;p&gt;For me, the concept of methods executing as a result of implicitly sent messages seemed fairly straightforward, but the discussion took an interesting turn.&lt;/p&gt;  &lt;p&gt;He said, “Imagine that these messages can be sent from anywhere. They can be running within the same process, running on your machine, or they can even be coming from a machine that is located halfway across the world.”&lt;/p&gt;  &lt;p&gt;The concept of Remote Procedure Calls wasn’t anything new—but the next thing he said piqued my interest.&lt;/p&gt;  &lt;p&gt;“If every method is merely a message to be sent, then any method call can be remoted [if you rewrite it with IL].”&lt;/p&gt;  &lt;p&gt;In effect, what Greg was proposing was to create a messaging system that was so granular that one could transparently take any method call on any given machine and have it execute on another machine or process without modifying the original source code. In theory, one could use such a system to split a single process so that it runs parallel on multiple machines, all without modifying the original source code.&lt;/p&gt;  &lt;p&gt;In other words, he was proposing automatic parallelization of any application using AOP, and IL rewriting.&lt;/p&gt;  &lt;p&gt;Needless to say, my jaw hit the floor, and I was left speechless.&lt;/p&gt;  </description><link>http://www.bytecodeartist.net/2011/04/cqrs-and-aop-expert-walk-into-oredev.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/__BgjkW_AfhY/TbQJaUAqvbI/AAAAAAAAANk/VZUdmCsrY38/s72-c/5166054200_220bec62b0_b_thumb%5B1%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-1673201780192952882</guid><pubDate>Sun, 24 Apr 2011 10:29:00 +0000</pubDate><atom:updated>2011-04-24T20:29:13.851+10:00</atom:updated><title>NDC Talk Preview: “Ten Simple Rules for Rewriting IL on the Common Language Runtime”</title><description>&lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p&gt;One of the things that frustrated me when I first started learning how to write (and rewrite) IL back in the early 2000’s was the fact that there were very few practical examples on how to do anything useful with IL. In fact, the only reliable learning tools that were available around at the time was &lt;em&gt;PEVerify.exe&lt;/em&gt;, and IL disassembler. In the six to seven years since I first started writing/rewriting IL, I often asked myself what it would take to write a useful IL tutorial that would be easy enough for someone with a minimal C# background to learn, and yet be informative enough that it would be useful to someone who has written an AOP framework of their own.&lt;/p&gt;  &lt;h4&gt;Fast forward to 2011&lt;/h4&gt;  &lt;p align="center"&gt;&lt;a href="http://lh4.ggpht.com/__BgjkW_AfhY/TbP7ddnbAsI/AAAAAAAAANY/N2fEm9WxOBA/s1600-h/frustrated%5B3%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="frustrated" border="0" alt="frustrated" src="http://lh5.ggpht.com/__BgjkW_AfhY/TbP7eJlZKGI/AAAAAAAAANc/221X9vuJTOA/frustrated_thumb%5B1%5D.png?imgmax=800" width="282" height="318" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Over the years, I have learned some really hard lessons about IL, and now that &lt;a href="http://plaureano.blogspot.com/2011/04/speaking-about-ten-simple-rules-for.html"&gt;I’ll be speaking at NDC 2011&lt;/a&gt;, it will be the first time that I will get to share what I have learned with the .NET community. Make no mistake—IL writing can be a &lt;a href="http://ayende.com/Blog/archive/2007/05/30/IL-Weaving--Mystery-wrapped-in-an-Enigma-containing-Frustration.aspx"&gt;“mystery wrapped in frustration containing an enigma”&lt;/a&gt;, but the good news is that I have been through many of those pitfalls, and now I can help other developers understand what’s going on when those errors occur, and I can even show you how to fix those errors.&lt;/p&gt;  &lt;h4&gt;Ten Lessons Learned&lt;/h4&gt;  &lt;p&gt;In fact, there are ten basic principles that you can follow that will make it easier for you to understand and work with IL writing/rewriting. These principles are:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Rule #1:&lt;/strong&gt; If you don’t understand IL, then learn from the C# compiler&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #2:&lt;/strong&gt; Always keep the stack in a balanced state; One push = one pop&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #3:&lt;/strong&gt; Don’t forget to explicitly box/unbox your types&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #4:&lt;/strong&gt; &lt;em&gt;PEVerify.exe &lt;/em&gt;can be your best friend and worst enemy at the same time&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #5:&lt;/strong&gt; Use &lt;em&gt;Console.WriteLine()&lt;/em&gt; to triangulate errors in your IL at runtime; Use it well, and use it often&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #6:&lt;/strong&gt; GOTO statements in IL are a Good Thing™&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #7:&lt;/strong&gt; Emit as few IL instructions as possible&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #8:&lt;/strong&gt; Almost anything can be modified, including sealed classes and final methods&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #9:&lt;/strong&gt; You can replace any given method call as long as you replace it with a method that leaves the stack in the same state&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rule #10:&lt;/strong&gt; IL can be learned like any other programming language; practice makes perfect.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Many of these lessons learned are fairly straightforward, while some principles will require a bit more explanation during the talk. No matter what your level of experience in IL might be, I guarantee that this is one informative session that you won’t want to miss. Stay tuned, and I’ll see you all at NDC 2011!&lt;/p&gt;  </description><link>http://www.bytecodeartist.net/2011/04/ndc-talk-preview-ten-simple-rules-for.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/__BgjkW_AfhY/TbP7eJlZKGI/AAAAAAAAANc/221X9vuJTOA/s72-c/frustrated_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-3533155403840848514</guid><pubDate>Sat, 23 Apr 2011 02:45:00 +0000</pubDate><atom:updated>2011-04-23T12:45:56.644+10:00</atom:updated><title>I’m joining Readify in Sydney, Australia!</title><description>&lt;p align="center"&gt;&lt;a href="http://lh5.ggpht.com/__BgjkW_AfhY/TbI9XEtOsEI/AAAAAAAAANI/5SF9nyL-mYk/s1600-h/readify%5B2%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="readify" border="0" alt="readify" src="http://lh4.ggpht.com/__BgjkW_AfhY/TbI9XoTrsCI/AAAAAAAAANM/dT6_TcXPCqw/readify_thumb.png?imgmax=800" width="237" height="187" /&gt;&lt;/a&gt;&lt;/p&gt;      &lt;h4&gt;Way more than the average job&lt;/h4&gt;  &lt;p&gt;After a year of working for an investment bank in Hong Kong, I realized that I needed a job more meaningful than just churning out number-crunching apps for people that I would never meet or rarely ever see. Opportunity, as the saying goes, rarely ever knocks twice at a person’s door, and when &lt;a href="http://readify.net"&gt;Readify&lt;/a&gt; said that they would let me work for them after I turned them down last year to go to Hong Kong, I knew that this opportunity would never come again, and I decided to take it. It’s not every day that one gets a chance to work with some of the best developers in the .NET community, and I am humbled by the fact that I will get the chance to work with them.&lt;/p&gt;  &lt;h4&gt;Hong Kong, in Retrospect&lt;/h4&gt;  &lt;p&gt;In hindsight, living in Hong Kong has been a very comfortable experience. The city is relatively small, it has an excellent public transportation system, and you can practically get anything you want within a few keystrokes on your keyboard, a call from your mobile phone, or just by going to Mongkok to shop for the latest gadgets. For me, the problem wasn’t living in the city per se—it was the idea of having to indefinitely work on the same business apps within the same business domain for years upon years on end. Working in investment banking IT has certainly been a lucrative venture for me, but I wanted a role that would let me go out and help other clients and developers be more effective at what they do. At the same time, I wanted to work within the confines of a relatively smaller and close-knit company that gave their people more control over how they work rather than having them go through so many processes because of legal restrictions or mandated institutional regulations. &lt;/p&gt;  &lt;h4&gt;To Sydney, and Beyond&lt;/h4&gt;  &lt;p align="left"&gt;&lt;a href="http://lh3.ggpht.com/__BgjkW_AfhY/TbI9YX3PYJI/AAAAAAAAANQ/SZ7cS9KF1oc/s1600-h/3563650121_b659b9de96%5B4%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="3563650121_b659b9de96" border="0" alt="3563650121_b659b9de96" src="http://lh5.ggpht.com/__BgjkW_AfhY/TbI9Yzrv-vI/AAAAAAAAANU/v6ZM83BBARw/3563650121_b659b9de96_thumb%5B2%5D.jpg?imgmax=800" width="437" height="309" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Shortly after &lt;a href="http://plaureano.blogspot.com/2011/04/speaking-about-ten-simple-rules-for.html"&gt;my upcoming talk&lt;/a&gt; at &lt;a href="http://ndc2011.no/"&gt;NDC 2011&lt;/a&gt;, I’ll be packing all my things and I’ll be moving to Sydney, Australia to work with the folks at Readify. There’s no telling what the future holds for me, but if this job opportunity is any indication, then the future looks bright indeed.&lt;/p&gt;  </description><link>http://www.bytecodeartist.net/2011/04/im-joining-readify-in-sydney-australia.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/__BgjkW_AfhY/TbI9XoTrsCI/AAAAAAAAANM/dT6_TcXPCqw/s72-c/readify_thumb.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-2104076519833600661</guid><pubDate>Fri, 22 Apr 2011 08:21:00 +0000</pubDate><atom:updated>2011-04-22T18:21:08.552+10:00</atom:updated><title>Speaking about “Ten Simple Rules for Rewriting IL on the Common Language Runtime” at NDC 2011</title><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/__BgjkW_AfhY/TbE6cFKiibI/AAAAAAAAANA/ZIGmVCl6Nuc/s1600-h/Red-Pill-vs.-Blue-Pill%5B5%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Red-Pill-vs.-Blue-Pill" border="0" alt="Red-Pill-vs.-Blue-Pill" src="http://lh6.ggpht.com/__BgjkW_AfhY/TbE6c2A1H-I/AAAAAAAAANE/Rl6CrJBgqAE/Red-Pill-vs.-Blue-Pill_thumb%5B2%5D.jpg?imgmax=800" width="440" height="189" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;Red or Blue&lt;/h4&gt;  &lt;p&gt;Have you ever wondered what happens under the hood of an AOP framework? Have you ever tried writing IL, only to get hit by an unintelligible wall of &lt;em&gt;PEVerify.exe&lt;/em&gt; errors?&lt;/p&gt;  &lt;p&gt;Learning how to (re)write IL can be hard, but in this session, I will show you some techniques for rewriting almost any .NET assembly with the least amount of effort and the most minimum amount of frustration possible. The art of rewriting IL will give you the ability to change programs as if you (the developer) are the compiler itself. With IL rewriting, you can add, remove, replace, unseal, and redirect method calls from any type. You can change almost any method you can possibly imagine, including methods in third party libraries that don’t have any source code publicly available. The possibilities are endless, and there’s a whole other hidden world that goes on when you compile your code into IL using your favorite .NET language, and this talk will show you just how different things are once take the red pill. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://ndc2011.no/agenda.aspx?cat=1071&amp;amp;id=-1&amp;amp;day=3728"&gt;Join me&lt;/a&gt; at NDC 2011 in Oslo at 11:40AM-12:40PM on Track 5, and I’ll show you just how deep that rabbit hole really goes.&lt;/p&gt;  </description><link>http://www.bytecodeartist.net/2011/04/speaking-about-ten-simple-rules-for.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/__BgjkW_AfhY/TbE6c2A1H-I/AAAAAAAAANE/Rl6CrJBgqAE/s72-c/Red-Pill-vs.-Blue-Pill_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-6058630161417478637</guid><pubDate>Mon, 15 Nov 2010 13:54:00 +0000</pubDate><atom:updated>2010-11-16T00:54:22.144+11:00</atom:updated><title>Øredev 2010, Part 1</title><description>&lt;a title="The Oredev poster by philip.laureano, on Flickr" href="http://www.flickr.com/photos/philiplaureano/5166055190/"&gt;&lt;img alt="The Oredev poster" src="http://farm2.static.flickr.com/1312/5166055190_60eb65c352.jpg" width="436" height="345" /&gt;&lt;/a&gt;   &lt;h4&gt;Background&lt;/h4&gt;  &lt;p&gt;Aside from becoming a great programmer, one of my lifelong dreams has always been to visit Sweden and be able to see everything that its cities have to offer. That dream finally came true last week when my employers gave me the whole week off so that I can attend the Øredev conference in Malmo, Sweden.&lt;/p&gt;  &lt;p&gt;There were so many great things that went on during that conference, and if I had to summarize it in a single word, I would call it “amazing”, and if you’re looking for a more detailed summary, here we go:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;Day Zero - Old world architecture, fine dining, and bloody gooses&lt;/h4&gt; &lt;a title="Oredev 2010 Speakers Dinner - Malmo City Hall by philip.laureano, on Flickr" href="http://www.flickr.com/photos/philiplaureano/5162926883/"&gt;&lt;img alt="Oredev 2010 Speakers Dinner - Malmo City Hall" src="http://farm2.static.flickr.com/1206/5162926883_824e2e706d.jpg" width="428" height="86" /&gt;&lt;/a&gt;   &lt;p&gt;After a quick round of introductions at &lt;a href="http://twitter.com/noopman"&gt;Magnus Mårtensson's place&lt;/a&gt;, everyone walked over to the Malmo City Hall for the Speaker’s dinner. This place was huge, and according to some historians, it was the place where the Swedes celebrated victory over the Danish invaders that were trying to reconquer the southern region of Skane (the area now known as Malmo) back in the 1700s. It was the first time that I’ve ever seen such a place, and suffice to say, I was impressed.&lt;/p&gt;  &lt;p&gt;The building interior was ornate with paintings of its former Danish princes and rulers, and the far end of the Council Room had images of its Swedish victors. The deputy mayor of the City of Malmo stood in front of all the attendees and welcomed us to the city. I watched as everyone lifted their champagne glasses to toast the event, and at first, I thought that we were just going to stand around in the Council Room and have a cocktail party. Ten minutes later, they escorted us into a huge dining hall, and the surroundings were like something out of an old world fairy tale.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://lh5.ggpht.com/__BgjkW_AfhY/TOE7dy0oF7I/AAAAAAAAALk/tnlvDcxtnl8/s1600-h/IMG_0141%5B7%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="IMG_0141" border="0" alt="IMG_0141" src="http://lh4.ggpht.com/__BgjkW_AfhY/TOE7enrkZ_I/AAAAAAAAALo/HBR3ZfebNZo/IMG_0141_thumb%5B4%5D.jpg?imgmax=800" width="446" height="292" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;After everyone found their seats among the various open tables, the waiters started serving us various types of white and red wines. Everything seemed normal up to that point, until the waiters served this one mysterious bowl of soup:&lt;/p&gt; &lt;a title="Goose Blood Soup by philip.laureano, on Flickr" href="http://www.flickr.com/photos/philiplaureano/5163598946/"&gt;&lt;img alt="Goose Blood Soup" src="http://farm5.static.flickr.com/4087/5163598946_198088acde.jpg" width="437" height="345" /&gt;&lt;/a&gt;  &lt;p&gt;At first, I thought it was some bowl of chocolate-flavored syrup. After all, as I naively told myself, the Swedes must be known for their chocolate. As it turns out, it was the Swiss (not the Swedes) that were known for their chocolate, and that “special” soup was actually a bowl that was chalk full of goose blood. When I asked the waiter what was in the bowl, he just gave me a weird look and said, “they didn’t tell you what was in the bowl?”&lt;/p&gt;  &lt;p&gt;I just shook my head and waved the bowl away, and if weren’t for my vegetarian tendencies, I probably would have become an avian vampire. After picking and eating the meatless option (which was the lobster soup), I went around the room and took a few pictures of some of the notable speakers that were at the conference:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/__BgjkW_AfhY/TOE7gFxfnhI/AAAAAAAAALs/-B9uFcdrnIU/s1600-h/IMG_0144%5B4%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="IMG_0144" border="0" alt="IMG_0144" src="http://lh5.ggpht.com/__BgjkW_AfhY/TOE7gyvwZ6I/AAAAAAAAALw/Dzm-JCLTkmI/IMG_0144_thumb%5B1%5D.jpg?imgmax=800" width="441" height="306" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;h6&gt;Center: Jeremy Miller, aka “The Shade Tree Developer”&lt;/h6&gt;  &lt;p&gt;What surprised me was that many of the developers that I had been reading about for many years were sitting right there in front of me in that dining hall. They were no longer just some prestigious names on a random blog post on a programmer site. They were there, &lt;em&gt;en vivo&lt;/em&gt;, and it was nice to finally be able to match a face to each one of their names.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/__BgjkW_AfhY/TOE7hR5_pCI/AAAAAAAAAL0/eizqLqzAq_A/s1600-h/IMG_0162%5B3%5D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="IMG_0162" border="0" alt="IMG_0162" src="http://lh6.ggpht.com/__BgjkW_AfhY/TOE7inxm4YI/AAAAAAAAAL4/tWCTQsiqxVo/IMG_0162_thumb.jpg?imgmax=800" width="244" height="164" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h6&gt;Center: Roy Osherove, author of “The Art of Unit Testing in .NET”, and Hadi Hariri, Tech Evangelist at Jetbrains&lt;/h6&gt;  &lt;p&gt;For the most part, the rest of the dinner was filled with toasts and speeches from the Øredev organizers. Overall, I’d say that the food was excellent, and as introductory dinners go, it was filled with lots of people getting to know each other over bleeding gooses. For me, it was remarkable for its attendees, but relatively unremarkable for the dinner chatter. &lt;/p&gt;  &lt;p&gt;Things didn’t get interesting until we all left Malmo City Hall and went to the Green Lion Inn. After spending about an hour talking to Hadi (Hariri) about emergent refactoring techniques, he said that he had to get some sleep because he had to be at the Jetbrains booth very early by the next morning, so I said goodbye to him, and he left.&lt;/p&gt;  &lt;p&gt;I was tired too, and I figured that I should go back to my room and get some rest. My internal body clock was seven hours ahead of my physical location, and I was headed for the door until some stranger approached me and told me that he wanted to talk to me about AOP. The two-hour conversation about AOP that soon followed seriously changed the way I saw AOP, and that stranger was no ordinary stranger.&lt;/p&gt;  &lt;p&gt;It was Greg Young, and he blew me away with what he had to say.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;…to be continued&lt;/em&gt;&lt;/p&gt;  </description><link>http://www.bytecodeartist.net/2010/11/redev-2010-part-1.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://farm2.static.flickr.com/1312/5166055190_60eb65c352_t.jpg" height="72" width="72" /><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-5301977795878761357</guid><pubDate>Sun, 07 Mar 2010 11:14:00 +0000</pubDate><atom:updated>2010-03-07T22:14:57.136+11:00</atom:updated><title>Announcement: LinFu is now on Github!</title><description>LinFu has been my obsession for the better part of six years, and despite all its features, almost every patch, fix, and feature request has been handled by one and only one person--&lt;a href="http://plaureano.blogspot.com"&gt;me&lt;/a&gt;. After three years, the codebase itself has become quite mature, and I realize that there will be some point where I have to completely open this project up to the rest of the community so that it can grow and this is just the first step.&lt;br /&gt;&lt;br /&gt;Today, I'm happy to announce that all of LinFu version 2.0 &lt;a href="http://github.com/philiplaureano/LinFu"&gt;has been moved to Github&lt;/a&gt; (under the same LGPL license), and you can fork it as much as you want. I will still actively maintain it (just as I maintained it on google code), but the difference is that anyone here can make changes to the project and I will be more than happy to accept pull requests. &lt;br /&gt;&lt;br /&gt;No man is an island, and now it's time to swim in the sea of possibilities.&lt;br /&gt;&lt;br /&gt;Happy forking, and let me know if you come up with anything interesting :)</description><link>http://www.bytecodeartist.net/2010/03/announcement-linfu-is-now-on-github.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-2206168811399654381</guid><pubDate>Fri, 25 Dec 2009 02:02:00 +0000</pubDate><atom:updated>2009-12-25T15:52:44.043+11:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">hiro ioc codeproject dynamic</category><title>Introducing Hiro.Functors, and Making Hiro a Dynamic Container</title><description>One of the more salient criticisms that I've often heard about Hiro is the fact that for the most part, the containers that Hiro creates are static and cannot be modified once they have already been compiled and instantiated. There might even be cases where you might have to have a Hiro-compiled container return an existing service instance, and that service instance (in turn) might not be available when the container is first compiled. For example, let's assume that you have an existing service instance of class type Foo that implements the IFoo interface:&lt;br /&gt;&lt;br /&gt;public interface IFoo&lt;br /&gt;{&lt;br /&gt;    // ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Foo : IFoo&lt;br /&gt;{&lt;br /&gt;    // ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Let's also assume that Foo is some sort of long running service that must be returned every time you ask for an IFoo instance from a Hiro-compiled container. In previous revisions of Hiro, you probably could have registered the IFoo service as a singleton using the following code:&lt;br /&gt;&lt;br /&gt;var map = new DependencyMap();&lt;br /&gt;map.AddSingletonService&lt;IFoo, Foo&gt;();&lt;br /&gt;&lt;br /&gt;var container = map.CreateContainer();&lt;br /&gt;&lt;br /&gt;// ..Use the container code here&lt;br /&gt;&lt;br /&gt;The problem with the previous code above is that you can't use it if the Foo instance has already created before the container instance has been compiled. Since the hypothetical Foo service is a long-running service that already exists before each container is compiled, there doesn't seem to be any way to make a DependencyMap return an existing object instance--that is, until now. This is where Hiro.Functors comes to the rescue:&lt;br /&gt;&lt;br /&gt;// For the sake of simplicity, let's assume that Foo has already been instantiated..&lt;br /&gt;var existingFooInstance = new Foo();&lt;br /&gt;&lt;br /&gt;// Define the factory functor&lt;br /&gt;Func&amp;lt;IMicroContainer, object&amp;gt; makeFoo =&amp;gt;existingFooInstance;&lt;br /&gt;&lt;br /&gt;var map = new DependencyMap();&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;map.AddService(typeof(IFoo), makeFoo);&lt;/span&gt;&lt;br /&gt;var container = map.CreateContainer();&lt;br /&gt;&lt;br /&gt;// Get the foo service; The makeFoo functor will be called here&lt;br /&gt;var actualFoo = container.GetInstance&lt;IFoo&gt;();&lt;br /&gt;&lt;br /&gt;// This will evaluate to true&lt;br /&gt;Assert.AreSame(existingFooInstance, actualFoo);&lt;br /&gt;&lt;br /&gt;As you can see from the code above, the Hiro.Functors library extends Hiro so that you can use C#'s native lambda syntax to add your own custom factory methods to any Hiro-compiled container. A single call to the additional DependencyMap.AddService(yourServiceType, yourFunctor) is all you need to convert Hiro from a static container to a dynamic container without sacrificing speed for flexibility. It's just that simple.&lt;br /&gt;&lt;br /&gt;I prefer to call Hiro the "blue collar" IOC container framework, and Hiro.Functors is just another step in helping you (the end-user developer) get the job done with as little drama as possible in the fewest amount of steps possible. While there's definitely nothing revolutionary about this tiny release, I think there's plenty to be said about having an IOC container that gets in and does the job using the flattest learning curve possible, and that's exactly what Hiro offers. &lt;br /&gt;&lt;br /&gt;While I can't promise my readers that I'll change the world with revolutionary tools, what I will promise them is that my tools will be the simplest tools that they can possibly use so that they'll be able to meet their deadlines on time and make it home early for holidays like today. Hiro is one such tool, and if you ever run into any problems with it, I'm only a patch and an email away. Happy holidays, everyone!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(Note: You can get Hiro.Functors &lt;a href="http://code.google.com/p/hiro-contrib/source/checkout"&gt;here&lt;/a&gt;)</description><link>http://www.bytecodeartist.net/2009/12/introducing-hirofunctors-and-making.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-5309986570257573474</guid><pubDate>Wed, 09 Sep 2009 23:01:00 +0000</pubDate><atom:updated>2009-09-10T09:33:58.084+10:00</atom:updated><title>Creating Per-Session Services with Hiro</title><description>Over the past week, I've had a few people ask me how they can use Hiro to create services that are instantiated once per web session. Ideally, Hiro should be able to do something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="csharp"&gt;&lt;br /&gt;&lt;br /&gt;// Instantiate the IFoo service&lt;br /&gt;var context = HttpContext.Current;&lt;br /&gt;&lt;br /&gt;if (context.Session["SomeFooKey"] == null)&lt;br /&gt;  context.Session["SomeFookey"] = container.GetInstance&amp;lt;IFoo&amp;gt;();&lt;br /&gt;&lt;br /&gt;return (IFoo)context.Session["SomeFooKey"];&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The code itself is pretty simple, but the problem is that this can get quite cumbersome if you have more than a few services that need to be stored in the Session object. There has to be some way to do this with Hiro using a single method call, and indeed, Hiro makes creating per-session services just as equally effortless:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="csharp"&gt;&lt;br /&gt;// Register the Foo service into the DependencyMap&lt;br /&gt;var map = new DependencyMap();&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;map.AddPerSessionService(typeof(IFoo), typeof(Foo));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;// Compile the container&lt;br /&gt;var container = map.CreateContainer();&lt;br /&gt;&lt;br /&gt;var foo = container.GetInstance&amp;lt;IFoo&amp;gt;();&lt;br /&gt;&lt;br /&gt;// ...use the IFoo per-session service here (just like any other service)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see from the example above, Hiro abstracted away the details of having to deal with caching your services into a Session object. In fact, all you have to do to add per-session services to your dependency maps is to use the &lt;span style="font-style: italic;"&gt;AddPerSessionService&lt;/span&gt; extension method, compile your container and use it like any other Hiro-compiled container. It's just that simple.&lt;br /&gt;&lt;br /&gt;NOTE: You can download the Hiro.Web extensions (including the per-session service support extension method) &lt;a href="http://code.google.com/p/hiro-contrib/source/checkout"&gt;here&lt;/a&gt;.</description><link>http://www.bytecodeartist.net/2009/09/creating-per-session-services-with-hiro.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-3214101499232928563</guid><pubDate>Mon, 27 Jul 2009 22:57:00 +0000</pubDate><atom:updated>2009-07-29T14:07:06.758+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">hiro ioc codeproject dotnet</category><title>Introducing Hiro, the World's Fastest IOC Container, Part III: The Container Itself</title><description>&lt;span style="font-weight: bold;"&gt;Introduction&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In this final installment of the Hiro series, I'll show you just how ridiculously simple it is to use Hiro in your own applications without sacrificing speed for simplicity. I'll also show you how you can use Hiro's registration conventions so that you will never have to worry about managing any external configuration files or declare any external attributes on your types just so you can get your types registered into a service container.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Simply Fast, and Simply Simple&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As I mentioned in &lt;a href="http://plaureano.blogspot.com/2009/04/introducing-hiro-worlds-fastest-ioc_15.html"&gt;my previous posts&lt;/a&gt;, Hiro lacks many of the "fat" features that you would normally associate with other IOC containers (including LinFu). At first, that might seem like a disadvantage for potential Hiro users, but it also implies that you won't have to waste any time trying to understand any IOC container features that you probably will never use in most of your applications. Instead, Hiro focuses on doing three things:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Determine the list of available services from any given assembly.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Registering those services into a container&lt;/li&gt;&lt;li&gt;Ensuring that the container can create the services that you registered.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Unlike other IOC containers, Hiro doesn't need you to use fluent interfaces or attributes to register your types into a container. You don't even need lambda functions or an external XML configuration file to get your services up and running with Hiro. Instead, Hiro uses &lt;a href="http://en.wikipedia.org/wiki/Convention_over_configuration"&gt;Convention over Configuration&lt;/a&gt; semantics that let you register your services with only a few lines of code. Here's an example:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;// Use the loader to scan the target assembly&lt;br /&gt;// for services&lt;br /&gt;var loader = new DependencyMapLoader();&lt;br /&gt;&lt;br /&gt;// The loader will handle all the details of loading&lt;br /&gt;// the services into the container for you&lt;br /&gt;DependencyMap map = loader.LoadFromBaseDirectory("SampleDomain.dll");&lt;br /&gt;var container = map.CreateContainer();&lt;br /&gt;&lt;br /&gt;// (Do something useful with the container here)&lt;br /&gt;var greeter = container.GetInstance&amp;lt;IGreeter&amp;gt;();&lt;br /&gt;&lt;br /&gt;// ...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;"Convention over...what?"&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;At first glance, it's hard to believe that Hiro can load all the services from a given assembly (or a set of assemblies) using only the code that was given in the previous example. After all, any given service can have nearly an infinite amount of constructor and property injection dependencies, and given the sheer complexity of this task, there must be a set of rules that Hiro follows that somehow makes the registration happen "automagically". At the same time, there might be some end-user developers out there that might want to have strict control over the actual services that will be registered into the compiled container. In other words, how does Hiro know exactly what to register and still be able let users control what gets registered into a compiled container?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;A Few Rules Go A Long Way&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now the reason why Hiro can do so much in so few lines of code is because that it implements a few conventions that typical IOC container users would follow when registering their own types. As long as users follow those conventions, they will never have to worry about manually registering any of their service types with Hiro. For example, let's assume that you had this particular service type declared in a single assembly:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;  &lt;br /&gt;  public interface IGreeter&lt;br /&gt;  {&lt;br /&gt;      void Greet(string name);&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Let's also assume that you have three concrete classes that implement the same IGreeter interface:&lt;br /&gt;&lt;pre lang="cs"&gt;  &lt;br /&gt;  public class GermanGreeter : IGreeter&lt;br /&gt;  {&lt;br /&gt;      public void Greet(string name)&lt;br /&gt;      {&lt;br /&gt;          Console.WriteLine("German: Hallo, {0}!!");&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public class FrenchGreeter : IGreeter&lt;br /&gt;  {&lt;br /&gt;      public void Greet(string name)&lt;br /&gt;      {&lt;br /&gt;          Console.WriteLine("French: Bonjour, {0}!", name);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public class Greeter : IGreeter&lt;br /&gt;  {&lt;br /&gt;      public void Greet(string name)&lt;br /&gt;      {&lt;br /&gt;          Console.WriteLine("English: Hello, {0}!", name);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;"Just give me the default service, please!"&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Based on the examples above, it probably didn't take you very long to infer that the Greeter class is the default implementation for the IGreeter interface. Furthermore, it's probably safe to assume that a default implementation exists if you have an interface named IYourService and if you already have a concrete class named YourService that implements that particular service type. Hiro follows the same convention, so calling container.GetInstance on an IGreeter type will yield a Greeter instance:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;  &lt;br /&gt;&lt;br /&gt;// Load the sample assembly&lt;br /&gt;var loader = new DependencyMapLoader();&lt;br /&gt;DependencyMap map = loader.LoadFromBaseDirectory("SampleDomain.dll");&lt;br /&gt;var container = map.CreateContainer();&lt;br /&gt;&lt;br /&gt;// Get the default implementation&lt;br /&gt;var greeter = container.GetInstance&amp;lt;IGreeter&amp;gt;();&lt;br /&gt;&lt;br /&gt;// Determine the actual type that implements the IGreeter interface&lt;br /&gt;var greeterType = greeter.GetType();&lt;br /&gt;&lt;br /&gt;// This expression will return true&lt;br /&gt;Console.WriteLine(greeterType == typeof(Greeter));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see, the example above is fairly self-explanatory. The DependencyMapLoader class will automatically determine that the Greeter type is the default implementation for the IGreeter interface and register it into the DependencyMap. Of course, this approach assumes that you have a class with a type name that matches the service type name; however, there might be cases where you have multiple classes that implement the same interface but don't follow this naming convention. For example, if I have the FrenchGreeter and a GermanGreeter class but don't have a default Greeter class, how does Hiro determine which implementation should be used as the default implementation type?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;When All Else Fails, Use the Alphabet&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The answer is that by default, Hiro will pick the first implementation type from a list of service implementations that have been sorted alphabetically by type name. In this case, Hiro will choose the FrenchGreeter as the default IGreeter implementation since the FrenchGreeter class name alphabetically appears before the GermanGreeter class name:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;// ...&lt;br /&gt;&lt;br /&gt;// Get the default implementation&lt;br /&gt;var greeter = container.GetInstance&amp;lt;IGreeter&amp;gt;();&lt;br /&gt;&lt;br /&gt;// Determine the actual type that implements the IGreeter interface&lt;br /&gt;var greeterType = greeter.GetType();&lt;br /&gt;&lt;br /&gt;// This expression will return true since&lt;br /&gt;// the FrenchGreeter type appears before the GermanGreeter type&lt;br /&gt;Console.WriteLine(greeterType == typeof(FrenchGreeter));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;While this approach might be useful for determining (and thus accessing) a default service implementation, there has to be a way to access the other concrete IGreeter implementations that reside in the compiled container. As it turns out, Hiro makes this process just as equally effortless:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;// Get the French and German greeters&lt;br /&gt;var frenchGreeter = container.GetInstance&amp;lt;IGreeter&amp;gt;("FrenchGreeter");&lt;br /&gt;var germanGreeter = container.GetInstance&amp;lt;IGreeter&amp;gt;("GermanGreeter");&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;By default, Hiro registers each service implementation using the class name of each concrete service type. In order to access a particular service implementation, all you need to do is pass the name of the implementing type in the container.GetInstance&amp;lt;T&amp;gt; call, and the container will instantiate the corresponding type for you.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Using Constructor Injection and Property Injection&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now that we have an idea of how Hiro registers service types into a container, the next thing that you might be wondering about is exactly how Hiro injects dependencies into constructors and properties. For example, if I have a class named GreeterHost that takes one IGreeter dependency in its constructor, how do I tell Hiro to use that constructor instead of the default constructor? Let's take a look at the GreeterHost class:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;public class GreeterHost&lt;br /&gt;{&lt;br /&gt;  public GreeterHost ()&lt;br /&gt;  {&lt;br /&gt;  }&lt;br /&gt;  public GreeterHost(IGreeter greeter)&lt;br /&gt;  {&lt;br /&gt;     // Do something useful here&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // ...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Let's also assume that I manually registered the GreeterHost class with the DependencyMap using the DependencyMap.AddService method. Here's how you get Hiro to perform constructor injection:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;&lt;br /&gt;// Manually add the GreeterHost to the dependency map&lt;br /&gt;var map = new DependencyMap();&lt;br /&gt;map.AddService&amp;lt;GreeterHost, GreeterHost&amp;gt;();&lt;br /&gt;&lt;br /&gt;// Manually add an IGreeter service so that Hiro uses the constructor&lt;br /&gt;// with the IGreeter parameter&lt;br /&gt;map.AddService&amp;lt;IGreeter, Greeter&amp;gt;();&lt;br /&gt;&lt;br /&gt;// Compile the container&lt;br /&gt;var container = map.CreateContainer();&lt;br /&gt;&lt;br /&gt;// Create the GreeterHost. Hiro will automatically&lt;br /&gt;// inject the IGreeter implementation into the constructor&lt;br /&gt;var greeterHost = container.GetInstance&amp;lt;GreeterHost&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see from the example above, Hiro automatically used the contents of the dependency map to determine which constructor should be used for instantiating the GreeterHost type. The only thing that I needed to do to perform constructor injection was to make sure that the IGreeter service type was available in the dependency map once the container was compiled. The compiled container, in turn, used the IGreeter dependency to call the correct GreeterHost constructor.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Singletons Galore&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;NOTE: If you prefer to have your Greeter type registered as a singleton in the dependency map, however, here's how you do it:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;// Manually add an IGreeter service so that Hiro uses the constructor&lt;br /&gt;// with the IGreeter parameter&lt;br /&gt;map.AddSingletonService&amp;lt;IGreeter, Greeter&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Constructor, Inject Thyself&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The best part about the constructor injection example with Hiro is the fact that it handles all the gory details of performing constructor injection for you in the most minimal lines of code possible. In this case, the only convention that you have to follow is to make sure that the dependency map contains all the necessary services for Hiro to call the appropriate constructor. What makes this even more interesting is that Hiro can perform automatic constructor injection with constructors that have any arbitrary number of constructor arguments. In other words, if the DependencyMap instance has the constructor dependency, then Hiro can handle the injection.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Property, Inject Thyself Too&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For the most part, Hiro's property injection follows some of the same conventions as constructor injection. Much like constructor injection, the only thing you need to do to use property injection with Hiro is to make sure that services required by the target property are already registered with the dependency map once the container is compiled. For example, let's assume that GreeterHost class as an IGreeterProperty named CurrentGreeter:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;public class GreeterHost&lt;br /&gt;{&lt;br /&gt;  public IGreeter CurrentGreeter&lt;br /&gt;  {&lt;br /&gt;     get;set;     &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here's how you set up Hiro's dependency map to perform property injection:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;// Manually add the GreeterHost to the dependency map&lt;br /&gt;var map = new DependencyMap();&lt;br /&gt;map.AddService&amp;lt;GreeterHost, GreeterHost&amp;gt;();&lt;br /&gt;&lt;br /&gt;// Manually add an IGreeter service so that Hiro uses the constructor&lt;br /&gt;// with the IGreeter parameter&lt;br /&gt;map.AddService&amp;lt;IGreeter, Greeter&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The first thing that you might notice is that the setup code for property injection and constructor injection are both identical to each other. In fact, you could even say that I just "copy &amp;amp; pasted" the example from previous examples in this article. Indeed, that was intentional, and the point here is that are only two simple things that you need to do to make Hiro automatically perform property injection on your types:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Ensure that the service types that your properties require are registered with the dependency map.&lt;/li&gt;&lt;li&gt;Ensure that the properties that will be injected are not read-only properties.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;In other words, Hiro's property injection conventions follow one of the most basic dependency injection conventions of all:&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="font-style: italic;"&gt;If you don't want Hiro to inject dependencies into your target property, then make sure the property is a read-only property or the property type doesn't exist in the container as a service type.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;All you need to do to disable property injection for the GreeterHost is to prevent a default IGreeter service from ever being registered into the dependency map. You can do this by either constructing the dependency map by hand, or you can attach a service filter to the DependencyMapLoader so that it avoids loading a default IGreeter service into the resulting dependency map:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;var loader = new DependencyMapLoader();&lt;br /&gt;&lt;br /&gt;// Make sure that no default service is loaded for the IGreeter interface&lt;br /&gt;loader.ServiceFilter = service=&gt;service.ServiceType != typeof(IGreeter) &amp;&amp; string.IsNullOrEmpty(service.ServiceName);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see from the example above, all you need to do to prevent the DependencyMapLoader from registering services is use a simple lambda function, and it can't get any simpler than that.&lt;br /&gt;&lt;br /&gt;Next, here's the call that will construct the GreeterHost and perform the property injection operation if you still have the required dependency inside the container: &lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;// This is the only method call you need&lt;br /&gt;// to do property injection&lt;br /&gt;var greeterHost = container.GetInstance&amp;lt;GreeterHost&amp;gt;();&lt;br /&gt;&lt;br /&gt;// ...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Fortunately for the end-user developer, the GetInstance method that performs the constructor injection is also the same method that will perform all property injection operations. Hiro uses these simple conventions to make developers' lives easier, and if it can help at least one person out there write better code, then I consider this project to be a success.&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;&lt;br /&gt;You can download Hiro from &lt;a href="http://is.gd/1S4j4"&gt;here&lt;/a&gt;.&lt;br /&gt;You can also &lt;a href="http://is.gd/1S4OE"&gt;click here&lt;/a&gt; to download the Hiro example solution.</description><link>http://www.bytecodeartist.net/2009/07/introducing-hiro-worlds-fastest-ioc.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-8141072638969660469</guid><pubDate>Fri, 17 Jul 2009 01:47:00 +0000</pubDate><atom:updated>2009-07-17T13:33:12.149+10:00</atom:updated><title>NDepend Review</title><description>&lt;span style="font-weight: bold;"&gt;The Quick &amp;amp; Dirty&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There have been a &lt;a href="http://www.google.com/search?q=ndepend+review&amp;amp;hl=en&amp;amp;start=20&amp;amp;sa=N"&gt;lot of good reviews&lt;/a&gt; written for NDepend, so for the sake of efficiency, I'll get straight to the point: &lt;span style="font-weight: bold;"&gt;NDepend is an amazing tool&lt;/span&gt;. It lets you graphically inspect your assemblies and it lets you pinpoint the spots in your code base that need refactoring.&lt;br /&gt;&lt;br /&gt;So would I recommend buying NDepend Professional? &lt;span style="font-weight: bold;"&gt;Yes&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Is there something that I didn't like about NDepend that I think the users should know about? &lt;span style="font-weight: bold;"&gt;Yes&lt;/span&gt;, and that's exactly what I'll talk about.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;"So what's not to like about NDepend?"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;My only problem with NDepend is that its CQL query engine can only be accessed from the Visual NDepend application. NDepend does let you embed CQL attributes in your applications, but the problem is that I shouldn't have to force each one of my applications to have a dependency on NDepend just because I want to have some code metrics calculations in my build process. Of course, there are a few ways that I can work around that limitation, but the point is that I shouldn't have to make a workaround for something that should have been there in the first place.&lt;br /&gt;&lt;br /&gt;In other words, what NDepend sorely needs right now is a publicly-accessible CQL engine API. If NDepend is truly a tool built by developers for developers, then it should have an API that other developers can use to analyze their applications. It's not enough to throw them a rudimentary command-line tool that spits out code metrics in XML--that pretty much leaves developers to write code against the "shadow" of the CQL API rather than write against the CQL API itself, and for me, that just doesn't quite cut it.&lt;br /&gt;&lt;br /&gt;Don't get me wrong--the Visual NDepend winforms application is an awesome app--but I'd rather be able to filter through all that information using a single API call rather than have to manually sift through several windows just to find the information I need.&lt;br /&gt;&lt;br /&gt;Now the reason why I'm harping so much about the lack of a public CQL API is that &lt;a href="http://www.codelean.com/"&gt;my company&lt;/a&gt; uses NDepend Professional in its command line builds to ensure that our builds fail when the Cyclomatic Complexity score exceeds 2.5 in any of our projects. The problem is that we want our code quality inspections to be completely automated, and we can't do that unless we have access to the CQL API and can generate CQL queries at runtime. The maximum Cyclomatic Complexity score might prevent our developers from writing complicated code, but there's far more to code inspections than just looking at a single score. What we need is a way to look deeper into the quality of our code base using the hypothetical CQL API with the same ease that Visual NDepend users can browse through their code.&lt;br /&gt;&lt;br /&gt;So in the end, I still recommend NDepend Pro as definite buy, but it still lacks a few key features that would make me give Patrick Smacchia's tool the gushing review that other bloggers have given it in their respective reviews. Overall, it's definitely a great tool, but it's not without a few shortcomings.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;[Public Disclosure: Thanks to Patrick Smacchia for providing me with a free NDepend Pro license for this review.]&lt;/span&gt;</description><link>http://www.bytecodeartist.net/2009/07/ndepend-review.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-446687795186661198</guid><pubDate>Wed, 15 Apr 2009 23:06:00 +0000</pubDate><atom:updated>2009-04-16T10:33:33.464+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">hiro ioc codeproject</category><title>Introducing Hiro, the World's Fastest IOC Container, Part II: The Little Feature Set That Could</title><description>&lt;span style="font-weight: bold;"&gt;The Art of Lean&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now that everyone knows just how fast Hiro can be, the next question you might be asking yourself is, "What features will it support?"&lt;br /&gt;&lt;br /&gt;The simplest answer that I can give is that Hiro is that it will implement as little features as possible--in fact, Hiro will implement &lt;span style="font-style: italic;"&gt;so little features&lt;/span&gt; that it might turn away your traditional "dynamic" IOC user. Here's the features that Hiro will implement:&lt;br /&gt;&lt;br /&gt;-&lt;span style="font-weight: bold;"&gt;Named/Anonymous Static Component Registration.&lt;/span&gt; This means that you'll be able to register your services using a service name, service type, and the implementing type.&lt;br /&gt;&lt;br /&gt;-&lt;span style="font-weight: bold;"&gt;Convention Over Configuration for Registration and Injection.&lt;/span&gt;. In addition to its support for programmatic registration, Hiro will be able to scan your assemblies and automatically infer:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;The list of available services&lt;/li&gt;&lt;li&gt;The list of concrete types that implement those services&lt;/li&gt;&lt;li&gt;The list of properties that can be injected&lt;/li&gt;&lt;li&gt;The constructors that will be used for constructor injection&lt;/li&gt;&lt;li&gt;The services that will be injected into each parameter during a constructor injection call.&lt;/li&gt;&lt;/ol&gt;The most interesting part about this is the amount of work that will take to do this sort of registration. Take a look at this example:&lt;br /&gt;&lt;br /&gt;&lt;pre lang="cs"&gt;&lt;br /&gt;          var loader = new DependencyMapLoader();&lt;br /&gt;          var map = loader.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "Yourlibrary.dll");&lt;br /&gt;&lt;br /&gt;          IContainerCompiler compiler = new ContainerCompiler();&lt;br /&gt;          var microContainer = compiler.Compile(map);&lt;br /&gt;&lt;br /&gt;          // Do something with the container here&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Believe it or not, this is the only code that you need to use to configure the dependencies for any assembly of any given size. What makes this even more interesting is the fact that &lt;span style="font-weight: bold;"&gt;all of this is done statically at (container) compile time, not runtime&lt;/span&gt;. This means that unlike the current generation of IOC containers, Hiro will not waste any time trying to rediscover your configuration, and based on the benchmark results from my &lt;a href="http://plaureano.blogspot.com/2009/04/introducing-hiro-worlds-fastest-ioc.html"&gt;previous post&lt;/a&gt;, the performance numbers are nothing short of being dramatically one-sided.&lt;br /&gt;&lt;br /&gt;-&lt;span style="font-weight: bold;"&gt;Transient/Singleton instances.&lt;/span&gt; Aside from performance, however, Hiro has to be able to create both Transient (that is, plain old object instances created with the New operator) and Singleton instances.&lt;br /&gt;&lt;br /&gt;-&lt;span style="font-weight: bold;"&gt;Property/Constructor Injection.&lt;/span&gt; Hiro will implement both property and constructor injection by default. What makes this even more interesting is that like everything else with Hiro, all property and constructor injection calls will all be precompiled into IL, meaning that there will be no performance issues when using Hiro.&lt;br /&gt;&lt;br /&gt;-&lt;span style="font-weight: bold;"&gt;Stateless&lt;/span&gt;. Each Hiro-compiled container instance will not have any private local data (or even shared data) that will distinguish it from another compiled container of the same type. This means that you can scale Hiro's compiled containers across multiple cores AND multiple threads without using a single semaphore, mutex, or lock statement (in C#).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;"But wait, your container isn't a REAL container until you implement feature X!"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Some would argue that Hiro would have to implement a minimum "baseline" feature set in order to be considered a "commercial" quality IOC container. However, my counterargument there is that it's this same "fat feature" mindset that got these IOC containers (including LinFu) into this speed problem in the first place. Secondly, if you're experienced enough about IOC to understand the significance of what Hiro does, then it's safe to assume that you're a user that falls into at least one of the following categories:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;You've probably written at least one IOC container framework, or&lt;/li&gt;&lt;li&gt;You already are comfortable with an existing IOC container framework (such as Castle, Ninject, AutoFac, Unity, StructureMap, LinFu, etc) and you know enough to customize it to your needs.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;Assuming that you're an IOC container author, it would be pointless for me to implement something in Hiro that you've probably rolled into your own framework, and given that you're skilled enough to write your own framework, it would be practically trivial for you to plug Hiro into your own framework and reap the performance benefits, and there's clearly no reason to reinvent the wheel here if you somehow did a better job than I did in implementing "feature X".&lt;br /&gt;&lt;br /&gt;Now, if you think you're a user that falls into the second category, there's a good chance that you've pretty much decided to stick to the favorite framework of your choice, and like the other IOC container authors, there's really nothing that I can do for you unless you decide to plug in Hiro into your favorite container.&lt;br /&gt;&lt;br /&gt;So between these two types of users, who do you think Hiro is written for?&lt;br /&gt;&lt;br /&gt;Here's my answer: &lt;span style="font-weight: bold;"&gt;Neither one of them&lt;/span&gt;. Hiro is written for the average developer who wants to get started with an IOC container and doesn't have time to "geek out" over the latest and greatest features of an IOC container framework. Given that there are probably far better IOC container developers than myself, I've pretty much decided to skip the "my container is better than your container" religious wars and focus on what really matters: the end users.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;The Pareto Pleasure Principle&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Hiro doesn't need to implement 80% of the expected features of an IOC container in order to be useful--instead, it only has to implement the other 20% of the overall features that (in my opinion) people will actually use. In the end, if I can help those people get their jobs done in the simplest possible way without forcing them to wade through the "awesomeness" of my framework, then I would call Hiro a success, and at the end of the day, that's really all that matters.</description><link>http://www.bytecodeartist.net/2009/04/introducing-hiro-worlds-fastest-ioc_15.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>16</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-1347962814343708249</guid><pubDate>Tue, 07 Apr 2009 23:42:00 +0000</pubDate><atom:updated>2009-04-15T09:32:14.289+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">hiro linfu ioc dotnet codeproject</category><title>Introducing Hiro, the World's Fastest IOC Container, Part I: Design Diary</title><description>Introduction&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Have you ever had one of those moments where someone told you that your work sucked, and it inspired you to create something better? About a month ago,&lt;a href="http://www.codeproject.com/script/Forums/View.aspx?fid=1532288&amp;msg=2962114"&gt;Alex Simkin sent me a message&lt;/a&gt; on the CodeProject forums for one of my articles saying that &lt;span style="font-weight:bold;"&gt;LinFu ranked second to last in performance&lt;/span&gt; among all the other containers, and that he was willing to show me the benchmark code that produced those numbers.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Eating the Humble Pie&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Unfortunately, Alex was correct. Despite all of its features, LinFu landed a spot near the bottom of the pack, and needless to say, there had to be a better way to design a container so that it wouldn't have these bottlenecks. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;"But...but...my container is dynamic and it's flexible!"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As an IOC container author myself, I've probably given that same excuse a dozen times over whenever someone complained that my framework was too slow. I never realized that the flexibility that I so touted in all my IOC articles was the same cause for all my performance headaches. Indeed, there had to be some way to improve these numbers, and at first, I thought adding more dynamic IL generation would solve the problem. After all, Lightweight Code Generation with DynamicMethod seems to be the trend nowadays among the other frameworks like Ninject, and that makes their code run faster, right?&lt;br /&gt;&lt;br /&gt;Once again, I was wrong. DynamicMethods didn't make much of a performance impact because Ninject (which reportedly uses a lot of LCG its code) was actually the slowest among all of the IOC containers tested in the benchmark (Sorry Nate). Of course, this doesn't mean that the DynamicMethod approach is the cause of the slowdown; what it does suggest, however, is that piling more and more reflection onto the speed problem is not the solution. In addition, there were other frameworks in that benchmark (such as Funq) that didn't use any reflection at all, and yet, they still were taking significant performance hits on that benchmark. In fact, even the fastest among all the other containers--StructureMap--&lt;span style="font-weight:bold;"&gt;was still running forty-four times slower&lt;/span&gt; than the Plain/No Dependency Injection use case!&lt;br /&gt;&lt;br /&gt;So the principle question is this: &lt;span style="font-style:italic;"&gt;"Where is this bottleneck coming from, and how do I eliminate it?"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;br /&gt;The Real Problem&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As it turns out, the answer was staring me in the face all along: &lt;span style="font-style:italic;"&gt;"It's the configuration, stupid"&lt;/span&gt;, I thought to myself. The problem is that every major IOC container at the time of this post (such as Ninject, StructureMap, Unity, AutoFac, Castle, LinFu, etc) essentially has to trawl through each one of its dependencies just to instantiate a single service instance on every call, and practically no amount of optimization will ever compensate for the fact that they still have to "rediscover" any given part of an application's configuration in order to instantiate that one service instance. Needless to say, this rediscovery process wastes a huge amount of resources because these containers are actually "rediscovering" a configuration that (for all practical purposes) will rarely change between two successive method calls. &lt;br /&gt;&lt;br /&gt;In layman's terms, this is akin to stopping and asking for directions &lt;span style="font-weight:bold;"&gt;at every intersection&lt;/span&gt; every time you want to leave your home to go to some other destination. There has to be some way to see the "whole map" and plan the trip ahead of time without having to stop for directions at every intersection. If you could plan all the possible routes on that trip ahead of time, then all the time you would have wasted asking for directions immediately vanishes.&lt;br /&gt;&lt;br /&gt;In essence, that is what I did with Hiro. Hiro is an IOC container framework that reads the dependencies in your application ahead of time and actually &lt;span style="font-style:italic;"&gt;compiles&lt;/span&gt; a custom IOC container that knows how to create those dependencies from your application itself. &lt;span style="font-style:bold;"&gt;It uses absolutely no runtime reflection or runtime code generation&lt;/span&gt;, and since all your dependencies are discovered at compile time (that is, when the Hiro compiler runs), Hiro suffers zero performance penalties at runtime when instantiating your types.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Yes, you read that right:&lt;/span&gt; Hiro runs at 1:1 speed with a Plain/No DI configuration. Here's the results of the IOC container benchmark:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/__BgjkW_AfhY/SeUbpE79siI/AAAAAAAAAFk/JDWa2PcPMsc/s1600-h/hiro_chart1.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 190px;" src="http://1.bp.blogspot.com/__BgjkW_AfhY/SeUbpE79siI/AAAAAAAAAFk/JDWa2PcPMsc/s320/hiro_chart1.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5324692526949315106" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;br /&gt;&lt;br /&gt;As you can see from the results above, the current crop of IOC containers (including LinFu) can only reach 2% of the speed of an application that does not use an IOC container. Now, let's take a look at Hiro's results:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/__BgjkW_AfhY/SeUcm-iO8iI/AAAAAAAAAFs/2XHbfWUCYaE/s1600-h/hiro_chart2.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 194px;" src="http://1.bp.blogspot.com/__BgjkW_AfhY/SeUcm-iO8iI/AAAAAAAAAFs/2XHbfWUCYaE/s320/hiro_chart2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5324693590382670370" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you don't believe it, then you can &lt;a href="http://hiro.googlecode.com/files/Performance.zi_"&gt;download and run the benchmarks yourself.&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;Like LinFu, Hiro is licensed under the terms of the LGPL, and you can preview the source code &lt;a href="http://hiro.googlecode.com"&gt;at this site&lt;/a&gt;. I'll also be starting a &lt;a href="http://code.google.com/p/hiro-contrib/"&gt;Hiro-contrib&lt;/a&gt; project, so if you want to add your own extensions, just email me at marttub@hotmail.com and I'll be more than happy to anyone who is interested. Thanks! :)</description><link>http://www.bytecodeartist.net/2009/04/introducing-hiro-worlds-fastest-ioc.html</link><author>noreply@blogger.com (Philip Laureano)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/__BgjkW_AfhY/SeUbpE79siI/AAAAAAAAAFk/JDWa2PcPMsc/s72-c/hiro_chart1.png" height="72" width="72" /><thr:total>11</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-9140983147527711577</guid><pubDate>Sun, 01 Mar 2009 05:40:00 +0000</pubDate><atom:updated>2009-03-01T17:05:09.662+11:00</atom:updated><title>Common Service Locator Adapter for LinFu 2.0 Released!</title><description>Now that &lt;a href="http://www.codeproject.com/KB/cs/LinFu_IOC.aspx"&gt;LinFu.IOC 2.0&lt;/a&gt; is officially out, it's time to join the rest of the IOC container pack and let users try out LinFu without tying them to any specific IOC container API. The adapter for LinFu makes it possible to use the &lt;a href="http://www.codeplex.com/CommonServiceLocator"&gt;Common Service Locator interfaces&lt;/a&gt; to make your applications completely container-agnostic, so if you haven't tried LinFu yet, &lt;a href="http://linfu.googlecode.com/files/CommonServiceLocator.LinFuAdapter.zip"&gt;&lt;span style="text-decoration: underline;"&gt;download the adapter&lt;/span&gt;&lt;/a&gt;, and give it a shot. :)</description><link>http://www.bytecodeartist.net/2009/02/common-service-locator-adapter-for.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-3322215167469560443</guid><pubDate>Fri, 07 Nov 2008 03:34:00 +0000</pubDate><atom:updated>2008-11-07T14:47:47.147+11:00</atom:updated><title>LinFu.DynamicProxy 1.01 is now a part of NHibernate!</title><description>After passing all 1500+ tests in the NHibernate test suite, &lt;a href="http://groups.google.com/group/nhibernate-development/browse_thread/thread/c0452d5a16749e85"&gt;LinFu.DP1 will now be an official part&lt;/a&gt; of the NHibernate distribution!&lt;br /&gt;&lt;br /&gt;It's really an honor to be a part of a project that is so well-known in the .NET community, and I really want to thank the NHibernate team for giving LinFu a chance.&lt;br /&gt;&lt;br /&gt;There were a lot of developers who shied away from LinFu DP1 for the lack of unit tests, and this proves that LinFu DP 1.0 works as advertised, regardless of whether or not the tests existed. You now have 1500+ reasons to try it out without worrying about any problems, so &lt;a href="http://www.codeproject.com/KB/cs/LinFuPart1.aspx"&gt;give it a shot&lt;/a&gt;, and I'll always be here to answer your questions if anything breaks.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;[Disclaimer: That being said, I'm now a TDD convert&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;I no longer develop anything without&lt;/span&gt; &lt;span style="font-style: italic;"&gt;a batter&lt;/span&gt;y &lt;span style="font-style: italic;"&gt;of automated unit tests&lt;/span&gt;--&lt;span style="font-style: italic;"&gt;but it's still an big ego-booster to know that your code works exactly as it's supposed to work :) ]&lt;/span&gt;</description><link>http://www.bytecodeartist.net/2008/11/linfudynamicproxy-101-is-now-part-of.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>8</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-6952102463086623381</guid><pubDate>Mon, 22 Sep 2008 06:37:00 +0000</pubDate><atom:updated>2008-09-22T17:28:11.908+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">linfu ioc versiontwo csharp</category><title>LinFu IoC 2.0 Reaches an Important Milestone</title><description>&lt;span style="font-weight: bold;"&gt;Making the Grade&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;After nearly two months of hard work, I am pleased to announce the impending release of LinFu.IoC v2.0! Today marks an amazing milestone in the development of LinFu's new IoC container because as of the latest build (revision 258), LinFu is the first IoC container framework to pass ALL tests in both the "MustHave" and the "ShouldHave" categories in &lt;a href="http://tinyurl.com/6l2ulh"&gt;the latest comparison&lt;/a&gt; between the following IoC Frameworks:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;AutoFac&lt;/li&gt;&lt;li&gt;Castle&lt;/li&gt;&lt;li&gt;LinFu&lt;/li&gt;&lt;li&gt;Ninject&lt;/li&gt;&lt;li&gt;StructureMap&lt;/li&gt;&lt;li&gt;Spring.NET&lt;/li&gt;&lt;li&gt;Unity&lt;/li&gt;&lt;/ul&gt;The feature lists might vary among these IoC frameworks, but Andrey's blog post does a great job of listing some of the features that an Inversion of Control container "must" have, in addition to the features that it "should" have", and I am proud to say that &lt;span style="font-weight: bold;"&gt;LinFu is the first IoC container to successfully implement every feature described in that blog post!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What this means is that LinFu.IoC (formerly Simple.IoC) has gone from an undocumented and untested inversion of control container to a fully documented, heavily tested, feature-laden container that is capable of performing all of the tasks that one would expect from a commercial-grade inversion of control container.&lt;br /&gt;&lt;br /&gt;In addition to the keeping the code as clean and as compact as possible, I spent countless hours ensuring that every single method, class, interface, property and enum is fully documented, regardless of whether or not that item was marked as public, internal, or private. I love my code and I love what I do, and I hope that shows in &lt;a href="http://code.google.com/p/linfu/wiki/BuildingLinFu2"&gt;the code that I write&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The best part about all this is that LinFu's IoC container framework weighs in at only 94KB, making it the smallest IoC framework among its brethen, with the Autofac container falling at a close 110KB.&lt;br /&gt;&lt;br /&gt;Within the next few weeks, I'll be publishing an article on CodeProject that details all the features that you can expect from LinFu.IoC 2.0, and I will time the release so that it coincides with the actual CodeProject article itself.&lt;br /&gt;&lt;br /&gt;Meanwhile, stay tuned!&lt;br /&gt;&lt;br /&gt;EDIT: I realize that this post is quite scant on details so if you want to dive straight into the LinFu.IoC source, you can just go &lt;a href="http://code.google.com/p/linfu/wiki/BuildingLinFu2"&gt;here&lt;/a&gt;. If you want to take a look at the IoC framework comparison project, &lt;a href="http://code.google.com/p/net-ioc-frameworks"&gt;click here&lt;/a&gt;. The code practically speaks for itself. Enjoy!</description><link>http://www.bytecodeartist.net/2008/09/linfu-ioc-20-reaches-important.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>12</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-8678376088043654852</guid><pubDate>Mon, 04 Aug 2008 04:25:00 +0000</pubDate><atom:updated>2008-08-04T18:17:46.536+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">linfu ioc versiontwo design dotnet csharp</category><title>Making Simple.IoC Even More Simple</title><description>Believe it or not, the following code represents most of LinFu.IoC's functionality when managing service instances:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// The SimpleContainer will handle unnamed services&lt;br /&gt;public class SimpleContainer : IContainer&lt;br /&gt;{&lt;br /&gt;   private readonly Dictionary&lt;type,&gt; _factories = new Dictionary&lt;type,&gt;();  &lt;br /&gt;&lt;br /&gt;   public virtual bool SuppressErrors&lt;br /&gt;   {&lt;br /&gt;       get; set;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public virtual void AddFactory(Type serviceType, IFactory factory)&lt;br /&gt;   {&lt;br /&gt;       _factories[serviceType] = factory;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public virtual bool Contains(Type serviceType)&lt;br /&gt;   {&lt;br /&gt;       return _factories.ContainsKey(serviceType);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public virtual object GetService(Type serviceType)&lt;br /&gt;   {&lt;br /&gt;       object result = null;&lt;br /&gt;       if (!_factories.ContainsKey(serviceType) &amp;amp;&amp;amp; !SuppressErrors)&lt;br /&gt;           throw new ServiceNotFoundException(serviceType);&lt;br /&gt;&lt;br /&gt;       if (!_factories.ContainsKey(serviceType) &amp;amp;&amp;amp; SuppressErrors)&lt;br /&gt;           return null;&lt;br /&gt; &lt;br /&gt;       // Use the corresponding factory&lt;br /&gt;       // and create the service instance&lt;br /&gt;       var factory = _factories[serviceType];&lt;br /&gt;       if (factory != null)&lt;br /&gt;           result = factory.CreateInstance(this);&lt;br /&gt;&lt;br /&gt;       return result;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// The named container will handle named services&lt;br /&gt;public class NamedContainer : SimpleContainer, INamedContainer&lt;br /&gt;{&lt;br /&gt;   protected readonly Dictionary&lt;string,&gt;&gt; _namedFactories =&lt;br /&gt;       new Dictionary&lt;string,&gt;&gt;();&lt;br /&gt;&lt;br /&gt;   public virtual void AddFactory(string serviceName, Type serviceType, IFactory factory)&lt;br /&gt;   {&lt;br /&gt;       if (serviceName == string.Empty)&lt;br /&gt;       {&lt;br /&gt;           AddFactory(serviceType, factory);&lt;br /&gt;           return;&lt;br /&gt;       }&lt;br /&gt;       // Create the entry, if necessary&lt;br /&gt;       if (!_namedFactories.ContainsKey(serviceName))&lt;br /&gt;           _namedFactories[serviceName] = new Dictionary&lt;type,&gt;();&lt;br /&gt;&lt;br /&gt;       _namedFactories[serviceName][serviceType] = factory;      &lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public virtual bool Contains(string serviceName, Type serviceType)&lt;br /&gt;   {&lt;br /&gt;       // Use the standard IContainer.Contains(Type)&lt;br /&gt;       // if the service name is blank&lt;br /&gt;       if (serviceName == string.Empty)&lt;br /&gt;           return Contains(serviceType);&lt;br /&gt;&lt;br /&gt;       return _namedFactories.ContainsKey(serviceName) &amp;amp;&amp;amp;&lt;br /&gt;              _namedFactories[serviceName].ContainsKey(serviceType);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public virtual object GetService(string serviceName, Type serviceType)&lt;br /&gt;   {&lt;br /&gt;       // Used the other GetService method if&lt;br /&gt;       // the name is blank&lt;br /&gt;       if (serviceName == string.Empty)&lt;br /&gt;           return GetService(serviceType);&lt;br /&gt;&lt;br /&gt;       // Determine if the service exists, and&lt;br /&gt;       // suppress the errors if necessary&lt;br /&gt;       bool exists = Contains(serviceName, serviceType);&lt;br /&gt;       if (!exists &amp;amp;&amp;amp; SuppressErrors)&lt;br /&gt;           return null;&lt;br /&gt;&lt;br /&gt;       if (!exists &amp;amp;&amp;amp; SuppressErrors != true)&lt;br /&gt;           throw new NamedServiceNotFoundException(serviceName, serviceType);&lt;br /&gt;&lt;br /&gt;       var factory = _namedFactories[serviceName][serviceType];&lt;br /&gt;&lt;br /&gt;       // Make sure that the factory exists&lt;br /&gt;       if (factory == null)&lt;br /&gt;           return null;&lt;br /&gt;&lt;br /&gt;       return factory.CreateInstance(this);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/type,&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/type,&gt;&lt;/type,&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you can see, there's nothing special about this code. Some might even scoff at it since it's too minimalistic to be useful. After all, one might say that an IoC container has far more responsibilities than just object instantiation. &lt;a href="http://www.ninject.org/"&gt;Ninject&lt;/a&gt;, for example has features such as contextual binding and method interception. Surely this isn't all there is to LinFu.IoC's features, is it?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Bend it like Ockham&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Despite the varying complexity of most (if not all) IoC containers in the field, logically speaking, the code listed above is the absolute minimum amount of code necessary to separate the instantiation of a service instance from its actual client code. When you request a service instance from a given IoC container (whether it be LinFu, Ninject or countless other containers out there), there has to be some point where the container has to decide if that service instance can be created, as well as manage the lifetime of that service once it is already out of the container. As most of us IoC container developers know, there's quite a lot more to a container than just managing the lifetime of its individual services. Creating the instance is only the first step, and I'll show you how LinFu version 2's new IoC container will implement the some of the same features without sacrificing its relatively-horizontal learning curve.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The Pattern of Other Containers&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Typically, these containers will use either property setter injection or constructor injection to autowire together all of the dependencies that an application might need during its lifetime. In addition, they might implement additional features such as interception, logging, and AOP. However, despite the differences in features among containers, there are two points where these features are commonly applied:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;When a service is about to be created (e.g. constructor injection) or,&lt;br /&gt;&lt;/li&gt;&lt;li&gt;When a service is already instantiated (e.g. property setter injection, or interception)&lt;/li&gt;&lt;/ul&gt;This implies that a developer can add additional features to their respective IoC container simply by controlling the point where the service is going to be created as well as controlling the point where the service has been recently instantiated. In fact, if you can isolate those two points from the rest of the container, you can effectively add new features without affecting the rest of the code.&lt;br /&gt;&lt;br /&gt;Now the reason why LinFu's IoC container can get away with such simple code is because it actually delegates its factory methods to an instance of the IFactory interface:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;   public interface IFactory&lt;br /&gt;{&lt;br /&gt;    object CreateInstance(IContainer container);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;LinFu's IoC container uses each factory instance to determine how a service implementation should be instantiated, and each factory instance, in turn, is responsible for managing the lifetime of each component that it creates. With that in mind, the only thing that you have to do to extend LinFu's IoC container is to control how each factory creates its object instances (such as deciding which constructor and constructor arguments to use when implementing constructor injection) and control the instances that come out of each factory.&lt;br /&gt;&lt;br /&gt;For example, if I wanted to add interception to LinFu.IoC, all I have to do is wrap a Decorator around an IContainer (or INamedContainer) instance that wraps each service instance in a proxy:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class ContainerDecorator : IContainer&lt;br /&gt;{&lt;br /&gt;  private IContainer _container;&lt;br /&gt;  public ContainerDecorator(IContainer realContainer)&lt;br /&gt;  {&lt;br /&gt;       _container = realContainer;&lt;br /&gt;  }&lt;br /&gt;  // Other methods skipped for brevity&lt;br /&gt;  public object GetService(Type serviceType);&lt;br /&gt;  {&lt;br /&gt;       // Grab the service instance&lt;br /&gt;       var result = container.GetService(serviceType);&lt;br /&gt;    &lt;br /&gt;       // Wrap the instance, if possible&lt;br /&gt;       if (result != null)&lt;br /&gt;            return SomeProxyFactory.Wrap(result);&lt;br /&gt;&lt;br /&gt;       // Otherwise return the original instance&lt;br /&gt;       return result;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;...and in the client code, using the additional decorator is as easy as:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;var container = new ContainerDecorator(new SimpleContainer());&lt;br /&gt;&lt;br /&gt;// Use the container somehow and transparently use the decorator to wrap the&lt;br /&gt;// result&lt;br /&gt;var service = container.GetService&amp;lt;ISomeServiceType&amp;gt;();&lt;br /&gt;&lt;br /&gt;// ...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As you can see, the implementation of LinFu.IoC is very straightforward, and there's really nothing exotic about the design. For those of you who were probably wondering why LinFu.IoC isn't using generics, here's the list of extension methods that completes the design:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;  public static class ContainerExtensions&lt;br /&gt;  {&lt;br /&gt;      public static T GetService&lt;t&gt;(this IContainer container)&lt;br /&gt;          where T : class&lt;br /&gt;      {&lt;br /&gt;          var serviceType = typeof (T);&lt;br /&gt;          return container.GetService(serviceType) as T;&lt;br /&gt;      }&lt;br /&gt;      public static T GetService&lt;t&gt;(this INamedContainer container, string serviceName)&lt;br /&gt;          where T : class&lt;br /&gt;      {&lt;br /&gt;          return container.GetService(serviceName, typeof (T)) as T;&lt;br /&gt;      }&lt;br /&gt;      public static void AddFactory&lt;t&gt;(this INamedContainer container, string serviceName, IFactory&lt;t&gt; factory)&lt;br /&gt;      {&lt;br /&gt;          IFactory adapter = new FactoryAdapter&lt;t&gt;(factory);&lt;br /&gt;          container.AddFactory(serviceName, typeof (T), adapter);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      public static void AddFactory&lt;t&gt;(this IContainer container, IFactory&lt;t&gt; factory)&lt;br /&gt;      {&lt;br /&gt;          IFactory adapter = new FactoryAdapter&lt;t&gt;(factory);&lt;br /&gt;          container.AddFactory(typeof(T), adapter);&lt;br /&gt;      }&lt;br /&gt;      public static void AddService&lt;t&gt;(this IContainer container, T instance)&lt;br /&gt;      {&lt;br /&gt;          container.AddFactory(typeof (T), new InstanceFactory(instance));&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Again, there's nothing unconventional in the design. In the end, it's the simplicity that matters most, and that is the difference that LinFu.IoC offers.</description><link>http://www.bytecodeartist.net/2008/08/making-simpleioc-even-more-simple.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-3507277525494666444</guid><pubDate>Tue, 29 Jul 2008 23:47:00 +0000</pubDate><atom:updated>2008-07-30T10:56:12.597+10:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">linfu redesign rewrite versiontwo</category><title>The LinFu Reimplementation Plan, and Dogfooding</title><description>Before I hit the ground coding, let's go over some of the features that were in LinFu v1.0 that will also be in 2.0:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Dynamic Proxies&lt;/li&gt;&lt;li&gt;Late Binding / Multiple Dispatch&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Duck Typing&lt;/li&gt;&lt;li&gt;Mixins&lt;/li&gt;&lt;li&gt;Universal Event Handling&lt;/li&gt;&lt;li&gt;Closures with Lambda Arguments&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Design By Contract&lt;/li&gt;&lt;li&gt;AOP (static and dynamic weaving)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Inversion of Control / Dependency Injection&lt;/li&gt;&lt;li&gt;Adaptive Object Models&lt;/li&gt;&lt;li&gt;A MyXaml Engine Clone&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Aside from the fact that v1.0 has a fairly hefty feature list, one thing that I've noticed with the source is that despite those features, LinFu itself doesn't really use its own IoC or DbC facilities to improve the quality of its own code, and that's a wasted opportunity.&lt;br /&gt;&lt;br /&gt;With that in mind, I've decided to implement the following features first:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Inversion of Control / Dependency Injection&lt;/li&gt;&lt;li&gt;Dynamic Proxies&lt;/li&gt;&lt;li&gt;Design by Contract&lt;/li&gt;&lt;li&gt;Static AOP Weaving&lt;/li&gt;&lt;/ul&gt;Building an IoC/DI container will help enforce a separation of concerns, and using Design by Contract in tandem with TDD will eliminate over 90% of the bugs in the code. The reason why I'm also implementing the static AOP weaving and dynamic proxy generator first is that there has to be a way to transparently inject those contracts into a service instance using a dynamic proxy without having to clutter the original source code. In addition, using the static AOP weaving in a debug build will allow me to dynamically inject diagnostic code without affecting the actual release build.&lt;br /&gt;&lt;br /&gt;As for LinFu's Design by Contract v3.0, my goal is to make it injection-agnostic. That means that the library itself should be completely unaware of how it's actually being injected into a service instance. It will allow you to inject contracts using a 3rd-party dynamic proxy generator (such as Castle), and most importantly, it should make no distinction between LinFu's static AOP injection and its own proxy generator.&lt;br /&gt;&lt;br /&gt;Probably the most striking difference that you'll see between v1 and v2 is that&lt;span style="font-weight: bold;"&gt; LinFu will no longer be split into multiple projects&lt;/span&gt; (aside from PostWeaver.exe, and the MSBuild task for LinFu.AOP).  There might be some who might say that clumping all of LinFu into one DLL violates the principle of Separation of Concerns, but in practice, it will save you an untold amount of headaches when managing your project dependencies.&lt;br /&gt;&lt;br /&gt;I realize that rebuilding LinFu is a very tall order, and I'm putting my heart and soul into this one to make sure that it's the best code that I'll ever write. This is going to take some time to redo, and I'll keep posting more and more updates on my blog as time progresses.</description><link>http://www.bytecodeartist.net/2008/07/linfu-reimplementation-plan-and.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>7</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7349901047715470998.post-2166003486293020026</guid><pubDate>Tue, 29 Jul 2008 02:50:00 +0000</pubDate><atom:updated>2008-07-29T13:14:41.689+10:00</atom:updated><title>Behavior-Driven Development to the Rescue?</title><description>I've been trying to wrap my head around how I can increase LinFu's test coverage rate for over a week now, and I have to admit that doing DDT testing (aka TDD in reverse, or post-hoc unit testing) is much more difficult than its TDD counterpart.&lt;br /&gt;&lt;br /&gt;In contrast, TDD seems to apply a bottom-up approach to ensure that each component is functioning, and in LinFu's case, I'm going to need a top-down testing approach to ensure that all of LinFu's features work as advertised. My intent is to write each test as a specification for LinFu's behavior, and each one of these tests will ensure that LinFu behaves according to its specification.&lt;br /&gt;&lt;br /&gt;It's by no means perfect, but it's the only practical solution that I can apply to the existing code base without having to rewrite it.  At best, it seems like an ugly hack since I'll be effectively doing black box testing against LinFu instead of getting the 100% test coverage.&lt;br /&gt;&lt;br /&gt;The other alternative, of course, is that I can do a complete rewrite of LinFu (presumably version 2.0) from scratch. (It's not a pretty option, but then again, it's no better than having to do post-hoc unit testing).&lt;br /&gt;&lt;br /&gt;So here is my question for the developer community at large: Which option should I take, and why? If I do LinFu v2.0 with a 100% test coverage rate, will that up the adoption rate, or will LinFu v1.0 with specification tests added be sufficient enough to fix the test coverage problems?</description><link>http://www.bytecodeartist.net/2008/07/behavior-driven-development-to-rescue.html</link><author>noreply@blogger.com (Philip Laureano)</author><thr:total>10</thr:total></item></channel></rss>
