tag:blogger.com,1999:blog-36146885641476480572024-03-05T05:14:46.580-08:00Technocratica - Plebeian CruciblesPlease use the reaction widget in every post to let me know if you found the post helpful. Appreciate your time to give me feedback. Thanks!!!Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.comBlogger45125tag:blogger.com,1999:blog-3614688564147648057.post-74114377341604939052013-02-24T09:52:00.001-08:002013-02-24T09:52:58.886-08:00Using Microsoft Fakes to Unit Test Entity Framework<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:466a5922-7baa-4022-9e73-e99cfe1b2187" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/Microsoft+Fakes" rel="tag">Microsoft Fakes</a>,<a href="http://technorati.com/tags/Entity+Framework" rel="tag">Entity Framework</a>,<a href="http://technorati.com/tags/Unit+Testing" rel="tag">Unit Testing</a></div> <p align="justify">If you have used Entity Framework, then I am sure you have read in various blogs that using the Repository pattern is the best way to test it.</p> <p align="justify">Though I hardly have the expertise to challenge that notion, I take that statement with caution. The pattern itself is great for usages where you want to shield your data access from your middle tier, my personal opinion is that the most value of a repository pattern is achieved when you want to access disparate sources but provide a consistent experience of interacting with entities.</p> <p align="justify">So the idea of introducing a pattern for the sake of testability seemed a bit strange. For one, I couldn’t fathom why the application should carry the weight of an additional pattern when it is going to be used only for testing. So I went about exploring alternate options of testing EF without using the repository pattern. Its no secret that I am an admirer of Fakes (Wrote about its ancestor Pex and Moles <a href="http://vaideeswaranr.blogspot.com/2012/06/mocking-aspnet-objects-with-microsoft.html">here</a>), so I decided to see if Fakes will stand up to the challenge of unit testing EF.</p> <p align="justify">The structure of my solution itself is fairly trivial. I have a service project with an edmx in it and one GetValue method that returns an entity from my table using EF. A unit test project completes the solution.</p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6El-qyhapS7s2ZJna3_cRbDV-7WEpO0tIoMKta78aUwNKbasrabs1CL3hjGIBxrXMzJQCaQR6GfbhiC6Yxg1IJFrV4QG3RyTsqAN7X9yDnUF2wy5vjaPqLjqFQEQ2ht3WcggmvdNjDfQ/s1600-h/image5.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0U7SncVNb87IH3wFIACxRHhnhyhhy57j50Os4uE5uNSLxeUeJGE5Qz1c5VNAH14VKx0207r1INoXWmA3lkkfyyfib7qtVMXda0NGd3Ebo4pEZjDIkAlkkAmk2WECOON_h9Yzx-7AwsVM/?imgmax=800" width="343" height="276"></a></p> <p>My service method</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FakesService : IFakesService</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">public</span> MyEntity GetValue(<span style="color: #0000ff">int</span> <span style="color: #0000ff">value</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">using</span> (MyDbContext entities = <span style="color: #0000ff">new</span> MyDbContext())</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #0000ff">return</span> entities.MyEntities.Find(<span style="color: #0000ff">value</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">This is the method I want to test. To shim my EF entities, I created a Fakes Assembly for the service dll in my test project..</p><br /><p>The method that we are interested in shimming is the MyEntitiesGet; done with the code below</p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> ShimMyDbContext.AllInstances.MyEntitiesGet = (shimContext) =></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">return</span> shimContext.MyEntities;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> };</pre><!--CRLF--></div></div><br /><div> </div><br /><div align="justify">But there is an issue with the above snippet. I am detouring the Entities Get to execute my code, but not mocking anything. Because the shimContext has a reference to MyDbContext, the code executed will still be the EF code, pointing to the database. To truly mock this, I will have to create a fake DbContext.</div><br /><div align="justify"> </div><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FakesContext : DbContext</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">public</span> DbSet<MyEntity> MyEntities { get; set; }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">public</span> FakesContext()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">To have this constructor invoked when MyDbContext class is initialized, it has to be shimmed</p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> MyDbContext.Constructor = (paramDbContext) => GenerateConstructor();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #0000ff">private</span> DbContext GenerateConstructor()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> FakesContext();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">So, that’s all!! Or so it would seem. Just when I was ready to celebrate my little victory of mocking EF, something hit when I debugged the test method. </p><br /><p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Cixzsgm4I_ugeGKR5PsMkzvL2e6snQNTUbprLFpIJljqMhRiU__OZNvjYiBaTBG7pdiFydGA2ygI5KKBnRIdvb9Oe06y3rYy3SG-CQyPhNhyphenhyphen1k4b0eLdxEsNfbvNYcHyIpYBGHAwSQo/s1600-h/image12.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-u1BuIEkzxzI/USpTef_0iMI/AAAAAAAAAWQ/DbsD8QTkDxA/image_thumb8.png?imgmax=800" width="367" height="220"></a></p><br /><p align="justify">The line that was causing this error was</p><br /><p align="justify">return entities.MyEntity.Find(value);</p><br /><p align="justify">Digging a bit more, I found that the using statement was the culprit and the error occurred in Dispose method of the DbContext object in EF. At first I was left scratching my head. But then I realised that since we are mocking the constructor, it is trying to dispose the original MyDbContext class and it isn’t able to. After trying various alternatives, I came to the conclusion that the only way to get around this is to shim the Dispose method of the EF DbContext. To do this, I had to generate a Fakes Assembly for the EntityFramework dll (I am using EF 5). I wasn’t sure how well Fakes was going to handle this but to my surprise, it went smoothly enough and I ended up with the method below. </p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> ShimDbContext.AllInstances.Dispose = (context) => DummyDispose();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> DummyDispose()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> { </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">The only thing left to do now, is to return my own list of entities instead of ones from the database. We go back to the EntitiesGet shim to do that. The code for that now looks something like the below.</p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> MyDbContext.AllInstances.MyEntitiesGet = (shimContext) =></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> FakesContext dummyContext = <span style="color: #0000ff">new</span> FakesContext();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> Database.SetInitializer<FakesContext>(<span style="color: #0000ff">null</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> Entity entity1 = <span style="color: #0000ff">new</span> Entity();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> entity1.Id = 1;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> entity1.Name = <span style="color: #006080">"First created from test"</span>;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> dummyContext.MyEntities.Add(entity1);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> Entity entity2 = <span style="color: #0000ff">new</span> Entity();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> entity2.Id = 2;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> entity2.Name = <span style="color: #006080">"Second created from test"</span>;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> dummyContext.MyEntities.Add(entity2);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> <span style="color: #0000ff">return</span> dummyContext.MyEntities;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> };</pre><!--CRLF--></div></div><br /><p align="justify">Nothing out of ordinary except for the SetInitializer statement. That is to turn off tracking by EF. There is a post by Scot Hanselman (I think) on it, so I wont go into it too much.</p><br /><p align="justify">So there you go! A successfully mocked unit test on EF. I have to admit that I haven’t tried mocking other EF methods and there could be challenges there, but with this little exercise, I am fairly confident that those challenges can be overcome. Happy Coding!!</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com2tag:blogger.com,1999:blog-3614688564147648057.post-42882159522514932932012-12-07T15:30:00.001-08:002012-12-07T15:30:15.730-08:00My experiments with Windows Azure Mobile Services- Part II<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5222d53c-b885-4e09-9340-f2ca0a42faaa" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/Azure+Mobile+Services" rel="tag">Azure Mobile Services</a>,<a href="http://technorati.com/tags/custom+parameters" rel="tag">custom parameters</a>,<a href="http://technorati.com/tags/read" rel="tag">read</a>,<a href="http://technorati.com/tags/script" rel="tag">script</a></div> <p align="justify">The first post on this topic was written in October <a href="http://vaideeswaranr.blogspot.com/2012/10/my-experiments-with-windows-azure.html">here</a>. Though I wanted to follow it up quickly with Part II, I got distracted, but here I am with the second part of Azure Mobile Services.</p> <p align="justify">One of the things that wasn’t detailed in the first post was the Ratings class. So I will start from there. The ratings class is simply an instance of the ratings table in the database. It hence inherits the AzureMobileTable class that we saw in the previous post. </p> <p align="justify">The table has the columns listed below</p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTMCYPhPYdt5iWOALWgiCFHrbmoCUWOAdJUff2qYpC1RrYzF8Kyr-RMcaV2Bomvv-XMcKbpoMAVAr6nAgoQncMDBNT7SoQ6RMi8ZYUqqPR7KjkGCW7Fti7eIfn7kRSSH5sqYl7ReHj8PA/s1600-h/image%25255B4%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-xIgD6-ZuEzs/UMJ8BL1TRII/AAAAAAAAAVk/uWNsIFkokdM/image_thumb%25255B2%25255D.png?imgmax=800" width="438" height="195"></a></p> <p align="justify">The scenario that we will deal with in this post, is to calculate the Average ratings for any given movie. This can be done in two ways</p> <ul> <li> <div align="justify">Get all the movies to the service and calculate the average in the service. The downside of this is that if I have a lot of ratings for a movie I have to fetch all the records which is inefficient.</div> <li> <div align="justify">The second option is to calculate the Average in Azure Mobile Services and return the records. The question though is, how to do this with Azure Mobile Services.</div></li></ul> <p align="justify">Azure Mobile Services has support for scripting. Every table has a tab called script and when you click on it, you will see that you can write your script for 4 operations; Insert, Update, Delete and Read. This is what we will use for calculating averages. But there is a catch. Each operation just supports one implementation of the script. The read, for example by default executes read all, which we have already used. So you cannot write another read script to do Averages. </p> <p align="justify">One way to get around this limitation is to create another Azure Mobile Service table called Averages and script the read operation of this table to calculate averages for the ratings table. Some code snippet is in order. So here goes</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">function</span> <span style="color: #0000ff">read</span>(query, <span style="color: #0000ff">user</span>, request) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> mssql.query("<span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">AVG</span>(<span style="color: #0000ff">Value</span>) <span style="color: #0000ff">as</span> Average <span style="color: #0000ff">from</span> Ratings <span style="color: #0000ff">WHERE</span> MovieID = ?", [query.id], {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> success:<span style="color: #0000ff">function</span>(results) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> request.respond(200, results[0]); </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> });</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #008000">//console.log(query);</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> <span style="color: #008000">//console.log(query.getComponents());</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">The script calculates the average for a given movie id and returns the status of 200 along with the results. The commented code just show you how to use logging.</p><br /><p align="justify">Now, what is left to do is to call this from our service described in Part I. There is a method in the Ratings class that calls this script. </p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">using</span> System;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">using</span> System.Linq;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">using</span> System.Web;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">namespace</span> TMDBHelperService.AzureMobileServices</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Ratings: AzureMobileTable</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> <span style="color: #0000ff">public</span> Ratings():<span style="color: #0000ff">base</span>(<span style="color: #006080">"ratings"</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> <span style="color: #0000ff">public</span> AzureMobileQuery getAverage(<span style="color: #0000ff">int</span> movieId)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> AzureMobileQuery(<span style="color: #006080">"tables/averages/"</span> + movieId.ToString());</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">Note the getAverage method queries against the Averages table and not the ratings table. The only other thing left to do is to call this method from our broker. </p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> getAverageRatings(<span style="color: #0000ff">int</span> movieId)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> var _ratingsTable = <span style="color: #0000ff">new</span> Ratings();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> AzureMobileQuery _query = _ratingsTable.getAverage(movieId);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">if</span> (_query!=<span style="color: #0000ff">null</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #0000ff">return</span> _helperClass.Execute(_query); </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> <span style="color: #0000ff">return</span> String.Empty;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> }</pre><!--CRLF--></div></div><br /><p align="justify"></p><br /><p align="justify">This solution works very well if you just have one or two operations. But it quickly becomes unmanageable if you have write a bunch of custom operations on a table. </p><br /><p align="justify">The better way of doing this is to use custom parameters. You can pass custom parameters by appending them as query strings to the table uri. Something like below</p><br /><p align="justify">tables/ratings?ops=avg&movieid=<movieid></p><br /><p align="justify">Then change the ratings read query to do different operations based on these parameters. </p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">function</span> <span style="color: #0000ff">read</span>(query, <span style="color: #0000ff">user</span>, request) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #0000ff">if</span>(request.<span style="color: #0000ff">parameters</span>.ops == "<span style="color: #0000ff">avg</span>")</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> console.log(request.<span style="color: #0000ff">parameters</span>.ops);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> console.log(request.<span style="color: #0000ff">parameters</span>.movieid);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> mssql.query("<span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">AVG</span>(<span style="color: #0000ff">Value</span>) <span style="color: #0000ff">as</span> Average <span style="color: #0000ff">from</span> Ratings <span style="color: #0000ff">WHERE</span> MovieID = ?", [request.<span style="color: #0000ff">parameters</span>.movieid], {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> success:<span style="color: #0000ff">function</span>(results) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> request.respond(200, results[0]); </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> });</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> <span style="color: #0000ff">else</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> request.<span style="color: #0000ff">execute</span>();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">This is a bit more elegant way of “overloading” the read operation without the overhead of additional tables. </p><br /><p align="justify">I find myself using Azure Mobile Services more and more. I love the simplicity of it for small applications and the speed with which I can get it up and running. They are not perfect but if you are on the fence, I highly encourage you to try them.</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-11222330111338533502012-10-10T20:46:00.001-07:002012-10-12T10:23:10.102-07:00My experiments with Windows Azure Mobile Services – Part I<p align="justify"> </p> <p align="justify">In August, Microsoft released a new Azure service called Windows Azure Mobile Services. These services, make developing windows store apps easier. If you haven’t started taking a look at it, you can start <a href="http://www.windowsazure.com/en-us/develop/mobile/">here</a>. If you are thinking about using them in your app, you have to be aware of the fact that the javascript client and the server side client versions have some differences.</p> <p align="justify">While most of the fundamentals are the same, I believe the C# version of the Mobile Services Client is a bit more comprehensive than the Javascript version. This being the first version, I am hoping they are going to have the same features across both the clients.</p> <p align="justify">This post deals with using the client in javascript apps. I am not going to go into the details of how to get it working. There is a lot of literature out there that covers this. In short, you download the extension for javascript, get your app key from the portal and use the client library to make calls to the service. </p> <p align="justify">This works very well. With a couple of lines of code, you get your data to the app. But, I was wary of leaving the app key in the client machine. I also want all my business rules in the service not in the client app.</p> <p align="justify">So I was looking to access the Mobile Services from a service.Also, my app already talks to a service that I host on azure, so I wanted all logic to be on the service not on the client. Unfortunately, you can’t use the client library in a WCF Service project. So I decided to figure out how to call the Mobile Services from a service.</p> <p align="justify">At the backend, the Mobile Services client library just calls a REST API. You can see this by stepping through the code in MobileServices.js. The code that actually makes the call is </p> <div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; height: 30px; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 86.27%; background-color: #f4f4f4"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> Platform.webRequest(options, handler);</pre><!--CRLF--></div></div><br /><p align="justify">Once I figured that out, I decided to write code in my service that does the same. </p><br /><p align="justify">Let me give you some context about what my app does. The app allows users to rate movies. It talks to an external webservice to get the list of movies and stores the ratings in Azure Mobile Web Services table. My Azure Mobile Service table structure is shown below</p><br /><p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYw-3OkziY_1webu1e1fUUlJcApl6TjuGYv1O6pokooVb3Cm6q4aBApeu_vGW3RX87nB52lDcP8kp_qkolk5ANwUyB-wYM1d4jSFZk7YYzk5yxMEg8iT48pFjfftPzfcUuycRx24K6kxY/s1600-h/image%25255B6%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-luk8x1ghgEE/UHZBCajNO9I/AAAAAAAAAU4/90vjd3q7CuE/image_thumb%25255B4%25255D.png?imgmax=800" width="451" height="290"></a></p><br /><p align="justify">The MovieId stores the movie id from the external service and the Value column stores the ratings of the movie. This is the table I am going to query from my service. </p><br /><p align="justify"><a href="http://lh4.ggpht.com/-fCXT4fwL07g/UHZBDJLVL-I/AAAAAAAAAVA/Ms4L_N92yxo/s1600-h/image%25255B12%25255D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-hPFJecWrJug/UHZBELEuYEI/AAAAAAAAAVI/kkciwu6FDmQ/image_thumb%25255B8%25255D.png?imgmax=800" width="447" height="317"></a></p><br /><p align="justify">I created the classes above to do this. A brief description of the classes before we delve into the code within each of them</p><br /><p align="justify"><strong>AzureMobileTable</strong> – This loosely represents an Azure Mobile Services Table. I say loosely because, the methods here are not present in Azure Mobile Services. These will be translated into REST URIs as you will shortly see.</p><br /><p align="justify"><strong>AzureMovieBroker</strong> – This acts as the broker between the WCF service and Azure Mobile Services. This will implement the methods that are needed by the app.</p><br /><p align="justify"><strong>AzureMobileHelper</strong> – This is the helper class that runs the methods on Azure Mobile Services. i.e. Makes the REST call. The helper class always executes a query on a table, so it needs a handle to both these objects.</p><br /><p align="justify"><strong>AzureMobileQuery</strong> – This class makes up the queries. Queries like where, order by and so on. </p><br /><p align="justify"><strong>Ratings</strong> – This is the implementation of the ratings table we saw earlier. This class extends the functionality of the Azure table by providing custom operations. Average is an example. All tables might not need an Average operation. For the app, the ratings table returns the average rating for a movie.</p><br /><p align="justify">Now that we have looked at the classes, let us see how the code works. First the <strong>MobileHelper</strong></p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; height: 680px; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 102.67%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> AzureMobileHelper</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">string</span> _baseurl;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">string</span> _appKey;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">string</span> _installationId;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #0000ff">public</span> AzureMobileHelper(<span style="color: #0000ff">string</span> url, <span style="color: #0000ff">string</span> appKey, <span style="color: #0000ff">string</span> installationId)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> _baseurl = url;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> _appKey = appKey;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> _installationId = installationId;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> <span style="color: #0000ff">public</span> AzureMobileTable getTable(<span style="color: #0000ff">string</span> tableName)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> AzureMobileTable(tableName);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Execute(AzureMobileQuery query)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> <span style="color: #0000ff">string</span> fullUri = _baseurl + query.Uri;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> HttpWebRequest _azureRequest = WebRequest.Create(fullUri) <span style="color: #0000ff">as</span> HttpWebRequest;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> _azureRequest.Headers.Add(<span style="color: #006080">"X-ZUMO-APPLICATION"</span>, _appKey);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> _azureRequest.Headers.Add(<span style="color: #006080">"X-ZUMO-INSTALLATION-ID"</span>, _installationId);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> <span style="color: #0000ff">using</span> (HttpWebResponse restResponse = _azureRequest.GetResponse() <span style="color: #0000ff">as</span> HttpWebResponse)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum27" style="color: #606060"> 27:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum28" style="color: #606060"> 28:</span> Encoding enc = System.Text.Encoding.GetEncoding(1252);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum29" style="color: #606060"> 29:</span> StreamReader _jsonStream = <span style="color: #0000ff">new</span> StreamReader(restResponse.GetResponseStream(),enc);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum30" style="color: #606060"> 30:</span> <span style="color: #0000ff">return</span> _jsonStream.ReadToEnd();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum31" style="color: #606060"> 31:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum32" style="color: #606060"> 32:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum33" style="color: #606060"> 33:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum34" style="color: #606060"> 34:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">The MobileHelper constructor takes three arguments. The url, the appKey and the installation id. The appKey and the Installation Id are used by Azure Mobile Services to authenticate a request. This is based on a setting in the management portal (You can also choose to authenticate using LIVE Id). You will get the appKey and the url from the management portal. I got the installation id from MobileServices.js (this will be in the request header). At the moment, I don’t know of another way to get it.</p><br /><p align="justify">The getTable method returns a handle to the AzureMobileTable object. We will take a look at the MobileTable constructor momentarily.</p><br /><p align="justify">The execute method is the method that does the job. It creates a web request, adds the necessary headers and reads the response returning it as a string. Any response received from Mobile Services is in the JSON format. Pretty simple.</p><br /><div><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> AzureMobileTable </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">string</span> _tableName = <span style="color: #0000ff">string</span>.Empty;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> tableUrl { </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> get{</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">return</span> <span style="color: #006080">"tables/"</span> + _tableName;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> <span style="color: #0000ff">public</span> AzureMobileTable(<span style="color: #0000ff">string</span> tableName)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> _tableName = tableName;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> <span style="color: #0000ff">public</span> AzureMobileQuery Where(<span style="color: #0000ff">string</span> whereExpression)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> Expression convertedExpression = Expression.Constant(whereExpression);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> <span style="color: #0000ff">if</span> (convertedExpression <span style="color: #0000ff">is</span> MethodCallExpression)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception(<span style="color: #006080">"Invalid Expression"</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> AzureMobileQuery(tableUrl + <span style="color: #006080">"?$filter="</span> + whereExpression);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum27" style="color: #606060"> 27:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">The AzureMobileTable class in a way, is the glue that sort of holds everything together. It does this very simply, by building the URIs. The constructor takes in the table name and builds the URI for a table by prefixing tables/.</p><br /><p align="justify">At the moment, the class only has one filter operation which is Where. It first validates if this is a valid LINQ Expression and if it is, constructs the appropriate URI. I will be adding more operations, but you get the idea. The last class we will see in this post is the AzureMobileQuery</p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> AzureMobileQuery</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Uri { </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> get{</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">return</span> _uri;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _uri;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> <span style="color: #0000ff">public</span> AzureMobileQuery(<span style="color: #0000ff">string</span> uri)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> _uri = uri;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> }</pre><!--CRLF--></div></div><br /><p>The class is pretty much self explanatory. The last piece of the jigsaw is the Broker class that initiates the call. </p><br /><div id="codeSnippetWrapper"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> getRatingsbyId(<span style="color: #0000ff">int</span> movieId)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> AzureMobileHelper _helperClass = <span style="color: #0000ff">new</span> AzureMobileHelper(<span style="color: #006080">"<url>"</span>, <span style="color: #006080">"<appKey>"</span>, <span style="color: #006080">"<installationid>"</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> AzureMobileTable _moviesCollection = _helperClass.getTable(<span style="color: #006080">"ratings"</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> AzureMobileQuery _query = _moviesCollection.Where(<span style="color: #006080">"MovieId eq '"</span> + movieId + <span style="color: #006080">"'"</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">if</span> (_query != <span style="color: #0000ff">null</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> <span style="color: #0000ff">return</span> _helperClass.Execute(_query);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> <span style="color: #0000ff">return</span> String.Empty;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> }</pre><!--CRLF--></div></div><br /><p align="justify">The method instantiates the helper class with app specific url, app key and installation id. As explained before, it then gets a handle to the table that the query needs to be executed on and the actual query that gets executed. The query in this case just filters all entries for a given movie id.</p><br /><p align="justify">I have consciously not touched on the ratings class because it solves another problem area that I will deal with in the next post. The above code is just an illustration of how services can call Azure Mobile Services. </p><br /><p align="justify">The fact that Azure Mobile Services is based on REST gives the developers great flexibility on how to access it and what to do with it. I am not sure when the Mobile Services team plan to release a client library for services or if they even plan to, but if you need to access the services from a service, this has hopefully given you a starting point. Happy Coding!!!</p><br /><div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3be75eed-507c-4a7b-a028-02cc21d6a5da" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/Windows+Azure+Mobile+Services" rel="tag">Windows Azure Mobile Services</a>,<a href="http://technorati.com/tags/WCF" rel="tag">WCF</a>,<a href="http://technorati.com/tags/Mobile+Services" rel="tag">Mobile Services</a></div> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-36103862601057332712012-07-02T15:41:00.001-07:002012-07-02T16:21:30.649-07:00Javascript Metro App Search with Web Services<p align="justify">If you are here, it probably means you are not contented with the default search implementation in javascript metro apps. The default search method when you add a search contract is as below</p> <div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #008000">// This function populates a WinJS.Binding.List with search results for the</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #008000">// provided query.</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> searchData: <span style="color: #0000ff">function</span> (queryText) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">var</span> originalResults;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">var</span> regex;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #008000">// TODO: Perform the appropriate search on your data.</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #0000ff">if</span> (window.Data) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> originalResults = Data.items.createFiltered(<span style="color: #0000ff">function</span> (item) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> regex = <span style="color: #0000ff">new</span> RegExp(queryText, <span style="color: #006080">"gi"</span>);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> <span style="color: #0000ff">return</span> (item.title.match(regex) || item.subtitle.match(regex) || item.description.match(regex));</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> });</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> } <span style="color: #0000ff">else</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> originalResults = <span style="color: #0000ff">new</span> WinJS.Binding.List();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> <span style="color: #0000ff">return</span> originalResults;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> },</pre><!--CRLF--></div></div><br /><div class="csharpcode" align="justify"><pre></pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p align="justify">Pay attention to <strong>line number 8</strong>. The way the search results are populated is by creating a filtered list of Data.items that match the query text. Which means, you should already have items loaded into the app to be able to search from them. But what if, I do not load all the items in the app but want to search for items in my database that I haven’t loaded?</p><br /><p align="justify">If you haven’t guessed already, that is exactly what this post will show. Be aware that this is not the only implementation that is possible for this. This is just the way I did it. I really did not see any help or a post that shows this, so I thought I will blog about it, just in case somebody is looking for something similar. And just so you know, this was built using Windows 8 Release Preview, so it could be a bit different if you are on Windows 8 CP.</p><br /><p align="justify">When you add a search contract to your project, there are two methods in searchResults.js that are of interest to us. They are <strong>handleQuery</strong> and <strong>searchData</strong>. You will see that the handleQuery in turn calls searchData and a couple of other methods. The search data method is synchronous because it is only filtering items that have already been fetched. If you want to call a web service when the user searches, then this method has to be asynchronous since all web service calls made using WinJS.Xhr are asynchronous by default. </p><br /><p align="justify">I am going to add a new method in data.js called searchEventHandler</p><br /><div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">function</span> searchEventHandler(data) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #0000ff">var</span> searchList = <span style="color: #0000ff">new</span> WinJS.Binding.List();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> WinJS.Promise(<span style="color: #0000ff">function</span> (c, e, p) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> WinJS.xhr({ type: <span style="color: #006080">"POST"</span>, url: localSettings.values[<span style="color: #006080">"webserviceUri"</span>], headers: { <span style="color: #006080">"Content-type"</span>: <span style="color: #006080">"application/x-www-form-urlencoded"</span> }, data: data }).done(</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">function</span> (result) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">if</span> (result != <span style="color: #0000ff">null</span> && result.responseText != <span style="color: #006080">""</span>) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #0000ff">var</span> jsonData = JSON.parse(result.responseText);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">var</span> i = 0; i < jsonData.length; i++) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> <span style="color: #008000">//process your results if need be</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> searchList.push(jsonData[i]);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> c(searchList);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> },</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> <span style="color: #0000ff">function</span> error(result) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> <span style="color: #0000ff">if</span> (result.responseText != <span style="color: #006080">""</span>) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> <span style="color: #0000ff">var</span> item = <span style="color: #0000ff">new</span> Object();</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> item.title = <span style="color: #006080">"Error occurred."</span>;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> item.group = sampleGroups[0];</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> item.backgroundImage = lightGray;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> searchList.push(item);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> e(searchList);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> );</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum27" style="color: #606060"> 27:</span> });</pre><!--CRLF--></div></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p align="justify">Will take a moment to explain what this method does. It calls the web service passing the query string as a parameter and adds the results to a WinJS binding list. To make this method asynchronous, I have used WinJS.Promise. The method then calls the success or the error call back with the search results as a parameter. With this, we now have an asynchronous method that calls a web service and returns the search results. We now have to call this method from searchResults.js. Add this method to the data namespace, so it can be called from searchResults. </p><br /><div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> WinJS.Namespace.define(<span style="color: #006080">"data"</span>, {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> items: groupedItems,</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> products:productsList,</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> groups: groupedItems.groups,</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> getItemsFromGroup: getItemsFromGroup,</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> searchEventHandler: searchEventHandler</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> });</pre><!--CRLF--></div></div><br /><p>The searchData function then needs to be modified as below</p><br /><div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #008000">// This function populates a WinJS.Binding.List with search results for the</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #008000">// provided query.</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> searchData: <span style="color: #0000ff">function</span> (queryText) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">var</span> originalResults;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #008000">//set the criteria to en-us and on-demand</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">var</span> criteria = <span style="color: #006080">"culture=en-us&eventType=3&kwdAny="</span> + queryText;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> data.searchEventHandler(criteria).done(<span style="color: #0000ff">function</span> (results) {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> originalResults = results;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> searchResults.generateFilters(originalResults);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> searchResults.populateFilterBar(currentElement, originalResults);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> });</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> }</pre><!--CRLF--></div></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p>Apart from the obvious change of calling the searchEventHandler, also note that the generateFilters and the populateFilterBar functions are being called from here instead of handleQuery. This is because both those functions depend on the results. I am leaving out more obvious code changes here like making currentElement a class variable.</p><br /><p>One last thing that is left to do is to generate the filters. If the filters are generated from the results, then the generateFilters method has to be changed to update your filters after the search is done rather than before. Since it depends on your implementation of the filters, I am not including that code here.</p><br /><p>As I stated in the post earlier, there are innumerable variations through which this can be achieved. Please feel free to let me know if you have done this any differently.</p><br /><div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:29dac975-3081-405d-94b8-4357440cc3c1" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/Metro" rel="tag">Metro</a>,<a href="http://technorati.com/tags/Windows+8" rel="tag">Windows 8</a>,<a href="http://technorati.com/tags/Javascript" rel="tag">Javascript</a>,<a href="http://technorati.com/tags/Search" rel="tag">Search</a></div> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com5tag:blogger.com,1999:blog-3614688564147648057.post-18610558505467091282012-06-11T18:54:00.001-07:002012-06-11T18:54:11.070-07:00Mocking ASP.NET Objects with Microsoft Moles<p align="justify">We have a lot of legacy code in one of our ASP.NET projects and I was looking for ways to test it without having to refactor them too much. I started using Pex and Moles to generate some methods and see if they did the job for me. It hasn’t been a bad road so far. There have been some bumps and pits but nothing yet that has forced me to go back. </p> <p align="justify">While I was writing unit tests using Moles, I did not find a lot of help on how we can mock some ASP.NET objects like query strings, for example. There are some teasers in this link <a href="http://rickardnilsson.net/post/2010/04/19/Isolate-your-code-from-ASPNET-with-Moles-Isolation-Framework.aspx">here</a><strong>,</strong> but nothing that got me onto fourth gear. So I thought I will just ink down my findings on my journey so far in case it helps any of you. This post assumes that you already have Pex and Moles set up, so I am not going to deal with setting them up or creating a unit test using Pex. </p> <p align="justify">I have code that reads the query string and based on the value of the query string returns a file name. This code is in the code behind of a user control. The code snippet looks like one below</p> <div class="csharpcode"><pre class="alt"><span class="lnum"> 1: </span><span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> GetFileName()</pre><pre><span class="lnum"> 2: </span>{</pre><pre class="alt"><span class="lnum"> 3: </span> </pre><pre><span class="lnum"> 4: </span> <span class="kwrd">int</span> queryStringValue;</pre><pre class="alt"><span class="lnum"> 5: </span> </pre><pre><span class="lnum"> 6: </span> </pre><pre class="alt"><span class="lnum"> 7: </span><span class="kwrd">if</span> (HttpContext.Current.Request.QueryString[<span class="str">"fileType"</span>] != <span class="kwrd">null</span> && <span class="kwrd">int</span>.TryParse(HttpContext.Current.Request.QueryString[<span class="str">"fileType"</span>], <span class="kwrd">out</span> queryStringValue) == <span class="kwrd">true</span>)</pre><pre><span class="lnum"> 8: </span> {</pre><pre class="alt"><span class="lnum"> 9: </span> <span class="kwrd">switch</span> (queryStringValue)</pre><pre><span class="lnum"> 10: </span> {</pre><pre class="alt"><span class="lnum"> 11: </span> <span class="kwrd">case</span> 1:</pre><pre><span class="lnum"> 12: </span> strPageName = <span class="str">"Unix.html"</span>;</pre><pre class="alt"><span class="lnum"> 13: </span> <span class="kwrd">break</span>;</pre><pre><span class="lnum"> 14: </span> <span class="kwrd">case</span> 2:</pre><pre class="alt"><span class="lnum"> 15: </span> strPageName = <span class="str">"Windows.html"</span>;</pre><pre><span class="lnum"> 16: </span> <span class="kwrd">break</span>;</pre><pre class="alt"><span class="lnum"> 17: </span> <span class="kwrd">case</span> 3:</pre><pre><span class="lnum"> 18: </span> strPageName = <span class="str">"iOS.html"</span>;</pre><pre class="alt"><span class="lnum"> 19: </span> <span class="kwrd">break</span>;</pre><pre><span class="lnum"> 20: </span> <span class="kwrd">case</span> 4:</pre><pre class="alt"><span class="lnum"> 21: </span> strPageName = <span class="str">"Chrome.html"</span>;</pre><pre><span class="lnum"> 22: </span> <span class="kwrd">break</span>;</pre><pre class="alt"><span class="lnum"> 23: </span> }</pre><pre><span class="lnum"> 24: </span> }</pre><pre class="alt"><span class="lnum"> 25: </span>}</pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p align="justify">Pay attention to the fact that this is a protected method and the method reads the value of the query string directly. No parameters are passed. If this was a new project, one of the suggestions would have been to pass the query string value as a parameter making this readily unit testable. </p><br /><p align="justify">The Pex Method that I use to test this is given below</p><br /><div class="csharpcode"><pre class="alt"><span class="lnum"> 1: </span>[PexMethod(MaxBranches = 20000)]</pre><pre><span class="lnum"> 2: </span> <span class="kwrd">public</span> <span class="kwrd">string</span> GetFileName(<span class="kwrd">string</span> communityValue)</pre><pre class="alt"><span class="lnum"> 3: </span> {</pre><pre><span class="lnum"> 4: </span> System.Web.Moles.MHttpContext.CurrentGet = () =></pre><pre class="alt"><span class="lnum"> 5: </span> {</pre><pre><span class="lnum"> 6: </span> <span class="kwrd">return</span> BHttpContext.SetCurrent();</pre><pre class="alt"><span class="lnum"> 7: </span> };</pre><pre><span class="lnum"> 8: </span> </pre><pre class="alt"><span class="lnum"> 9: </span> MolesDelegates.Func<HttpRequest, NameValueCollection> queryStringMole;</pre><pre><span class="lnum"> 10: </span> queryStringMole = (nameVal) =></pre><pre class="alt"><span class="lnum"> 11: </span> {</pre><pre><span class="lnum"> 12: </span> NameValueCollection queryString = <span class="kwrd">new</span> NameValueCollection(1);</pre><pre class="alt"><span class="lnum"> 13: </span> queryString.Add(<span class="str">"fileType"</span>, <span class="str">"1"</span>);</pre><pre><span class="lnum"> 14: </span> <span class="kwrd">return</span> queryString;</pre><pre class="alt"><span class="lnum"> 15: </span> };</pre><pre><span class="lnum"> 16: </span> MHttpRequest.AllInstances.QueryStringGet = queryStringMole;</pre><pre class="alt"><span class="lnum"> 17: </span> var target = UserControlWrapperFactory.Create();</pre><pre><span class="lnum"> 18: </span> <span class="kwrd">string</span> _fileName = target.getFileName();</pre><pre class="alt"><span class="lnum"> 19: </span> <span class="kwrd">return</span> _fileName;</pre><pre><span class="lnum"> 20: </span> }</pre><pre class="alt"><span class="lnum"> 21: </span> }</pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p></p><br /><p align="justify">You will see that the code uses Moles to mock the HttpContext and the query string. We will get to the UserControlWrapperFactory in a minute but the query string itself is a simple implementation. A name value collection is created and then assigned to querystring get. Now, whenever your target code calls querystring[“somekey”] the code block inside MoleDelegates.Func gets executed. Though this example is about mocking query string, the same concept can be used to mock any ASP.NET object. In my tests, I have mocked ApplicationState, HttpRuntime Cache and HttpContext.Items all using similar code.</p><br /><p align="justify">Now to deal with the UserControlWrapperFactory. As I already indicated above, the method in the user control is a protected method. So I am not going to be able to call this from the test method. To get around this, I created a user control wrapper that inherits from the user control I am testing. </p><br /><div class="csharpcode"><pre class="alt"><span class="lnum"> 1: </span><span class="kwrd">public</span> <span class="kwrd">class</span> UserControlWrapper: Controls_MyControl</pre><pre><span class="lnum"> 2: </span> {</pre><pre class="alt"><span class="lnum"> 3: </span> <span class="kwrd">public</span> <span class="kwrd">string</span> getFileName()</pre><pre><span class="lnum"> 4: </span> {</pre><pre class="alt"><span class="lnum"> 5: </span> <span class="kwrd">return</span> <span class="kwrd">base</span>.GetFileName();</pre><pre><span class="lnum"> 6: </span> }</pre><pre class="alt"><span class="lnum"> 7: </span> }</pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /><br /><p align="justify">The factory method just creates an instance of the wrapper and returns it. </p><br /><p align="justify">Based on what I have worked with, I would strongly recommend Pex and Moles if you are looking to increase the code coverage from your unit testing and have hit a road block because you have objects that you are unable to mock. Before you start using it, also be aware that Pex and Moles are going to be replaced by Fakes in Visual Studio 2012 so you might have some code changes when you migrate. If you are looking for tutorials or documentation on Pex, try <a href="http://research.microsoft.com/en-us/projects/pex/">here</a> first (especially Pex in the news and blogs section).</p><br /><p align="justify">Happy Coding!!!</p><br /><p align="justify"> </p><br /><p align="justify"><br /><div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:efb463da-7432-4b46-ac0c-a6ec1963b627" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/Pex" rel="tag">Pex</a>,<a href="http://technorati.com/tags/Moles" rel="tag">Moles</a>,<a href="http://technorati.com/tags/Query+String" rel="tag">Query String</a>,<a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Unit+Testing" rel="tag">Unit Testing</a></div></p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-10738605547311292442012-03-19T17:51:00.001-07:002012-03-19T17:51:19.532-07:00Querying Active Directory from a CRM 2011 Form<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e513e698-9b37-4255-b02b-c4b5e2f079a1" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/CRM+2011" rel="tag">CRM 2011</a>,<a href="http://technorati.com/tags/Active+Directory" rel="tag">Active Directory</a>,<a href="http://technorati.com/tags/Form" rel="tag">Form</a></div> <p align="justify">How do you query Active Directory from a CRM 2011 form? The typical scenario is this. The user enters the username of a domain user. The system has to validate if the username is a valid AD user and respond appropriately.</p> <p align="justify">My first instinct was to write a javascript web resource that connected to Active Directory and did the necessary checks. But on second thoughts, I recollected that Dynamics CRM did something similar when you add a user. I wanted to re-use as much of the code in CRM as possible and decided to check out how it was done in the New User form. </p> <p align="justify"><a href="http://www.microsoft.com/download/en/details.aspx?id=18359" target="_blank">IE Developer toolbar</a> was my best friend for this. I opened up the new user form in CRM 2011 and found the id of the user name field.</p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik47hXILF4FSsZ4PfdQ7zoDtaxr6tquRWyePbgQEJOsH77yPPEaa7SraCCUc4JCc0rrtuJsU_LNHP1nqMGEBBgSJbLZgzr-OCspmv21S7bwYexkQlg4YfOUFWSnN0ODmQhLU9FMV_o7Fo/s1600-h/image4.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-i_o4BlJJA9w/T2fUhH18JmI/AAAAAAAAATs/X4bsvXpW24I/image_thumb2.png?imgmax=800" width="444" height="56"></a></p> <p align="justify">The next step was to search in the scripts what function was firing on domain name change. </p> <p align="justify"><a href="http://lh5.ggpht.com/-4mH8HfL2mUA/T2fUharoyPI/AAAAAAAAAT0/QClzpW5MyJw/s1600-h/image9.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-UfwDGLy5EyE/T2fUhmDXwbI/AAAAAAAAAT8/FVUnLwIIM10/image_thumb5.png?imgmax=800" width="442" height="50"></a></p> <p align="justify">After due validations, the function was instantiating a RemoteCommand object and making a call using that object to get the user properties.</p> <p align="justify">The remote command is an object in the Global.ashx handler and luckily, this handler is loaded on every CRM form. Once I had made that assertion, it was a matter of writing the javascript to pass in the value from the field in my form and doing the same checks as the CRM script. </p> <p align="justify">My code looks something like this</p> <div class="csharpcode"> <div class="csharpcode"><pre class="alt"><span class="lnum"> 1: </span> <span class="kwrd">var</span> oCommand=<span class="kwrd">new</span> RemoteCommand(<span class="str">"UserManager"</span>,<span class="str">"RetrieveADUserProperties"</span>);</pre><pre><span class="lnum"> 2: </span> <span class="kwrd">if</span>(oCommand!=<span class="kwrd">null</span>)</pre><pre class="alt"><span class="lnum"> 3: </span> {</pre><pre><span class="lnum"> 4: </span> oCommand.SetParameter(<span class="str">"domainAccountName"</span>,userName);</pre><pre class="alt"><span class="lnum"> 5: </span> <span class="kwrd">var</span> oResult=oCommand.Execute();</pre><pre><span class="lnum"> 6: </span> <span class="kwrd">if</span>(oResult.Success&&!IsNull(oResult.ReturnValue)&&oResult.ReturnValue.length>0)</pre><pre class="alt"><span class="lnum"> 7: </span> {</pre><pre><span class="lnum"> 8: </span> <span class="kwrd">var</span> firstName = <span class="str">""</span>;</pre><pre class="alt"><span class="lnum"> 9: </span> <span class="kwrd">var</span> lastName =<span class="str">""</span>; </pre><pre><span class="lnum"> 10: </span> <span class="kwrd">for</span>(<span class="kwrd">var</span> oUserXmlDoc=loadXmlDocument(oResult.ReturnValue),oNodeList=oUserXmlDoc.documentElement.childNodes,i=0;i<oNodeList.length;i++)</pre><pre class="alt"><span class="lnum"> 11: </span> {</pre><pre><span class="lnum"> 12: </span> <span class="kwrd">var</span> oNode=oNodeList.item(i);</pre><pre class="alt"><span class="lnum"> 13: </span> <span class="kwrd">if</span> (oNode.tagName == <span class="str">"firstname"</span>)</pre><pre><span class="lnum"> 14: </span> {</pre><pre class="alt"><span class="lnum"> 15: </span> firstName = oNode.text;</pre><pre><span class="lnum"> 16: </span> }</pre><pre class="alt"><span class="lnum"> 17: </span> <span class="kwrd">else</span> <span class="kwrd">if</span>(oNode.tagName == <span class="str">"lastname"</span>)</pre><pre><span class="lnum"> 18: </span> {</pre><pre class="alt"><span class="lnum"> 19: </span> lastName = oNode.text;</pre><pre><span class="lnum"> 20: </span> }</pre><pre class="alt"><span class="lnum"> 21: </span> }</pre><pre><span class="lnum"> 22: </span> }</pre><pre class="alt"><span class="lnum"> 23: </span> } </pre></div><br /><style type="text/css">.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }<br /></style><br /></div><br /><p align="justify">I use the RemoteCommand I discussed earlier and pass the username in question as a parameter. The return from the function is an XML with the properties of the AD User. The name of the property is the tag of the node. So I check for the properties I want and get the text of the node. As simple as that!!</p><br /><p align="justify">Why do I like this approach? Because as a developer, I don’t have to be worried about which AD instance to connect and the permissions to connect to the AD from my code. CRM does most of the heavy lifting for me. I don’t have to worry about security or exposing my AD credentials.</p><br /><p align="justify">This approach is not without its own pitfall. This approach doesn’t work if you want to invoke this from within a HTML web resource. Since a HTML web resource is always contained with an IFrame calling the CRM methods becomes a challenge. </p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com8tag:blogger.com,1999:blog-3614688564147648057.post-86815107096466450732012-03-09T16:05:00.001-08:002012-03-09T16:05:50.343-08:00Windows 8–Day 1<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1a699259-c733-4624-a47d-8d565e4cfd1a" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati Tags: <a href="http://technorati.com/tags/Windows+8" rel="tag">Windows 8</a>,<a href="http://technorati.com/tags/Metro" rel="tag">Metro</a>,<a href="http://technorati.com/tags/shortcuts" rel="tag">shortcuts</a>,<a href="http://technorati.com/tags/commands." rel="tag">commands.</a></div> <p align="justify">It is probably no news that Windows 8 Consumer Preview is out.I have been equally excited to try this out and I thought it wouldn’t be a bad idea to pen down my thoughts on my experience with Windows 8 as I go along. </p> <p align="justify">If you have just downloaded the bits and are scratching your head on how some of these things work, do check out this <a href="http://windowsteamblog.com/windows/b/windowsexperience/archive/2012/03/08/getting-around-in-windows-8.aspx">blog</a>. It has some useful tips on how to get around Windows 8. I will also share tips & tricks that I have found useful at the end of this post.</p> <p align="justify">The Windows 8 installation by itself was a breeze. I have it on a machine with 2 GB RAM with a 64 bit AMD processor. I also love the fact that I can switch between the desktop view and the metro view. There are two distinct purposes of these views, the way I see it. The desktop is the productive side of Windows 8 and the metro is the fun side. It might be a bit of a hard landing if you are trying to be completely productive with the metro i.e Don’t try and do everything you do on a PC from the metro. </p> <p align="justify">The next thing I did was to configure my email accounts using the Metro Mail app. Below is a snapshot of how the mail app looks. The list of emails is on the left and when you select an email it displays the body on the right and also enables action you can take on the top right. </p> <p align="justify"><a href="http://lh5.ggpht.com/-aC0Ko3AYIpM/T1qazsnUhKI/AAAAAAAAASY/HsSr7-FOzP4/s1600-h/MailView%25255B4%25255D.png"><img title="MailView" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="MailView" src="http://lh4.ggpht.com/-Io_gQbN3b_0/T1qa0crcfGI/AAAAAAAAASg/Z9mvZ19eAdE/MailView_thumb%25255B2%25255D.png?imgmax=800" width="417" height="396"></a></p> <p align="justify">You can switch between accounts by right click or from the app bar. Have Skype, Live writer and Lync. and not had any issues so far. There is also a messaging app in metro which I use with my live id. It enables me to chat with my facebook friends too. It displays all the conversations you have with your contacts as individual threads so no more switching tabs. You can also read your facebook updates from the same app!!!</p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq5DcC0wJs08oU-217-8abiO8CuYBoF97dnpdQXJkWOuaRmcC44CPckDy9-_vbfjqqFEpFtBZBV6_SK9RuMBex0uYExwTog9e7-gq7MVfYpShWqEnIHDMoIcesBOJ0XkoaYwmpl4qIO-I/s1600-h/Messaging%25255B3%25255D.png"><img title="Messaging" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="Messaging" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiX1QsOJrf0KlDma7kubhVOSJ_KPTgxRvPp-Uj4P9y3_gp-jPgqLba-Pg7wYy9rbFknwr9kBGFdtEkgMHuvSLpGvWA08jpQZNmsFUAvmV1IpFcL-pTmonoEBA5ZH5RKOHpw0W01pry8SdY/?imgmax=800" width="355" height="285"></a></p> <p align="justify">I managed to install VS 2011 and though I haven’t got around to extensively using it , the installation itself did not have any issues. I am hoping I will get to use it more in the coming days. Now to keep up my promise of sharing some tips that I have come across</p> <ul> <li> <div align="justify">If you want to close an app try using Alt + F4. Personally, I have found that to be the easiest way to close an app.</div></li> <li> <div align="justify">If you want to look at all the windows or apps you have open, hover your mouse on the top left. That will bring up a task bar like the one shown in the snapshot. You can close the window from here too</div></li></ul> <blockquote> <p><a href="http://lh3.ggpht.com/--cj8w_eoTMw/T1qa1jbaEgI/AAAAAAAAAS4/ehW6bXdV52I/s1600-h/AppsList%25255B3%25255D.png"><img title="AppsList" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="AppsList" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9zP3pk615BXWhWL7ZcUMLZdeljq-40hycdofQMK6ueUlYMKZjzR3opnt7EATf6Zw16un2wpspI3Ww4oWD_nlF8zbqS5iwpKWDKMNXkiMdPO2BR_jH5Ii3J2X3Tjg-xzOhW2EIJ-NIsCo/?imgmax=800" width="340" height="273"></a></p></blockquote> <ul> <li> <div align="justify">Don’t go looking for the run command. The whole desktop now functions as the run command. Switch to the metro view and just type. That serves as search and the run command</div></li> <li> <div align="justify">You don’t need to go into control panel to uninstall an app. Use Windows + Z and click on all apps. Right click and app and you will have the option to uninstall in the app bar.</div></li></ul> <blockquote> <p align="justify"><a href="http://lh5.ggpht.com/-vBw3PFeCR-E/T1qa2QJJOWI/AAAAAAAAATI/Bd8APxHCXLo/s1600-h/UnInstall%25255B3%25255D.png"><img title="UnInstall" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="UnInstall" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaVthuuEEF2WK1ohEwstUKLz97w-kEJAIPhyphenhyphenxmO7L12EoNLHs15Ha2vMPph01Ab-jk36iBDuB4plhdPru7Bo7ehBw4Cn-QwS2l4MLIWi2M5Yxp37UVH_xvOHr3UY-U8X-KKpnOQNpQ2Y4/?imgmax=800" width="349" height="106"></a></p> <p align="justify">What do you think about Windows 8? I would love to know what your thoughts are. Leave a comment on what you like or don’t like. Happy metroing <img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Smile" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNhjdDxmEGhQR34im3AhOB_r5zgKrlgukTPqj7d7hh6-RNOIfcBNTkFK9Mh-N2T_mOMe4V5jHj0RKGHH61QQsIK3WR2DzuPck4tcrWijEi9FVYsLYabn49w4MBNvigf0_03jCjInqJd94/?imgmax=800"></p></blockquote> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-26875873353953514652011-06-13T22:51:00.001-07:002011-06-13T22:51:31.416-07:00Designing with Umbraco : XSLT vs User Controls<p> </p> <p align="justify">in Jan, I wrote my first post on Umbraco and how to build a simple Hello World page using Umbraco.  Having spent the last few months working on Umbraco, I have to say, my liking for Umbraco has only grown. It is a fairly simple CMS and unlike Sharepoint, does not complicate the whole authoring experience. You could always argue that Sharepoint is comprehensive but that’s a discussion we will save for another post. </p> <p align="justify">Back to what you can do with Umbraco, there are really two major options as an Umbraco developer to build UI; XSLT and User Controls. A lot of times as a developer, the decision you have to make is which way to go. As with a lot of other design discussions, the perfect answer is “It Depends”. But during the course of this post, I will try and unravel that generic answer to some realistic scenarios. I am going to try and layout the factors that I usually consider when deciding one way or the other. Keep in mind that this post is written targeting the web form model of Umbraco and  come razor, some of these could change.</p> <p align="justify"><strong><u>XSLT</u></strong></p> <p align="justify">XSLT is the in-thing when it comes to content managed sites. It gives you the option of giving flexibility to the content author to change the way the page is displayed making it truly content managed.</p> <p align="justify">With umbraco, you get the option of using XSLT to do almost everything you want. I have typically used the following constructs to decide if XSLT is the best fit</p> <p align="justify"><strong>Fluctuating UI Changes</strong> -  You think something is going to change often. A typical scenario is where you have HTML tags emitted within the macro. Though there are ways and means of getting the styling changed with css, there are times when you have no choice but to emit HTML. XSLT is a natural choice in such scenarios</p> <p align="justify"><strong>Navigate Content Tree</strong>- If you have to work with the Umbraco content tree. Since the Umbraco content tree is XML, XSLT becomes a natural choice if you have to navigate through the content tree. </p> <p align="justify"><strong>Compliance to Standards – </strong>There are a lot of content managed standards that are prevalent. Standards like DITA are becoming more of a requirement nowadays when authoring sites. A lot of these CMS standards depend on XML and XSLT.</p> <p align="justify"><strong>When in Doubt – </strong>Yes. We all come across instances when we are not sure whether to use XSLT or user controls. In such cases, my recommendation will be to start off with XSLT unless you hit an implementation roadblock that forces you to change to User Controls.</p> <p><strong><u>User Controls</u></strong></p> <p align="justify">User controls are the most natural choice for most .NET developers because they relate to it better due to their familiarity with .NET. But with XSLT being so powerful, the use of user controls for me, is limited to the following scenarios</p> <p align="justify"><strong>Code Abstraction – </strong>When you do not want site managers or authors to see the code. A valid argument is that the permissions to viewing XSLT can be restricted as well through the User Permissions but bear in mind that XSLT is file based in Umbraco so users who have permissions to access the XSLT folder can view, copy or do anything with the code. If you do not want users to see what your code is doing, stick to the user control model.</p> <p align="justify"><strong>Interaction with external web services</strong> – This is one of the scenarios for which I still use User control. Theoretically, it is still possible to write an XSLT extension that talks to the web service and use that within XSLT but I think it is beneficial only in scenarios where you are using the strong points of XSLT. One more interesting approach when using webservices is to use javascript and jquery templates. You could completely eliminate a macro in this scenario by having the webservice return JSON. </p> <p align="justify"><strong>Parallelism – </strong>If you want to write muti-threaded code or use PLINQ then you do that only with user controls; so they become a forced choice</p> <p align="justify"><strong>Use ASP.NET Features -</strong>  Though this looks like a no-brainer, the caveat here is that you can still access some of the features from XSLT. The umbraco.library does have methods to get the query string or the form variables. But if you want to check if a page is posted back or not, you cannot do that in XSLT without doing some hacks.</p> <p align="justify"><strong>Unit Testing Requirements – </strong>If your project requires to have high code coverage then move away from XSLT.Though there are XSLT Unit Testing suites, they are still nascent and wont really improve your code coverage results.</p> <p align="justify">Though the above list might not be exhaustive, I have tried to cover scenarios in my experience with Umbraco so far. I will be interested in knowing if there are any scenarios that you have come across that isn’t covered in this post and if you agree\disagree to the observations made in this post.</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com1tag:blogger.com,1999:blog-3614688564147648057.post-36815732920075106982010-11-16T19:15:00.001-08:002010-11-17T07:02:56.172-08:00My first page with Umbraco<p> </p> <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:78b2d991-73da-4bcd-ba6c-f4c5a35b8632" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/Umbraco" rel="tag">Umbraco</a>,<a href="http://technorati.com/tags/CMS" rel="tag">CMS</a>,<a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a></div> <p align="justify">After a small Diwali break, I am back to my blogging. The focus of this post is going to be a CMS from MS called Umbraco. I came to know about Umbraco from one of my friends. Having been involved in developing and designing a few content managed websites, I decided to take a deeper look into Umbraco. It is just a week since I started playing around with it but I thought I will get started off about creating a first page with Umbraco as I delve into other advanced aspects and figure out how to get it done.</p> <p align="justify">For the unfamiliars, <a href="http://umbraco.org/" target="_blank">Umbraco</a> is an ASP.NET based CMS. You can install Umbraco by using the web platform installer. The installation is fairly easy and self-explanatory. After you have installed it, launching Umbraco will give you the admin log in page. Use the credentials you used during set up to log in. Your home page will look like below</p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQVrQHV0LqANBln-G1iibdKI7kGqnjB2WUCdNdOJ6g4zk6XEFOkaOZ7XEfQjc49Equi3uZsuM-qVJ8CDN48JtBuF83-wk0imwXLdvmgrQp4PA0r6hCXnsaSZofi0uMaBEsUOejjImRYAE/s1600-h/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_2cIm7h7Xp2w/TONIpaT2ZyI/AAAAAAAAAOk/SlorSb5o838/image_thumb1.png?imgmax=800" width="494" height="286" /></a> </p> <p align="justify">Settings is where to start off creating the first page. Clicking  on Settings will reveal the tree view that shows the building blocks that we will use to build our site. Let us focus on Templates and Document Types for a second. These are the two most important artifacts we will use to come up with a content managed page. For those of you familiar with Sharepoint, Templates are like Master Pages and Document Types are like Page Layouts. </p> <p align="justify">The first step in creating a site is to get the master page and the style sheets done. Since Umbraco is a folder based CMS, you should be able to copy your existing Stylesheets to the stylesheet folder under the Umbraco virtual directory and you are good to go. Though it does work for the master pages to some extent, I had a bit of an issue getting Umbraco to recognise the master pages I had copied. So, I create the master page through the Umbraco interface. You have to right click on <strong>Templates</strong> and then Click on <strong>Create</strong>. Once you provide the name and confirm the creation, you will have the ability to add HTML content to the master page. The interface will look as shown below</p> <p align="justify"> </p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvUKWSrZyY9KKcysL5UAvXi5Cz36-ibeMDQ2dF_UfdApHAvTdITbdYdqG1fCw4KPUW5Zpr_jv8wSp1wY8nJ2WiXoFgBz6FDHePuKeC-mWYg6UxXv3QZPMVtHuGscLFQcjyYutIpsNUCb0/s1600-h/image%5B4%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYDFyOESReYTvrKg60OC7VQYZA7bV1TpBI5-6vgGRPx2CKC5XdjUoDTjN4znxiq6nIPVHYc00MNjFLuBaFuLPuSjOLvNWveVQQy9aAJFlDCE-9IKLncFeODOsnsldtHkRVyGLswLllZ7E/?imgmax=800" width="517" height="299" /></a></p> <p align="justify">Umbraco, by itself creates a nested master page. You just have to put in your content. Since it is a nested master page, be sure you put the content inside your content place holder (in my case called “ContentPlaceHolderDefault”). You will also see that you have the option of using one of your own master pages as the parent master page.</p> <p align="justify">Once the master page is done, the next step is to go ahead and create a document type that we will use to put in content. Its quite easy to create a document type. You just right click and say Create. </p> <p align="justify"> </p> <p align="justify"><a href="http://lh6.ggpht.com/_2cIm7h7Xp2w/TONIsLBKq2I/AAAAAAAAAOw/JQjmaAAOe4k/s1600-h/image%5B9%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4hZ5eHW0nH8ZfRBsgRb_gXekSHAPzU8-uDGkhIGQIrE0YotCUwbqXjojRNHO1Ww4St9Y2ICdzrNBu7ptd2PCymNALK60Z2OZ2_ilR6DfGJrnY9fa2MI7Dw3jgH7hdh0ZJmiYbvhrAuBk/?imgmax=800" width="398" height="231" /></a>  </p> <p align="justify">Give a name for the Document Type and select the Template that this document type will use. You can change this later too so don’t worry too much about selecting the wrong master page. </p> <p align="justify">The document type is where most of the work happens to make a page content managed. There are four tabs for every document type you create.  The first tab is the Info tab where all the details of the document type are displayed. You can provide an alias and associate the document type with an image. You can also set what Master Pages this document type can use and what is the default Master Page for this document type. Below is an example of the HomePage document type I created.</p> <p align="justify"> </p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGASRLOC1JyqpORpXYZxuOpUbNeNJPJslz7x-zO3XD9-gYk0stsNQtsGiNNUjOxjEf7dFQbZ14praYypgRxCmD-9vXig_IDPRuoJuCg7D9oOiPTCSDEoJjYHU3gB5od5c_SvlBlFr8BhU/s1600-h/image%5B14%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPQStu-m_WsgkKU_5g0eDsDj37dDoWqxC-3pFU3c0i_XTM6upUBnDLwbP1xWziQLMIcwchlk42JKgIwNdjm4HOQGfvfCWyGG9EK8sN2ODILAPipMxupMYSpbkAmrhkJaGqh7BklwYKwxM/?imgmax=800" width="507" height="294" /></a> </p> <p align="justify">Next, let us examine the Tabs tab. In this tab, you can create tabs that will be displayed when this document type is used to create content. To show how it works, I have created a tab called Content that will hold the rich text box for the content author to put in the content. </p> <p align="justify">The next step is to let the document type know that it has to display a rich text box in the Content tab. You do this by creating a property of type Rich text box in the Generic Properties tab under Content. </p> <p align="justify">As you can see in the screenshot, I have created a “HelloWorldContent” property of type rich text box. You can also create properties of various other types. These types are essentially controls. Do note that the alias is like the HTML id of the control. You can also set if the content in this is mandatory and what regular expressions can be used to validate the content but we will save that for later. Now that the document type is created and we have created a rich text control as a part of the document type, we have to let the master page know where to display the content of the rich text box. </p> <p align="justify"><a href="http://lh4.ggpht.com/_2cIm7h7Xp2w/TONIu6pNw6I/AAAAAAAAAPA/jMJRrHMdh6Y/s1600-h/image%5B19%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_2cIm7h7Xp2w/TONIv8ZRdVI/AAAAAAAAAPE/EDFGmjo2X8U/image_thumb%5B10%5D.png?imgmax=800" width="516" height="299" /></a> </p> <p align="justify">This can be done by inserting a page filed in the master page. Go back to the Master Page and click on Insert Page field in the tool bar above the master page. The below window will open up. If you have created the property right, then you should see it in the Choose Field dropdown. Select the field and leave the other fields to default. Save the master page.</p> <p align="justify"><a href="http://lh5.ggpht.com/_2cIm7h7Xp2w/TONIweBMUsI/AAAAAAAAAPI/9hNKpTUtskA/s1600-h/image%5B24%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_2cIm7h7Xp2w/TONIxbz2guI/AAAAAAAAAPM/G2nsrsNFQGE/image_thumb%5B13%5D.png?imgmax=800" width="452" height="262" /></a> </p> <p align="justify">We are done with the administration part. The final step in creating a page is the content authoring page. All content authoring in Umbraco is done in the Content screen. Click on Content in the Sections area. Right Click on the top level Content and click on Create. This pulls up a window with a text box for naming your page and a dropdown with the list of created Document Types. Choose the document type you just created and give the page a name. In my case, I choose to call it HomePage. Post creation, you will see two tabs Content and Properties as seen below. </p> <p align="justify"><a href="http://lh6.ggpht.com/_2cIm7h7Xp2w/TOPumZkhDTI/AAAAAAAAAPY/qv1NVxO1Fow/s1600-h/image%5B4%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh78LNUwU-xEp3l6Hj_0Rtof4u1C6iVu0748cbfIRICWPbJSja5Lj3r-etQ_bmzcRV99pRe64w_ZTWwV-NwMqCayoaxDoZRSKfb30Yg_qZJjkYNx4PprosMB2hDQtPY4O2ZwhlSYyrr6bw/?imgmax=800" width="686" height="396" /></a> </p> <p align="justify">The order in which they are displayed depends on the Sort Order I have set in the Tabs tab of the document type. The content author can put in any content in the rich text box and publish the page by clicking the Save and Publish icon. As soon as you publish, Umbraco generates a URL which you can view in the Properties tab. The page that I have created is shown below</p> <p align="justify"><a href="http://lh6.ggpht.com/_2cIm7h7Xp2w/TONIyvnwTyI/AAAAAAAAAPQ/xStUreKrpYw/s1600-h/image%5B28%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_2cIm7h7Xp2w/TONIzYperdI/AAAAAAAAAPU/DH1HK13LAkM/image_thumb%5B15%5D.png?imgmax=800" width="507" height="293" /></a> </p> <p align="justify">Phew!!! That was quite a long post. I am sure this is the first of many to come from me on Umbraco. If you want to to get adventurous with Umbraco too, take a look at their site. For starters I found this <a href="http://www.blogfodder.co.uk/2010/5/13/building-your-first-umbraco-site-from-scratch" target="_blank">site</a> too very useful. Please feel free to leave your comments on this post and as always, Happy Coding!!!</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-3330348180171719622010-09-22T01:00:00.001-07:002010-09-22T01:02:34.526-07:00HTML5.Decode<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:0fabc7a1-2550-4beb-992b-c61bc951823c" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/HTML5" rel="tag">HTML5</a>,<a href="http://technorati.com/tags/HTML" rel="tag">HTML</a>,<a href="http://technorati.com/tags/HTML+4+vs+HTML+5" rel="tag">HTML 4 vs HTML 5</a></div> <p align="justify">Most of us would have heard that HTML 5 is in the works. It can be expected that almost all new browsers (some of the browsers already do) will now start supporting HTML5. What does it mean for developers and architects? This post will take a look at HTML 5 and some of the new elements in HTML 5. Detailed insights into the working of individual elements is out of scope for this particular post but I have provided links to references wherever necessary</p> <p align="justify">1. Standards: One of the biggest changes in HTML 5 is that it is no longer based upon SGML. Though this does not impact developers or the architect community directly, it is good to know this because the HTML document no longer needs a DTD to render. So the !DOCTYPE element is now redundant. However, this element now performs a different function. It actually tells the browser if the document has to comply to standards or not. You can do this by setting the DOCTYPE to either of the three allowed modes; quirks mode, limited quirks mode and no quirks mode.</p> <p align="justify">2. New Elements: HTML 5 has introduced a lot of new HTML elements. The new elements fall into one (sometimes multiple) of the below categories</p> <p align="justify"> </p> <ul> <li> <div align="justify">Sectioning – Sectioning can internally be divided into root and content. Root is a type of element that can have children under it. One of the new sectioning root element in HTML 5 is <a href="http://dev.w3.org/html5/html-author/#the-figure-element" target="_blank"><figure></a><strong>.  </strong><a href="http://www.w3.org/TR/html5/interactive-elements.html#the-details-element" target="_blank"><detail></a> is another sectioning root element that is new on the block. The <detail> element lets you hide details unless the user wants to view them explicitly. For example, you are showing only the name of the user and you want to show the details only if the user clicks on a button or a link. The sectioning content category contains new elements like <nav>, <article> and <section>. While <a href="http://www.w3.org/TR/html5/sections.html#the-nav-element" target="_blank"><nav></a> identifies sections intended for navigation (similar to a sitemap), <section> and <article> isolate elements in a HTML Page. <a href="http://www.w3.org/TR/html5/sections.html#the-article-element" target="_blank"><article></a>, for example, is intended for something like a blog entry which can be distributed independently. </div> </li> <li> <div align="justify">Heading – The heading category introduces <a href="http://www.w3.org/TR/html5/sections.html#the-header-element" target="_blank"><header></a> and <a href="http://www.w3.org/TR/html5/sections.html#the-hgroup-element" target="_blank"><hgroup></a> tags. While there were already 6 header tags <h1> to <h6>, you can now wrap them inside the newly introduced <header> and <hgroup> tags. While the <header> tag identifies something as a header section, the <hgroup> tag can be used to categorise the 6 header tags as subsections.</div> </li> <li> <div align="justify">Embedded Content – In HTML 4, we used the object tag to display video or play audio. In HTML 5, we have separate tags to embed audio or video in the page. <a href="http://www.w3.org/TR/html5/video.html#video" target="_blank"><video></a> and <a href="http://www.w3.org/TR/html5/video.html#audio" target="_blank"><audio></a> tags do exactly this. Apart from these tags, there is also the <a href="http://www.w3.org/TR/html5/the-iframe-element.html#the-embed-element" target="_blank"><embed></a> tag that helps you embed any other plug in. One of the more important embedded elements is also the <a href="http://www.w3.org/TR/html5/the-canvas-element.html#the-canvas-element" target="_blank"><canvas></a> that helps you display graphics or dynamic images.</div> </li> <li> <div align="justify">Phrasing Content – These are the HTML elements that can include normal text mark up. <span> is an example of phrasing content. <a href="http://www.w3.org/TR/html5/text-level-semantics.html#the-mark-element" target="_blank"><mark></a> is a new phrasing element in HTML 5. This is like the highlight text is MS Word. Using mark you can highlight text in a document for example, search results. <a href="http://www.w3.org/TR/html5/the-button-element.html#the-progress-element" target="_blank"><progress></a> element helps you show progress bar. There are other Phrasing elements like <a href="http://www.w3.org/TR/html5/text-level-semantics.html#the-time-element" target="_blank"><time></a> which help you show timestamps on your page. There is also the <datalist> element which now helps you create re-usable list options in your page.</div> </li> <li> <div align="justify">Menus and Commands – Though both the <a href="http://www.w3.org/TR/html5/interactive-elements.html#menus" target="_blank"><menu></a> element and <a href="http://www.w3.org/TR/html5/interactive-elements.html#the-command" target="_blank"><command></a> element are phrasing content, I have called them out separately because of the significant differences from the previous version. <menu> element,  which was deprecated in HTML 4 has been reintroduced in HTML 5. The menu element can now be enhanced using the <command> element. The <command> element allows the user to execute commands specified as a part of the element.</div> </li> </ul> <p>3. New Attributes: HTML 5 has also introduced a whole set of new attributes to existing elements. One of the most significant additions is to our favorite <input> tag. The type attribute of the <input> tag now has some new additions. Some of these are search, month, date etc. See <a href="http://www.w3.org/TR/html5/states-of-the-type-attribute.html" target="_blank">here</a> for the complete list of new values.</p> <ul> <li>The <a href="http://www.w3.org/TR/html5/the-map-element.html#the-area-element" target="_blank"><area></a> element now has a host of new attributes. I am listing some of them below</li> <ul> <li> <div align="justify">ping – this attribute contains a list of space separated URLs that are pinged when the URL is clicked. This is useful for content targeting and can be turned on or off by the user.</div> </li> <li> <div align="justify">media – This attribute is similar to the media attribute in the link element. This is again used to specify the type of media a link targets</div> </li> <li> <div align="justify">hreflang and rel – Again, these do the same function as in a link element. </div> </li> <li> <div align="justify">target – this attribute which was deprecated has been re-introduced.</div> </li> </ul> <li> <div align="justify">There is now an autofocus attribute to elements like <input> and <select>. This will declaratively set the focus on the control when the page loads. This is something the user can turn off if he does not want to use.</div> </li> <li> <div align="justify">There is a required attribute to the <input> element which validates mandatory fields.</div> </li> <li> <div align="justify">A new form attribute for text element inputs help the developer specify which form the specific element belongs to. This way, it is not necessary that all the elements lie within a form. They can be placed outside the form but still be a part of it in the HTML DOM.</div> </li> <li> <div align="justify">You can now disable validation of a form by using the novalidate attribute in the form element.</div> </li> <li> <div align="justify">The individual input elements now have attributes like formaction, formtarget which overrides the value set in the form tag. This way, a single page can submit to multiple targets declaratively</div> </li> <li> <div align="justify">You can now write scoped style sheets by using  the scoped attribute of the style element. for example, if you want the style to be applied only to a specific scope like one flow section, you can mention that by setting the scoped attribute to true.</div> </li> <li> <div align="justify">Developers also have more control over when a particular script is being executed by using the async and the defer attributes in the script element. </div> </li> <li> <div align="justify">The iframe element has a set of new attributes that controls how the content within the iframe behave. Using the <a href="http://www.w3.org/TR/html5/the-iframe-element.html#attr-iframe-sandbox" target="_blank">sandbox</a> attribute, for example, you can now restrict users from clicking links or executing active content within an iframe. There is now also a <a href="http://www.w3.org/TR/html5/the-iframe-element.html#attr-iframe-srcdoc" target="_blank">srcdoc</a> attribute using which you can specify the source of an iframe to be a document with a valid DOCTYPE</div> </li> <li> <div align="justify">Some HTML attributes have now been upgraded to <a href="http://www.w3.org/TR/html5/elements.html#global-attributes" target="_blank">global attributes</a> which means they will now be available across all the elements. Examples of such attributes are class, id, lang, dir etc. There are also new global attributes and event handlers in the list. </div> </li> <li> <div align="justify">The draggable attribute is more powerful and can be used in conjunction with the new drag & drop APIs in HTML 5</div> </li> </ul> <p align="justify">4. Absent Elements: Some of the defunct elements are frame, frameset, acronym, applet, font</p> <p align="justify">I will conclude this particular post here. Please be advised that HTML 5 is still being developed and some of the above may change. You can always find the latest reference to HTML 5 specifications <a href="http://www.w3.org/TR/html5/" target="_blank">here</a>. One of the things that has not been covered as part of this post are the HTML APIs and the changes in these APIs. Hopefully I will come out with another post that concentrates on the HTML 5 APIs. Till then ciao and happy coding!!!</p> <div align="justify"></div> <ul><strong></strong></ul> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-85863392727475346582010-08-30T10:20:00.001-07:002010-08-30T10:25:44.966-07:00Enterprise Library 5.0 – Configuration Services Part - III<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:eb731a0b-cd54-49ad-8fe6-8d6a20473148" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/enterprise+Library+5.0" rel="tag">enterprise Library 5.0</a>,<a href="http://technorati.com/tags/parentSource" rel="tag">parentSource</a></div> <p align="justify">The previous two posts on Configuration Services showed <a href="http://vaideeswaranr.blogspot.com/2010/08/enterprise-library-50-configuration.html">how we can use an external configuration file</a> and how <a href="http://vaideeswaranr.blogspot.com/2010/08/enterprise-library-configuration.html">we can use a specific section from an external config source</a>. </p> <p align="justify">In the final post of the Configuration Sources series, we will look at what I personally think is one of the most useful features of Configuration Services. We will see how we can have the application configuration file inherit from another configuration file and override or add only sections that are application specific. I will give you a scenario where this can be very useful.</p> <p align="justify">In one of my recent projects we were developing a number of websites. Except for some application specific key value pairs a lot of the other environmental configurations were the same across most of these websites. With this feature, it will be very easy to isolate the environmental specific values in a separate configuration file, inherit the same and add key value pairs to store application specific values.</p> <p align="justify">To show how this works, I have a shared config file that looks like the below. </p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">section</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="loggingConfiguration"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#ff0000;">requirePermission</span><span style="color:#0000ff;">="true"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">connectionStrings</span> <span style="color:#ff0000;">configSource</span><span style="color:#0000ff;">=""</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">connectionStrings</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">loggingConfiguration</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">=""</span> <span style="color:#ff0000;">tracingEnabled</span><span style="color:#0000ff;">="true"</span> <span style="color:#ff0000;">defaultCategory</span><span style="color:#0000ff;">="General"</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">listeners</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="EntLIbTestListener"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> <span style="color:#ff0000;">listenerDataType</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#ff0000;">source</span><span style="color:#0000ff;">="Enterprise Library Logging"</span> <span style="color:#ff0000;">formatter</span><span style="color:#0000ff;">="Text Formatter"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> <span style="color:#ff0000;">log</span><span style="color:#0000ff;">=""</span> <span style="color:#ff0000;">machineName</span><span style="color:#0000ff;">="."</span> <span style="color:#ff0000;">traceOutputOptions</span><span style="color:#0000ff;">="None"</span> <span style="color:#0000ff;">/></span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">listeners</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">formatters</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> <span style="color:#ff0000;">template</span><span style="color:#0000ff;">="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Text Formatter"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">formatters</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">categorySources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">switchValue</span><span style="color:#0000ff;">="All"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="General"</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">listeners</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="EntLIbTestListener"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">listeners</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">add</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum26" style="color:#606060;"> 26:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">categorySources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum27" style="color:#606060;"> 27:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">specialSources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum28" style="color:#606060;"> 28:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">allEvents</span> <span style="color:#ff0000;">switchValue</span><span style="color:#0000ff;">="All"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="All Events"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum29" style="color:#606060;"> 29:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">notProcessed</span> <span style="color:#ff0000;">switchValue</span><span style="color:#0000ff;">="All"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Unprocessed Category"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum30" style="color:#606060;"> 30:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">errors</span> <span style="color:#ff0000;">switchValue</span><span style="color:#0000ff;">="All"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Logging Errors &amp; Warnings"</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum31" style="color:#606060;"> 31:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">listeners</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum32" style="color:#606060;"> 32:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="EntLIbTestListener"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum33" style="color:#606060;"> 33:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">listeners</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum34" style="color:#606060;"> 34:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">errors</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum35" style="color:#606060;"> 35:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">specialSources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum36" style="color:#606060;"> 36:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">loggingConfiguration</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum37" style="color:#606060;"> 37:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre></div><br /></div><p align="justify">This is the same configuration as I have used in Part I and II with the logging settings. To use this as my parent configuration, I have to set a couple of properties. I have to set the Parent Source of my config file to the common config file I have created above. The settings are shown in the screenshot below. I have also changed the configuration source to be System Configuration which means my local app.config is being used.</p><p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdmmIEayVlncHfDIZigFWfST3wb6WYCQrNFiWlArQnOr6mD80vfmydTiYmsh3Mt2CMzGAFUtwFDMM3QJJNFjY86X-lIiXwngRKdv9a0XWENJ2LI7yXoOpTL2xQF5xqb47U-VA0xKQX_kQ/s1600-h/Parentconfig%5B4%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Parentconfig" border="0" alt="Parentconfig" src="http://lh6.ggpht.com/_2cIm7h7Xp2w/THvoO7UnKsI/AAAAAAAAAOA/ZmSksMtfgaI/Parentconfig_thumb%5B2%5D.jpg?imgmax=800" width="356" height="231" /></a> </p><p align="justify">Notice how this configuration file does not have any logging settings. After these changes, my config file now looks like</p><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;"><?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span> <span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">="utf-8"</span> ?<span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">section</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="enterpriseLibrary.ConfigurationSource"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#ff0000;">requirePermission</span><span style="color:#0000ff;">="true"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">enterpriseLibrary.ConfigurationSource</span> selectedSource="System Configuration Source"</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> parentSource="Shared Config"<span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">sources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="System Configuration Source"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Shared Config"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> <span style="color:#ff0000;">filePath</span><span style="color:#0000ff;">="C:\Users\r.vaideeswaran\Documents\Visual Studio 2010\Projects\EntLibTest\EntLibTest\CommonConfig.config"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">sources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">enterpriseLibrary.ConfigurationSource</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre></div><br /></div><br />Notice how there are now two sources added; one is the system configuration source and the other points to the shared configuration file.<br /><br /><p align="justify">As a first step, let me now test the logging code I wrote in the previous two parts. For re-cap, here is the code</p><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">namespace</span> EntLibTest1</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">class</span> Program</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> LogWriter textWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> LogEntry testEntry = <span style="color:#0000ff;">new</span> LogEntry();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> testEntry.Message = <span style="color:#006080;">"Logging the loading of EntLibTest1"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> testEntry.Priority = 2;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> testEntry.Categories.Add(<span style="color:#006080;">"General"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> textWriter.Write(testEntry);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> Console.WriteLine(<span style="color:#006080;">"Message Logged"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> Console.ReadLine();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> }</pre></div></div><span class="Apple-style-span" style="line-height: 16px;"><br /><span class="Apple-style-span" style="line-height: normal; ">This does indeed produce a “Message Logged” output on the console though there are no logging sections directly in my config. This tells me that my parent source is working. The next step is now to add a specific section and see if that works.</span><br /></span><br />I add a new key value pair in the appSettings section through enterprise library. The screenshot for the same is as below.<br /><br /><p align="justify"><a href="http://lh4.ggpht.com/_2cIm7h7Xp2w/THvoPgA-gmI/AAAAAAAAAOE/HJ2jHXMgq30/s1600-h/image%5B4%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF0PQjVAQLN9SmMiVW9ZkpkzpqAQrVIQbG8zd9rvfGNTCqabhB0fe3BrKspME69OClYMuf9pfhjM8jd9VswMaFuHLFOmgfHcmYWwPc_pnMKotAtc1KCLYVMuGE19D9RFgIu0uGJs-dzyg/?imgmax=800" width="430" height="294" /></a> </p><br />This is our good old appSettings key value pair. Unsurprisingly, the config is now changed to reflect the above settings.<br /><br /><span class="Apple-style-span" style="line-height: 16px;"><span class="Apple-style-span" style="line-height: normal;"><br /></span></span><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;"><?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span> <span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">="utf-8"</span> ?<span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">section</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="enterpriseLibrary.ConfigurationSource"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#ff0000;">requirePermission</span><span style="color:#0000ff;">="true"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">appSettings</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">key</span><span style="color:#0000ff;">="ConnectionString"</span> <span style="color:#ff0000;">value</span><span style="color:#0000ff;">="TestConnectionString"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">appSettings</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">enterpriseLibrary.ConfigurationSource</span> selectedSource="System Configuration Source"</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> parentSource="Shared Config"<span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">sources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="System Configuration Source"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Shared Config"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> <span style="color:#ff0000;">filePath</span><span style="color:#0000ff;">="C:\Users\r.vaideeswaran\Documents\Visual Studio 2010\Projects\EntLibTest\EntLibTest\CommonConfig.config"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">sources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">enterpriseLibrary.ConfigurationSource</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre></div><br /></div><br />To make sure my app can retrieve the appSettings value, I add the following three lines of codev(Do not forget the usings at the top)<br /><br /><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> AppSettingsReader connReader = <span style="color:#0000ff;">new</span> AppSettingsReader();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> var connString = connReader.GetValue(<span style="color:#006080;">"ConnectionString"</span>, <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">string</span>));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> Console.WriteLine(connString.ToString());</pre><br /><!--CRLF--></div><br /></div><br />Nothing fancy. Now when I run the application, you will see that the application picks up both the logging configuration and the appSettings key. This happens because at run time the Enterprise library merges the two configuration files so that they act as one. The above example did not specifically override a section but if I were to override, the value in the local configuration will take precedence over the shared configuration.<br /><br />That concludes my three part starter posts on Configuration Services. The Configuration Services is no where near perfect yet but as with anything else, I am sure this will evolve over multiple releases. Happy Coding!!!Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-59892537138158271262010-08-19T11:09:00.001-07:002010-08-19T11:13:02.066-07:00Enterprise Library 5.0 – Configuration Services Part -II<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:32dccb5e-1e21-4b6b-ab8c-24b176d86d8a" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/Enterprise+Library+5.0" rel="tag">Enterprise Library 5.0</a>,<a href="http://technorati.com/tags/Configuration+Services" rel="tag">Configuration Services</a>,<a href="http://technorati.com/tags/configSource" rel="tag">configSource</a></div> <p align="justify">Last week, we saw how to use the same configuration file for two different applications. If you have not read the post yet, you can read it <a href="http://vaideeswaranr.blogspot.com/2010/08/enterprise-library-50-configuration.html">here</a>. This week, we will look at how configuration services can help you use a section from a shared configuration file. For the sake of continuity, I will use the same example we used in the previous post.</p> <p align="justify">In the previous post, we added a separate logging section, the change here is that we will just add a redirected section in the Configuration Sources element itself. To enable section redirection, the following has to be done</p> <ul> <li> <div align="justify">Change the configuration source to System Configuration Source. This will enable the application to use its default app.config</div> </li> <li> <div align="justify">Add a redirected section. You can do this by expanding the Configuration Sources section and then add a redirected section. The source of the configuration section should now point to the section in the external file. The screenshot below shows how to add a redirected section</div> </li> </ul> <p align="justify"><a href="http://lh3.ggpht.com/_2cIm7h7Xp2w/TG1zSoY3INI/AAAAAAAAANo/oLHHBwSn500/s1600-h/image%5B5%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_2cIm7h7Xp2w/TG1zTLncUWI/AAAAAAAAANs/_zEz7gheNi8/image_thumb%5B3%5D.png?imgmax=800" width="399" height="279" /></a> </p> <div align="justify"> </div> <p align="justify">The configuration file now looks like the below</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;"><?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span> <span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">="utf-8"</span> ?<span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">section</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="enterpriseLibrary.ConfigurationSource"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#ff0000;">requirePermission</span><span style="color:#0000ff;">="true"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">enterpriseLibrary.ConfigurationSource</span> selectedSource="System Configuration Source"<span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">sources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="System Configuration Source"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Shared Config"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#ff0000;">filePath</span><span style="color:#0000ff;">="C:\Users\r.vaideeswaran\Documents\Visual Studio 2010\Projects\EntLibTest\logging.config"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">sources</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">redirectSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> <span style="color:#0000ff;"><</span><span style="color:#800000;">add</span> <span style="color:#ff0000;">sourceName</span><span style="color:#0000ff;">="Shared Config"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="loggingConfiguration"</span> <span style="color:#0000ff;">/></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">redirectSections</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">enterpriseLibrary.ConfigurationSource</span><span style="color:#0000ff;">></span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> <span style="color:#0000ff;"></</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">></span></pre><br /><!--CRLF--></div><br /></div><br /><br /><p align="justify">Note how the logging is now configured as a redirectSection. This will ensure that the logging section is picked up from the shared config file. The code in the previous post will now work with this configuration as well without any changes. For the sake of reference, below is the code </p><br /><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">class</span> Program</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> LogWriter textWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> LogEntry testEntry = <span style="color:#0000ff;">new</span> LogEntry();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> testEntry.Message = <span style="color:#006080;">"Logging the loading of EntLibTest1"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> testEntry.Priority = 2;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> testEntry.Categories.Add(<span style="color:#006080;">"General"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> textWriter.Write(testEntry);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> Console.WriteLine(<span style="color:#006080;">"Message Logged"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> Console.ReadLine();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> }</pre></div><br /></div><br /><br /><p align="justify">While in theory, this is the same as using the configSource for a particular setting, one of the advantages is that configSource doesnt permit you to use absolute paths (except for appSettings where you can use the file attribute instead of configSource). Sometimes, this can be a real pain. The Ent Lib Configuration Sources solves that problem.</p><br /><br /><p align="justify">Hope I was able to give you an idea of how to use a redirected section with Ent Lib 5.0. Happy Coding!!!!</p>Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com3tag:blogger.com,1999:blog-3614688564147648057.post-81663134815469744052010-08-08T07:45:00.001-07:002010-08-08T08:35:35.162-07:00Enterprise Library 5.0 – Configuration Services Part - I<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:5752d232-0587-479d-a7e7-e4bd9244b41d" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/Enterprise+Library+5.0" rel="tag">Enterprise Library 5.0</a>,<a href="http://technorati.com/tags/Configuration+Services" rel="tag">Configuration Services</a>,<a href="http://technorati.com/tags/.NET" rel="tag">.NET</a></div> <p align="justify">Microsoft recently released Ent Lib 5.0. While most of the application blocks are the same as 4.1, one of the new kids on the block is Configuration Services. Hence, I will start off this series on Ent Lib 5.0 by taking a closer look at the Configuration Services.</p> <p align="justify">The configuration services provides three capabilities when it comes to managing config files</p> <ul> <li> <div align="justify">Ability for multiple applications to use the same config file</div> </li> <li> <div align="justify">Ability to use of a specific section from a shared config file</div> </li> <li> <div align="justify">Ability for a configuration file to inherit from another config file</div> </li> </ul> <p>In this post, we will see how to enable multiple applications to share the same config file.</p> <h3><u>Multiple Applications using the Same Config File</u></h3> <p align="justify">I will start off by creating a simple console application. For first timers, right click on the config and you should see a context menu to edit enterprise library configuration.</p> <p>Clicking on the menu brings up the Ent Lib configuration console. The console has been re-designed in the latest version of enterprise library. To add a configuration source section, there is a menu called “Add Configuration Settings” under Blocks menu. Click on it and you should see a Configuration Section now added. Click on the <a href="http://lh3.ggpht.com/_2cIm7h7Xp2w/TF7DEDyozCI/AAAAAAAAANY/Blv7hSBWqt0/s1600-h/image%5B11%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSru8_ekJWiP1TpA02UF2UJfVgjOchUoA5UusuQCSAUCftUMH6duqeZGqvze2sU3ykXc5i0uHaAP05KeUokMaFTC4DGcCpi9eH-j27om3PC_iF2wlnepvocA6Vdxc8iLZndK9mVy7G0Mg/?imgmax=800" width="23" height="18" /></a> icon to expand the section and add a File Based Configuration Source. </p> <p align="justify">Provide the values of the config name and the path of the config file. In the selected source dropdown, select the shared source that we have just configured. Save the settings and right click on the Configuration Sources section and click on Validate. You will get the list of warnings and errors, if any. Let us take a moment to look at the changes in the config. The following section has been added under the <configuration> section</p> <blockquote> <p><configSections>
<br /> <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<br /></configSections> </p> <p><enterpriseLibrary.ConfigurationSource selectedSource="Common Config">
<br /> <sources>
<br /> <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<br /> <add name="Common Config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
<br /> filePath="CommonConfig" />
<br /> </sources>
<br /></enterpriseLibrary.ConfigurationSource></p> </blockquote> <p align="justify">When you close the enterprise library console, you should now see a commonconfig.config file in your project path. Right click and include the same in your project. Make sure you change the property of the file to copy over to the output path. This will now be the configuration that your application now uses. </p> <p>To test this, I have added a logging application block and used the following code to log an entry into the event log.</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> LogWriter textWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();</pre><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> LogEntry testEntry = <span style="color:#0000ff;">new</span> LogEntry();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> testEntry.Message = <span style="color:#006080;">"Logging the loading of EntLibTest"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> testEntry.Priority = 2;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> testEntry.Categories.Add(<span style="color:#006080;">"General"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> textWriter.Write(testEntry);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> Console.WriteLine(<span style="color:#006080;">"Message Logged"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> Console.ReadLine();</pre>
<br /><!--CRLF--></div>
<br /></div><style type="text/css"><br /><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style><p>Do remember to make the appropriate configuration changes to ensure logging is enabled. Running the application creates an entry in the event log. I now add another project to the same solution and update the configuration source to point to the commonconfig.config of the first project. Below is a snapshot of how my config for the second project looks like.</p><p><a href="http://lh6.ggpht.com/_2cIm7h7Xp2w/TF7DGDk_CFI/AAAAAAAAANg/dyY-YtSIVIE/s1600-h/image%5B21%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirPIzFgZF9RwK6ET8hkLRMzHMn582zxqZftDqa-xqgoGjZ4lq3q1Uu1hUoMOLkLJijCbq55gsRhtiaNzcrHDFPwAw3v5ud7vcyOJ14v98peVD4DIPRkxeQH-CDXd2ERJNEOBaJevEKZaI/?imgmax=800" width="302" height="142" /></a> </p><p align="justify">You should be able to run the application without making any other changes and ensure that messages are logged from the second console application as well. </p><p align="justify">Before all you web developers get excited, please remember that this feature does not work with web applications just yet. You can see the details of why in this <a href="http://www.dotnetforthoughts.com/post/2010/06/30/BUG-External-configuration-source-file-in-Enterprise-Library-5.aspx">blog</a>. </p><p align="justify">That concludes our first look at Configuration Services in Enterprise Library 5.0. I will dig into the other two aspects in subsequent posts. Happy Coding!!!</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com2tag:blogger.com,1999:blog-3614688564147648057.post-34807538737451099782010-07-19T11:56:00.001-07:002010-07-19T11:56:28.553-07:00Code Generation with VS 2010<p align="justify"> </p> <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:9da28b7f-bdd7-466b-914e-0b3d5f8d6c88" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/VS+2010" rel="tag">VS 2010</a>,<a href="http://technorati.com/tags/Code+Generation" rel="tag">Code Generation</a>,<a href="http://technorati.com/tags/UML" rel="tag">UML</a>,<a href="http://technorati.com/tags/Architecture" rel="tag">Architecture</a></div> <p align="justify">Have been off for quite some time, so its good to be back  blogging. I will start off with a relatively light post. </p> <p align="justify">All of us know that you can create Architectural Diagrams in VS 2010 using the architecture menu. But once, you have done creating the diagrams how do you actually make the diagrams generate code? The last thing you want to do is to create the diagrams and write the code for it manually. </p> <p align="justify">Though VS 2010 does not have built in support for code generation, you can have it generate code by downloading and installing <a href="http://msdn.microsoft.com/en-us/vstudio/ff655021.aspx">Visualisation and Modelling Feature Pack</a>.</p> <p align="justify">Once you download and install the feature pack from the link above, there are a couple of small things you have to do to get your diagrams ready for code generation. The first thing is to set a profile to your model. i.e tell the model which language to generate code.</p> <p align="justify">You can do this by viewing the properties of the model explorer. You should see an attribute called Stereotypes as shown in the diagram below. </p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJolg9oGb8C4kLPPzIkCdG4kGqTcyDaHfpJh7cIa2JbtbJwTIaIJwDlwXI_DIw76zscEcqnlg53HEuPFoKgfUa0ul2snrc5z7mAAXbnUTfDCld1czd0fKbrhFCja0p_vzOizIb5HMHuAw/s1600-h/image%5B4%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_2cIm7h7Xp2w/TESfzUvkNlI/AAAAAAAAANE/EmFBujJhtdQ/image_thumb%5B2%5D.png?imgmax=800" width="379" height="322" /></a> </p> <p align="justify"> </p> <p align="justify">Select C# namespace. As you can see, the whole code generation works on T4. So if you want to have code generated in SQL for example you could just write your text transformation templates and you should be ready to go.</p> <p align="justify">Actually, there is a very good blog <a href="http://www.olegsych.com/2010/01/uml-modeling-and-code-generation-in-visual-studio-2010/">here</a> on how exactly to write your own T4 templates for code generation. </p> <p align="justify">Once you have done that, you can then configure how your classes and namespaces should be named and organised. You do this by clicking on Architecture-> Configure Default code generation settings. </p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFxwdP2ZdFc8pzC7iLzIXqmCE4llLq22JOzy8n39C6mGUr-zQZabBDNduAY-9qYiQOeshNRTvo3Yl6q7Fq3CSSj7MTJjvaxwYY7XkosnQTUT6yiSmBJrcMnXVFL3HNX0VIPD1KBzD_4DY/s1600-h/image%5B13%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_2cIm7h7Xp2w/TESf09rWeVI/AAAAAAAAANM/WIuqsDLLFa4/image_thumb%5B7%5D.png?imgmax=800" width="317" height="178" /></a> </p> <p align="justify">Beware, by default, the namespace is always the name of the model so be wary when you name the model because once you set it, you will not be able to change it. You should now be ready to start generating code. Right click on your modelling diagram and you should see a generate code menu as shown. You can set the path under which you want the class to be generated here. </p> <p align="justify"> </p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJuYQjI1l8gBboHBaHxzyVAY69yU8xn5rb-v6vkKocvuwNu-XkraSvRJr-FqRqRFDeOSxF-EfPVquZwlUFuqwZRT6YO8Uf_sb-zz3W1A_y_v78GSmaYlpTxNlpZ_qxuzYajdADGEnWQ_U/s1600-h/image%5B9%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_2cIm7h7Xp2w/TESf2yQ8LcI/AAAAAAAAANU/SurxAOlXxLI/image_thumb%5B5%5D.png?imgmax=800" width="412" height="248" /></a></p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com1tag:blogger.com,1999:blog-3614688564147648057.post-75337531521612646762010-01-01T10:05:00.001-08:002010-01-01T10:08:33.568-08:00Working with Hierarchical data in SQL Server 2008<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:542fbb9e-c7d4-47b4-9aaa-46db01419f13" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/HierarchyId" rel="tag">HierarchyId</a>,<a href="http://technorati.com/tags/SQL+Server+2008" rel="tag">SQL Server 2008</a>,<a href="http://technorati.com/tags/CTEs" rel="tag">CTEs</a></div> <p align="justify">In my series on SQL Server 2008, one of the features I had missed was the HierarchyId datatype and its usage. I came across this recently and decided to take a peek into what it does. This post covers my thoughts on the HierarchyId datatype.</p> <p align="justify">At the time of this writing, I am still not convinced on the value add the HierarchyId datatype provides over traditional parent/child storage mechanisms. More on that later. For now, we will deal with how the datatype works and how it can be used to store hierarchial data.</p> <p align="justify">The big advantage with HierarchyId is that it is optimised to store data in a tree structure thereby making it simpler for developers to query and update hierarchical information.  The important thing to keep in mind is that using the hierarchyid by itself does not mean that data is stored in a tree structure. It is the responsibility of the application to make sure that the data is stored in the desired structure. HierarchyId just acts as an enabler. </p> <p align="justify">To demonstrate this, I will work through an example where we have to store the list of folders and files in the database. I have created a table with the following structure. </p> <blockquote> <p>Create Table FileExplorer <br />( <br />    ItemId HierarchyId Primary Key, <br />    ItemLevel as ItemId.GetLevel(), <br />    ItemName varchar(100)    <br />)</p> </blockquote> <p align="justify">I will touch upon the ItemLevel column later. Do note that the HierarchyId datatype by itself is not unique. It is application’s responsibility again to maintain the uniqueness of the ItemId column and hence I have declared a PrimaryKey on that column.</p> <p align="justify">Coming to ItemLevel column, the primary purpose of that is to enable breadth first ordering. Typically, hierarchy ids can be breadth first or depth first indexed. You can find the details and working of both these types of ordering <a href="http://msdn.microsoft.com/en-us/library/bb677268(SQL.105).aspx" target="_blank">here</a>.</p> <p align="justify">Now, let us create the breadth first index. Depending on how many levels (called fan-outs) you plan to store, your indexing strategy will vary. It is not necessary to create both the indexes for all the scenarios.</p> <blockquote> <p>CREATE INDEX FileExplorer_Breadth_First <br />ON FileExplorer(ItemLevel,ItemId) <br />GO</p> </blockquote> <p align="justify">Since the FileExplorer table already has a Primary key, the above index will be created as a non-clustered index. </p> <p align="justify">Now that we have created the table, let us start loading data into the table. If you haven’t already noted, let me point out that the ItemId is not declared as an Identity (it cannot be, actually) and hence it is the application’s responsibility again to make sure that the generated ids are unique. In addition, to ensure the tree structure, we have to make sure the id generated is based on the parent id. </p> <p align="justify">The stored procedure to insert a new file or folder is </p> <blockquote> <p>CREATE PROCEDURE InsertFile(@folderId hierarchyid, @ItemName nvarchar(100) ) <br />AS <br />BEGIN <br />    DECLARE @last_child hierarchyid </p> <p>SELECT @last_child = MAX(ItemId) FROM FileExplorer <br />    WHERE ItemId.GetAncestor(1) = @folderId <br />INSERT FileExplorer (ItemId, ItemName) <br />SELECT @folderId.GetDescendant(@last_child, NULL), @ItemName <br />END ;</p> </blockquote> <p> </p> <p align="justify">Let us examine this procedure for a second.  The ItemId.GetAncestor(1) gets the immediate parent of the ItemId. So essentially, the select statement gets  the last valid file under the given folder id. Now that we have the last inserted item under the parent, our new file has to be created as a peer to that item.</p> <p align="justify">This is done by the second select statement in the procedure. We use the GetDescendant method to create a child similar to another child, in this case, the item retrieved from the previous select. It wouldn’t be out of place to mention here that this is where our breadth first ordering helps. </p> <p align="justify">The tree can also be maintained by creating a parentId column that references the itemId but there is also a performance drag associated with that approach. A snapshot of the data after inserting some rows is as below</p> <p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1v-t-NJGMFQMexiKaIJ5bcTGQMobNxoXdvQPuC1RkyrP89FWVGw1bibc1OI0hPRhIZBToNTqnDigiMHB-lE45AmUX4LFtscJ3v0Im_skIUR8cKJubmxBBBvQSuW2nAf66zcNBuoL1uIY/s1600-h/Results%5B6%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Results" border="0" alt="Results" src="http://lh3.ggpht.com/_2cIm7h7Xp2w/Sz45bFPE64I/AAAAAAAAAMA/FsSam6WrgE0/Results_thumb%5B4%5D.jpg?imgmax=800" width="517" height="203" /></a></p> <p align="justify">Note how the item level computes itself. The folders testFolderChild and TestfolderChildPeer are at the same level and have the same parent –TestFolder. </p> <p align="justify">Let us now see how we can retrieve the list of parents for a given node. To do this, I had to write a stored procedure using CTEs. Below is the stored procedure I wrote</p> <blockquote> <p>create proc dbo.GetAncestors(@ItemId hierarchyId) <br />as <br />begin </p> <p>With AncestorsList(Itemid, ItemName, ItemLevel, Parent) <br />As <br />( <br />    Select ItemId, ItemName, ItemLevel, ItemId.GetAncestor(1) as Parent from Fileexplorer where ItemId=@ItemId <br />    UNION ALL <br />    Select fe.ItemId, fe.ItemName, fe.ItemLevel, fe.Itemid.GetAncestor(1) from FileExplorer fe Inner Join AncestorsList al <br />    ON fe.ItemId = al.Parent <br />)    <br />    Select fe.ItemId, fe.ItemName, fe.ItemLevel from FileExplorer fe INNER JOIN <br />    AncestorsList al on fe.ItemId = al.Itemid <br />end</p> </blockquote> <p align="justify"> </p> <p align="justify">If you are familiar with CTEs then most of the code above is self explanatory. My anchor members gets the first ancestor of the given node. This is then executed recursively by my recursive query. The last select executes the CTE to return the resultset.</p> <p align="justify">Having seen most of the uses of HierarchyId, its biggest drawback lies in the fact that it still needs the developers to do a lot. For fan-outs of less that 2 levels, I still feel the self referencing table is faster than using the hierarchyId. I strongly believe that Microsoft still has some work left to do with the HierarchyId datatype to make sure it is a bit more easier to implement. </p> <p align="justify">That though concludes this post. If you are looking to work with HierarchyIds, I hope this post has given you an idea of where to start. Hope to be back soon with something else. Happy Coding!!!!!!</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-80501628929703022662009-12-12T07:37:00.001-08:002009-12-12T07:50:03.861-08:00.NET 4.0 and VS 2010 Series – Dynamic Features<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:a63389c3-2bc1-4b91-b35c-dc239f2475e4" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/Dynamic" rel="tag">Dynamic</a>,<a href="http://technorati.com/tags/Expando" rel="tag">Expando</a>,<a href="http://technorati.com/tags/.NET+4.0" rel="tag">.NET 4.0</a>,<a href="http://technorati.com/tags/C%23+4.0" rel="tag">C# 4.0</a>,<a href="http://technorati.com/tags/DLR" rel="tag">DLR</a></div> <p align="justify">I have deliberately delayed writing this post to try and get my heads around all the dynamic features in .NET 4.0. At first read, I thought it was just the Expando object and whenever you use the ExpandoObject in your code, the code runs in DLR. Though this is partly true, I realised there is a bit more to the dynamic features than just a two-liner. </p> <p align="justify">The Dynamic Features are implemented in the following forms in .NET 4.0</p> <ul> <li> <div align="justify">The Expando Object</div> </li> <li> <div align="justify">The Dynamic Object</div> </li> <li> <div align="justify">The Dynamic Language Runtime (DLR)</div> </li> </ul> <p><strong><span style="font-size:100%;">ExpandoObject</span></strong></p> <p>As the name roughly suggests, expandos are expandable objects which means you can add properties, methods and even events at runtime. You can also set and get the values of such members.</p> <p>The syntax to create expandable objects is as follows</p> <blockquote> <p>dynamic person = new ExpandoObject(); <br />person.FirstName = "test First Name"; <br />person.LastName = "test Last Name";</p></blockquote> <p align="justify">You will see that I have created the person object as dynamic. We will come to that a bit more later. If you compile a solution, with just this piece of code, it will compile perfectly fine. </p> <p align="justify">You can also pass Expando objects as parameters to other methods or functions. Beware that when you do so, you will not get intellisense on the dynamic object because they aren’t yet resolved; they are resolved only during runtime. Below is a code snippet where I have created an ExpandoObject and passed that as a parameter to another method that displays the values of the object’s. members.</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background- padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">using</span> System;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;">using</span> System.Dynamic;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;">namespace</span> DLRExample</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">class</span> Program</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> dynamic Vehicle = <span style="color:#0000ff;">new</span> ExpandoObject();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> Vehicle.Make = <span style="color:#006080;">"Ford"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> Vehicle.Model = <span style="color:#006080;">"Endeavour"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> Vehicle.Engine = <span style="color:#006080;">"4 cylinder"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> Vehicle.Color = <span style="color:#006080;">"Black"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> WriteVehicleDetails(Vehicle);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> Console.ReadLine();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum26" style="color:#606060;"> 26:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> WriteVehicleDetails(dynamic vehicleobject)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum27" style="color:#606060;"> 27:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum28" style="color:#606060;"> 28:</span> Console.WriteLine(<span style="color:#006080;">"The make of the vehicle is {0}"</span>, vehicleobject.Make);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum29" style="color:#606060;"> 29:</span> Console.WriteLine(<span style="color:#006080;">"The color of the vehicle is {0}"</span>, vehicleobject.Color);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum30" style="color:#606060;"> 30:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum31" style="color:#606060;"> 31:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum32" style="color:#606060;"> 32:</span> }</pre></div></div><br /><p align="justify">Similarly, .NET allows you to add methods at runtime to an expando object. In C#, you can add methods by using lambda expressions. The syntax for the same is as below</p><p align="justify">expandObject.Increment = (Action)(() => { expandoObject.attribute++; });</p><p align="justify">I have modified the above code to add a method to the ExpandoObject called ChangeVehicleColor. The method changes the color of the vehicle to white from its existing value. The new code is as below</p><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><span class="Apple-style-span" style="line-height: normal;"><br /></span> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">using</span> System;</pre><!--CRLF--><span class="Apple-style-span" style=" white-space: pre; font-family:monospace;font-size:13px;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</span><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;">using</span> System.Dynamic;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;">namespace</span> DLRExample</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">class</span> Program</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> dynamic Vehicle = <span style="color:#0000ff;">new</span> ExpandoObject();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> Vehicle.Make = <span style="color:#006080;">"Ford"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> Vehicle.Model = <span style="color:#006080;">"Endeavour"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> Vehicle.Engine = <span style="color:#006080;">"4 cylinder"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> Vehicle.Color = <span style="color:#006080;">"Black"</span>;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> Vehicle.ChangeVehicleColor = (Action)(() => {Vehicle.Color=<span style="color:#006080;">"White"</span>;});</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> WriteVehicleDetails(Vehicle);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> Vehicle.ChangeVehicleColor();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> WriteVehicleDetails(Vehicle);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> Console.ReadLine();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum26" style="color:#606060;"> 26:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum27" style="color:#606060;"> 27:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum28" style="color:#606060;"> 28:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> WriteVehicleDetails(dynamic vehicleobject)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum29" style="color:#606060;"> 29:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum30" style="color:#606060;"> 30:</span> Console.WriteLine(<span style="color:#006080;">"The make of the vehicle is {0}"</span>, vehicleobject.Make);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum31" style="color:#606060;"> 31:</span> Console.WriteLine(<span style="color:#006080;">"The color of the vehicle is {0}"</span>, vehicleobject.Color);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum32" style="color:#606060;"> 32:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum33" style="color:#606060;"> 33:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum34" style="color:#606060;"> 34:</span> }</pre></div></div><br /><br /><p align="justify">Please note that the syntax to invoke the method in C# is slightly different from VB.NET. In VB.NET, you will have to use the Invoke function explicitly to call the dynamic method.</p><p align="justify">Lastly, the expando object also implements the generic IDictionary interface. This enables developers to enumerate the members of the object, if need be. The syntax to enumerate the members is fairly simple. For the vehicle object above, the code will be as below</p><blockquote> <p align="justify">foreach (var property in (IDictionary<String, Object>)Vehicle)</p> <p align="justify">{</p> <p align="justify"> Console.WriteLine(property.Key + ": " + property.Value);</p> <p align="justify">}</p></blockquote><br /><p align="justify"><strong><span style="font-size:100%;">Dynamic Object</span></strong></p><p align="justify">The dynamic object has been introduced in C# for a variety of reasons. One of the most important aspects of the dynamic object is that it skips type checking during compile time. You can invoke any method on the dynamic object type without having to worry about compiling errors.</p><p align="justify">Thought at the outset, the dynamic and expando objects seem similar, there are a number of differences between the two. While you can instantiate an expandoobject class, you cannot do the same with a dynamic class. Dynamic object is just a holding class. They are declared with the <em>dynamic</em> keyword as shown in my first example in this post.</p><p align="justify">Some of the practical usages of the dynamic object are with COM Interops. Instead of trying to tightly bind an interop class you can always declare that class as dynamic. You will do away with a lot of type casting that needs to be done otherwise.</p><p><strong><span style="font-size:100%;">Dynamic Language Runtime (DLR)</span></strong></p><p align="justify">The DLR is an open source runtime that sits above the CLR and ensures the working of the dynamic languages in .NET. Apart from the two objects described above, .NET also supports a couple of dynamic languages like IronRuby and IronPython. These run on the DLR. You can download the latest version of DLR from <a href="http://www.codeplex.com/dlr" target="_blank">codeplex</a>.</p><p align="justify">What the DLR does is to keep track of usages of the dynamic objects and ensures that they are evaluated at runtime. For this, the DLR has three services</p><ul><br /><li><div align="justify">Expression Trees - These represent language syntaxes and semantics</div></li><br /><li><div align="justify">Call Site Caching – These help track dynamic object usages and information about operations on the Dynamic Objects</div></li><br /><li><div align="justify">Dynamic Object Interoperability – These are essentially a set of interfaces that help creation of dynamic objects.</div></li></ul><p align="justify">You can get more information about the DLR <a href="http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx" target="_blank">here</a>.</p><p align="justify"><strong><span style="font-size:100%;">Usage Scenarios</span></strong></p><p align="justify">Now that we have seen the different elements of dynamic features in C# 4.0, let me try and explain where I think these can be used.</p><ul><br /><li><div align="justify">The Expando object is interoperable between different framework languages. So it is an obvious choice in situations where you have to pass objects between different framework languages.</div></li><br /><li><div align="justify">String to object conversion scenarios. A scenario commonly found in projects where mainframe is involved. I think it would be compelling to use the Expando object along with text generation templates to come up with a code generator for mapping mainframe strings to application objects</div></li><br /><li><div align="justify">As mentioned earlier in the post, the dynamic object is a good choice for interacting with COM interops. Not only that, I think it is very useful to also be used with scripting objects.</div></li><br /><li><div align="justify">If these two objects don’t do the trick for you, remember you can always create your own dynamic objects by implementing the IDynamicMetaObjectProvider interface.</div></li></ul><p align="justify">That concludes this post regarding the dynamic features in C# 4.0. I hope this has given you a good overview for you to start exploring these objects in a bit more detail</p><br /><p align="justify"><strong><span style="font-size:100%;"> </span></strong></p>Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-89083305116246906402009-11-21T08:58:00.000-08:002009-11-21T09:40:04.038-08:00.NET 4.0 and VS 2010 Series – Named and Optional Parameters<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:c73ac34c-af21-46fe-bf2e-ced717109c88" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/.NET+4.0" rel="tag">.NET 4.0</a>,<a href="http://technorati.com/tags/Named+Parameters" rel="tag">Named Parameters</a>,<a href="http://technorati.com/tags/Optional+Parameters" rel="tag">Optional Parameters</a>,<a href="http://technorati.com/tags/C%23+4.0" rel="tag">C# 4.0</a></div> <p align="justify">After my posts on the framework features over the past couple of weeks, I will now start focussing on the language aspects of C# 4.0. In C# 4.0, named and optional parameters have been introduced. This is one feature that I have been waiting for so I am only glad that the .NET team has decided to introduce this feature in C# 4.0.</p> <p align="justify">VB 6.0 programmers are aware of the usage of the optional parameters. It used to be declared as </p> <pre> Sub DoSomething(Optional ByVal Switch As Boolean = False)</pre><pre><span class="Apple-style-span" style="font-family: Georgia, serif; white-space: normal; font-size: 16px; ">What the above declaration meant was that you could call the DoSomething method without explicitly passing the <em>Switch</em> parameter. The default value of false will be assigned to the variable if it was not passed.</span></pre><p align="justify">This was not possible in .NET. The workaround was to declare overloaded methods. You do not need to do that anymore. In C# 4.0, you can declare a method with an optional parameter simply by assigning a default value for the parameter in question like the example below. </p><blockquote><div style="text-align: justify;">private string Extract(String inputString, int endIndex=0, int startIndex =0)</div><p> <br /></p></blockquote><p align="justify">Named parameters as the name suggests, enable the developers to skip a parameter while calling the method if they wish to. This is done by passing the parameter by quoting its name and value separated by a colon as shown in the syntax below</p><blockquote><p align="justify">Extract(inputString, startIndex: 4);</p><br /></blockquote><p align="justify">The syntax above calls the method extract with three parameters the signature of which is described previously. You will notice that the second parameter endIndex has been skipped and the value is being passed for startIndex. Below is the complete code snippet</p><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">using</span> System;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;">using</span> System.Diagnostics.Contracts;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;">namespace</span> NamedOptionalParams</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">class</span> Program</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> String inputString = <span style="color:#006080;">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> String result = Extract(inputString,6);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> String result1 = Extract(inputString, startIndex: 4);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> String result2 = Extract(inputString, startIndex: 6, endIndex: 10);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> Console.WriteLine(result);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> Console.WriteLine(result1);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> Console.WriteLine(result2);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> Console.ReadLine();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> <span style="color:#008000;">//this method creates a substring based on the input parameters</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum26" style="color:#606060;"> 26:</span> <span style="color:#008000;">//1. The input string is always passed</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum27" style="color:#606060;"> 27:</span> <span style="color:#008000;">//2. Start Index is an optional parameter. If this is not passed then the </span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum28" style="color:#606060;"> 28:</span> <span style="color:#008000;">// start is assumed to be 0 which is the default value</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum29" style="color:#606060;"> 29:</span> <span style="color:#008000;">//3. The End Index can be omitted. If it is omitted, then start Index has to</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum30" style="color:#606060;"> 30:</span> <span style="color:#008000;">// be passed and the string is from the start index to the end of the input</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum31" style="color:#606060;"> 31:</span> <span style="color:#008000;">// string</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum32" style="color:#606060;"> 32:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> Extract(String inputString, <span style="color:#0000ff;">int</span> endIndex=0, <span style="color:#0000ff;">int</span> startIndex =0)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum33" style="color:#606060;"> 33:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum34" style="color:#606060;"> 34:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum35" style="color:#606060;"> 35:</span> <span style="color:#0000ff;">if</span> (endIndex==0)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum36" style="color:#606060;"> 36:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum37" style="color:#606060;"> 37:</span> endIndex = inputString.Length;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum38" style="color:#606060;"> 38:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum39" style="color:#606060;"> 39:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum40" style="color:#606060;"> 40:</span> String outString = inputString;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum41" style="color:#606060;"> 41:</span> outString = inputString.Substring(startIndex, (endIndex - startIndex));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum42" style="color:#606060;"> 42:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum43" style="color:#606060;"> 43:</span> <span style="color:#0000ff;">return</span> outString;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum44" style="color:#606060;"> 44:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum45" style="color:#606060;"> 45:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum46" style="color:#606060;"> 46:</span> }</pre></div></div><span class="Apple-style-span" style="line-height: 16px;"><br /></span><p align="justify">In the above example, the extract method just creates a substring for a specified start and end index. I have made three calls to the method. Note that with Named Parameters, the order in which you pass the parameters is not important as long as you don’t forget to name them correctly. You can see this in Line 17 of the above code.</p><p>That is it for this post on named and optional parameters. They are quite a useful feature that will reduce the number of overloaded methods that we will have to write. I am sure you will find more ways of putting them to use. </p>Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-88611218524211000612009-11-17T18:43:00.001-08:002009-11-17T18:43:53.614-08:00.NET 4.0 and VS 2010 Series - PIA – Rest in Peace<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:21187181-7f9d-45d3-b38a-9c3bf8f48965" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/.NET+4.0" rel="tag">.NET 4.0</a>,<a href="http://technorati.com/tags/PIA" rel="tag">PIA</a>,<a href="http://technorati.com/tags/Interop" rel="tag">Interop</a>,<a href="http://technorati.com/tags/Embedded+Interop+Types" rel="tag">Embedded Interop Types</a>,<a href="http://technorati.com/tags/Type+Equivalence" rel="tag">Type Equivalence</a></div> <p align="justify">In previous versions of .NET, Primary Interop Assemblies (PIA) acted as the bridge between unmanaged COM components and managed code. Though PIA solved the problem of marshalling for almost all types, they came with their own problems. One of the biggest problem was that even if a small feature was used, the developer had to ship the complete PIA which is not a small payload by any means.</p> <p align="justify">Also, a developer had to develop an assembly for each version of word or excel that the application targeted. This drastically increased the size and the complexity of the deployment package.</p> <p align="justify">With .NET 4.0 this has changed. Developers no longer need to ship the whole PIA. .NET 4.0 gives the developer the flexibility of embedding only those features that are used by the application rather than shipping the whole assembly. This also means that the developer does not need to worry about the availability of the target applications like Word or Excel. Enabling embedded interop assemblies is very easy. You right click the interop reference and change the EmbedInteropTypes from false to True. </p> <p align="justify"><a href="http://lh5.ggpht.com/_2cIm7h7Xp2w/SwNfXxdSLYI/AAAAAAAAALs/hWP4YH_dTkE/s1600-h/Enbed3.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Enbed" border="0" alt="Enbed" src="http://lh3.ggpht.com/_2cIm7h7Xp2w/SwNfYqpNIzI/AAAAAAAAALw/HRel9nZ2zU8/Enbed_thumb1.jpg?imgmax=800" width="261" height="244" /></a></p> <p align="justify">When you do that, the interop assemblies are no longer referred. You can confirm this by using the reflector. The snapshot will look like below. Note that the Excel and Word are now namespaces within  the project instead of references. </p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHYfZofI_e_2APTrJ2OQH15LXaxS5o9mmnrh29duG_nCuDU8n4pMYpAB-rFO-hZWew0ddtNMFPKLMuQETcJACQYwtnlnMn7YRAauTfUndf1uMMVGGngloFNCa0mOtcOGTtQOGOQTGPrUk/s1600-h/Reflector%5B2%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Reflector" border="0" alt="Reflector" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdy8hnot983M-_pOl1wXvJCR1sYzpNlYS-TydleNvtyq1JIK0oK7UgdqN4Pl0vZV1bT9Pwen_SpvlueocgDznGu9HkfD9DeBA3onZAYAhhDInFb8YlXT7t1KzzqUH2jE8QLthb_XJamIw/?imgmax=800" width="210" height="244" /></a></p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-81414537260543345462009-11-06T23:09:00.001-08:002009-11-06T23:10:52.407-08:00How to: Increase the size of your virtual hard disk<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:1ea66487-b106-48c3-a6e7-a260d5bf22dd" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/VHD" rel="tag">VHD</a>,<a href="http://technorati.com/tags/Harddisk" rel="tag">Harddisk</a>,<a href="http://technorati.com/tags/VHD+Resizer" rel="tag">VHD Resizer</a>,<a href="http://technorati.com/tags/How+to" rel="tag">How to</a>,<a href="http://technorati.com/tags/Microsoft+Virtual+PC" rel="tag">Microsoft Virtual PC</a>,<a href="http://technorati.com/tags/Virtual+Image" rel="tag">Virtual Image</a>,<a href="http://technorati.com/tags/DISKPART" rel="tag">DISKPART</a></div> <p align="justify">If you have run out of space in your virtual image hard disk and didn’t know the way out of it; read on. I ran into this problem last week and found the following solution after a bit of googling. Though there were a lot of posts targeted at VMWare, I was looking for ones specific to Microsoft VPC. The magic tool that you have to look at is called <a href="http://vmtoolkit.com/files/folders/converters/entry87.aspx" target="_blank">VHD Resizer.</a> It is a free download available from the <a href="http://vmtoolkit.com/" target="_blank">VM tool kit</a> site. You just have to register on the site to download. </p> <p align="justify">When you run the tool, it will ask you to point to the vhd whose hard disk size you want to increase. You can also set the size you want to increase it to. The only thing that needs to be remembered, is that it makes a copy of the VHD so you will need the disk space to cope with it. </p> <p align="justify">When this process is complete, the next thing you have to do is to make sure your volume uses the new space allocated. You can do this by using the DISKPART command. You will find the necessary instructions <a href="http://support.microsoft.com/kb/325590" target="_blank">here</a>. That’s it!!! You will have your new drive with loads of space!!! </p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-82247664716410443832009-11-03T16:59:00.001-08:002009-11-03T17:07:34.833-08:00VS 2010 and .NET 4.0 Series – Code Contracts - II<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:28c595ab-1bf7-46b1-8b1f-b07ec38cf783" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/.NET+4.0" rel="tag">.NET 4.0</a>,<a href="http://technorati.com/tags/VS+2010" rel="tag">VS 2010</a>,<a href="http://technorati.com/tags/Code+Contracts" rel="tag">Code Contracts</a>,<a href="http://technorati.com/tags/Invariant" rel="tag">Invariant</a>,<a href="http://technorati.com/tags/PostConditions" rel="tag">PostConditions</a></div> <p align="justify">In the <a href="http://vaideeswaranr.blogspot.com/2009/10/visual-studio-2010-and-net-40-series.html" target="_blank">first</a> part of this series, I wrote about how you can check preconditions with Code Contracts. We also looked at some of the features of postconditions. In this post, we will implement postconditions with an example and go on to look at object invariants.</p> <p align="justify">You can implement Postconditions using Contract.Ensures method. Similar to the Contract.Requires, this takes in a boolean parameter and can display a string as the error message. Unlike the Requires method, Contract.Ensures cannot throw a specific exception. </p> <p>We will use the same Concatenate method that we kicked off in the first part and enhance that with some postconditions checks. Here is what it looks like</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> Concatenate(<span style="color:#0000ff;">string</span> leadingstring, <span style="color:#0000ff;">string</span> trailingstring)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#008000;">//raise an exception if the contract fails</span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> Contract.Requires<ArgumentNullException>(leadingstring.Length > 0);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> <span style="color:#008000;">//make sure the string is greater than 10 characters before the code exits the method</span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> Contract.Ensures(Contract.Result<<span style="color:#0000ff;">string</span>>().Length > 10, <span style="color:#006080;">"The string is too short"</span>);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#008000;">//ensure that the concatenated string is at least longer than the leading string by 2</span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> Contract.Ensures(Contract.Result<<span style="color:#0000ff;">string</span>>().Length > Contract.OldValue<<span style="color:#0000ff;">string</span>>(leadingstring).Length + 2, <span style="color:#006080;">"The value of the concatenated string is not long enough"</span>);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> leadingstring = String.Concat(leadingstring, trailingstring)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> <span style="color:#0000ff;">return</span> leadingstring;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> }</pre><!--CRLF--></div><br /></div><br /><br /><p align="justify">The method now returns the concatenated string. It also includes a couple of ensures statements. Note that though we are implementing postconditions, the Contract.Ensures statement should be included at the beginning of the method and not at the end. There are two checks I have done</p><br /><br /><ul><br /> <li><div align="justify">The first to make sure that the length of the concatenated string is greater than 10. The Contract.Result returns the value that is being returned from the method.</div><br /> </li><br /><br /> <li><div align="justify">The second check makes sure that the concatenated string is at least 2 characters longer than the leading string. Here, I have used the Contract.OldValue to get the original value of leadingstring when it entered the method.</div><br /> </li></ul><blockquote><p align="justify">Unlike Contract.Requires, you will not be able to catch a specific exception if the Contract.Ensures fails. In the main method, I have caught a generic exception to handle any contract exceptions. Below is my main method with the changes.</p><br /></blockquote><br /><span class="Apple-style-span" style="line-height: 16px;"><span class="Apple-style-span" style="line-height: normal;"><br /></span></span><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">try</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;">string</span> resultString = Concatenate(<span style="color:#006080;">"1234567891011"</span>, <span style="color:#006080;">"a"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> Console.WriteLine(<span style="color:#006080;">"The result is {0}"</span>, resultString);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> Console.WriteLine(resultString);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#008000;">//catch the argument null exception raised by the contract</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> <span style="color:#0000ff;">catch</span> (ArgumentNullException nullString)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> Console.WriteLine(nullString.Message);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> <span style="color:#0000ff;">catch</span> (Exception ex)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> Console.WriteLine(ex.Message);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> Console.ReadLine();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> }</pre></div></div><br /><span class="Apple-style-span" style="font-weight: bold; ">Object Invariants</span><br /><br /><p align="justify">Invariants are code contract features that enable you to check object state and ensure conditions are met when you call methods on these objects. To validate a class using invariants, you will have to write an Invariant method that returns void. This method needs to be decorated with the [ContractInvariantMethod] attribute. </p><p align="justify">To demonstrate the use of invariants, I have refactored the above concatenation code using an external class called StringManipulation. This class that has two public variables and one Concatenate method. </p><span class="Apple-style-span" style="line-height: 16px;"><span class="Apple-style-span" style="line-height: normal;"><br /></span></span><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">using</span> System;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> <span style="color:#0000ff;">using</span> System.Diagnostics.Contracts;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> <span style="color:#0000ff;">namespace</span> CodeContractsSample</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">class</span> StringManipulation</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> leadingString = <span style="color:#006080;">"12345678"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> trailingString = <span style="color:#006080;">"87654321"</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> [ContractInvariantMethod]</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> ObjectInvariant()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> Contract.Invariant(<span style="color:#0000ff;">this</span>.leadingString.Length>5);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> Contract.Invariant(<span style="color:#0000ff;">this</span>.trailingString.Length>5);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Concatenate()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> <span style="color:#0000ff;">return</span> String.Concat(leadingString, trailingString);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum26" style="color:#606060;"> 26:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum27" style="color:#606060;"> 27:</span> </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum28" style="color:#606060;"> 28:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum29" style="color:#606060;"> 29:</span> }</pre></div></div><span class="Apple-style-span" style="line-height: 16px;"><br /></span><br /><p align="justify">As you can see, I have also added the CodeInvariant method to check if both the strings are at least 5 characters in length. This means that I will have to initialize the string to pass that condition. Otherwise the object does not get created.</p><p align="justify">The other consideration you will have to keep in mind when using object invariants are that the invariant checks are only done when a method call is made. In essence, invariants will not work for DTO or DataContracts. The workaround is to use Contract.Requires on the property.</p><p align="justify"><strong>Legacy If..Else Blocks</strong></p><p align="justify">While Code Contracts are a new .NET 4.0 feature, there exists a lot of legacy code using If…Else… that does similar validations. If you want the .NET framework to treat them as contracts, you can use the EndContractBlock provided the If…Else… are the first set of statements in a method. when you use EndContractBlock, all the If..Else.. conditions are assumed as preconditions.</p><p align="justify"><strong>Code Contract Usage Considerations</strong></p><p align="justify">Code Contracts give dev leads and architects a plethora of options to do validations. But, as with anything else, there are certain decision points that need to be taken care of. </p><ul><br /> <li><div align="justify">Decide very early in the project how you are going to use Code Contracts. Though static checking is very powerful, it also puts a lot of drab in your build process. Make sure you are turning static checking on only when you need it.</div><br /> </li><li><div align="justify">You could also do runtime checking only on debug builds. This works very well for TDD projects</div><br /> </li><li><div align="justify">Be wary of turning on runtime checking on production builds. Remember, .NET 4.0 is still in its beta. </div><br /> </li><li><div align="justify">Code Contracts are very extensive. Make sure you understand all the features offered before picking on the best way to do your validations or checks. </div><br /> </li><li><div align="justify">When you are using both preconditions and postconditions in the same method, there is a specific order which is recommended. The order according to the Code Contract documentation is as follows</div><ul><br /> <li><div align="justify">Legacy if…Then…Else blocks</div></li><br /> <li><div align="justify">Contract.Requires to check public preconditions</div></li><br /> <li><div align="justify">Contract.Ensures to check public postconditions</div><br /> </li><li><div align="justify">Contract.EnsuresOnThrow to check public exceptional conditions</div><br /> </li><li><div align="justify">Contract.Ensures to check all private postconditions</div><br /> </li><li>Contract.EnsuresOnThrow to check all private postconditions before finishing off with the EndContractBlock </li><br /> </ul></li><li><div align="justify">You can use Contracts with pre .NET 4.0 versions by referencing the Microsoft.Contracts library. In .NET 4.0 Code Contracts have been integrated into the mscorlib.dll</div><br /> </li><li><div align="justify">One of the recommended ways of using Code Contracts is to have a separate config file for contracts.</div><br /> </li><li>The contracts build output can be shipped along with your build so that other developers\administrators can make use of it. Make sure you understand the different build options.</li><br /></ul><p align="justify">That concludes my two part post on Code Contracts and their usage. The posts try to give you a peek are by no means exhaustive on the features that Code Contracts offer. For complete understanding, I would recommend you download the manual from DevLabs and give it at least a quick skim through. Happy Coding!!!!</p><br /><br /><ul></ul>Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-18313151255678203252009-10-28T06:42:00.001-07:002009-10-28T06:50:52.635-07:00Visual Studio 2010 and .NET 4.0 Series – Code Contracts<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:bb1cf96e-2c62-4186-92d2-c932705fb28f" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/.NET+4.0" rel="tag">.NET 4.0</a>,<a href="http://technorati.com/tags/Visual+Studio+2010" rel="tag">Visual Studio 2010</a>,<a href="http://technorati.com/tags/VS+2010" rel="tag">VS 2010</a>,<a href="http://technorati.com/tags/Code+Contracts" rel="tag">Code Contracts</a></div> <p align="justify">I had written a couple of days ago on my <a href="http://vaideeswaranr.blogspot.com/2009/10/visual-studio-2010-and-net-40-beta-2.html" target="_blank">blog</a> about the release of VS2010 and .NET 4 Beta 2. Now that I have had a chance to install and play around a bit, I am hoping to explore some of the new features of .NET 4 and VS 2010 in the next few weeks. I will kick off this week with Code Contracts. </p> <p align="justify">According to MSDN, “Code contracts provide a way to specify preconditions, postconditions, and object invariants in your code”. To work with code contracts .NET 4 now comes with a new namespace <span style="font-family:Aria;color:#0080ff;">System.Diagnostics.Contracts</span>.</p> <p align="justify">As mentioned in the above definition, code contracts can be used to check, pre-conditions, post-conditions and invariants. Pre-conditions are conditions that should be satisfied before a method is invoked; post-conditions are conditions that should be met just before the method finishes execution and invariants validate class state. We will look at Pre-Conditions with an example and a peek into Post Conditions in this post. I will finish this off with another post on Post Conditions example and invariants.</p> <p align="justify"><strong>Preconditions</strong></p> <p align="justify">The Contract.Requires method can be used to validate preconditions. An example of this will be to validate parameters that are being passed into a method. To demonstrate this, I will create a new console project and write the code below.</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">using</span> System;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> <span style="color:#0000ff;">using</span> System.Diagnostics.Contracts;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;">namespace</span> CodeContractsSample</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">class</span> Program</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> Concatenate(String.Empty, <span style="color:#006080;">"test"</span>);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> Console.ReadLine();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Concatenate(<span style="color:#0000ff;">string</span> leadingstring, <span style="color:#0000ff;">string</span> trailingstring)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> Contract.Requires(leadingstring.Length > 0, <span style="color:#006080;">"Ensure leading string is valid."</span>);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> Console.WriteLine(String.Concat(leadingstring,trailingstring));</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> }</pre><br /><!--CRLF--></div><br /></div><br /><br /><p align="justify">Let us harp a bit on the Contract.Requires method. The method takes two parameters; the first is a boolean condition that needs to be enforced. The second optional parameter is the message that needs to be displayed if the condition evaluates to false. In the above example, the method makes sure the leading string is not empty.</p><p align="justify">When I wrote this code and ran it, I found it to my surprise that the contracts were not working. After a bit of research, I found that for contracts to work, you need an additional download. You can find the download at <a href="http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx" target="_blank">Dev Labs</a>. Once you download and run the installer, you will find an additional tab called Contracts in your solution properties as shown below.</p><p align="justify"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjgZ89LbE40wn291qOgCwV7lVuadzUCIVRjQb8NTMvmr1rHySOwwsVambOQ1Mt6FwgphWI3PZAuf4IYsq_cxIDPqpMxyyYEBsK7DDV42CdbYQI3yUop5qutKREz9PWUEx5C3wCU345Sps/s1600-h/CodeContracts6.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="CodeContracts" border="0" alt="CodeContracts" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSzxvMdwWbBarNv3l-riKOj5wI-RKi9IXHf_V5gNDoLZt6LfLq0s_bErIE3zM5ZuUeyV07Ktzm2GOuRBmDIo6TamwlBcpCnB526Vrmcn2R3Vy9FT6ywqgkKg5gaImmDqJJuYENyGyL6AQ/?imgmax=800" width="618" height="473" /></a></p><p align="justify">You can then control how your contracts work in this window. Please note that to without Team System you can do only runtime checking. Running the application now throws the designated message if we pass an empty string to the Concatenate method.</p><p align="justify">A variant of this is the Requires<> method which takes a generic TException parameter. Below is the code to raise an exception and catch it in code. One thing you have to make sure is you turn off the assert on contract failure setting in the Code Contracts settings tab. This will ensure exceptions are raised from the contracts.</p><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background- margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; direction: ltr; max-height: 200px; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:#f4f4f4;" id="codeSnippetWrapper"><br /> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">using</span> System;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum4" style="color:#606060;"> 4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum5" style="color:#606060;"> 5:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum6" style="color:#606060;"> 6:</span> <span style="color:#0000ff;">using</span> System.Diagnostics.Contracts;</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum7" style="color:#606060;"> 7:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum8" style="color:#606060;"> 8:</span> <span style="color:#0000ff;">namespace</span> CodeContractsSample</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum9" style="color:#606060;"> 9:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">class</span> Program</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum12" style="color:#606060;"> 12:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum13" style="color:#606060;"> 13:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum14" style="color:#606060;"> 14:</span> <span style="color:#0000ff;">try</span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum15" style="color:#606060;"> 15:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum16" style="color:#606060;"> 16:</span> Concatenate(String.Empty, <span style="color:#006080;">"test"</span>);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum17" style="color:#606060;"> 17:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum18" style="color:#606060;"> 18:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum19" style="color:#606060;"> 19:</span> <span style="color:#008000;">//catch the argument null exception raised by the contract</span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum20" style="color:#606060;"> 20:</span> <span style="color:#0000ff;">catch</span>(ArgumentNullException nullString)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum21" style="color:#606060;"> 21:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum22" style="color:#606060;"> 22:</span> Console.WriteLine(nullString.Message);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum23" style="color:#606060;"> 23:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum24" style="color:#606060;"> 24:</span> Console.ReadLine();</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum25" style="color:#606060;"> 25:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum26" style="color:#606060;"> 26:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum27" style="color:#606060;"> 27:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Concatenate(<span style="color:#0000ff;">string</span> leadingstring, <span style="color:#0000ff;">string</span> trailingstring)</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum28" style="color:#606060;"> 28:</span> {</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum29" style="color:#606060;"> 29:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum30" style="color:#606060;"> 30:</span> <span style="color:#008000;">//raise an exception if the contract fails</span></pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum31" style="color:#606060;"> 31:</span> Contract.Requires<ArgumentNullException>(leadingstring.Length > 0);</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum32" style="color:#606060;"> 32:</span> Console.WriteLine(String.Concat(leadingstring, trailingstring));</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum33" style="color:#606060;"> 33:</span> </pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum34" style="color:#606060;"> 34:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum35" style="color:#606060;"> 35:</span> }</pre> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; direction: ltr; border-top-style: none; border-left-style: none; overflow: visible; padding-top: 0pxfont-family:'Courier New', courier, monospace;font-size:8pt;color:black;"><span id="lnum36" style="color:#606060;"> 36:</span> }</pre><!--CRLF--></div><br /></div><br /><span class="Apple-style-span" style="font-weight: bold; ">PostConditions</span><br /><br /><br /><p align="justify">Though most of the basic concepts are the same for post and preconditions, postconditions offer a bit more flexibility and features that preconditions do not. Postconditions are validated by using the Contract.Ensures method. Postconditions also offer the following capabilities</p><ul><br /> <li><div align="justify">Ability to refer the return value of a method using Contract.Result<T> where T should be of the same type as the return type of the method. Obviously you cannot use Result for methods that do not return anything.</div></li><br /> <li><div align="justify">Ability to check the value of a variable at the beginning of the method. This can be done by using Contract.OldValue<T>(e) where e is the variable for which you want the old value and T is the type of that variable.</div></li><br /> <li><div align="justify">Ability to ensure a condition is met when an error is thrown in the method. You can use the Contract.EnsuresonThrow<>. </div><br /> </li></ul><p align="justify">That is it for this post. I will show you in my next post how to write PostConditions and we will also look at invariants. </p>Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-90315838979390904802009-10-19T11:14:00.001-07:002009-10-19T11:14:31.945-07:00Visual Studio 2010 and .NET 4.0 Beta 2 now available<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:62f2b4f3-140b-433a-b82f-a13f57eaeaa9" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/VS2010" rel="tag">VS2010</a>,<a href="http://technorati.com/tags/.NET+4" rel="tag">.NET 4</a>,<a href="http://technorati.com/tags/Beta" rel="tag">Beta</a>,<a href="http://technorati.com/tags/MSDN" rel="tag">MSDN</a></div> <p align="justify"> </p> <p align="justify">Microsoft made available today the beta 2 of VS2010 and .NET 4.0. It is available for MSDN Subscribers on the 19th and its general availability is from the 21st of October. You can download the beta 2 from <a title="https://msdn.microsoft.com/en-us/subscriptions/securedownloads/default.aspx?pv=18%3a370" href="https://msdn.microsoft.com/en-us/subscriptions/securedownloads/default.aspx?pv=18%3a370">https://msdn.microsoft.com/en-us/subscriptions/securedownloads/default.aspx?pv=18%3a370</a>.  </p> <p align="justify">Apart from bug fixes the Beta 2 also is supposed to have templates for Sharepoint solutions and Azure. More details on Somasegar’s blog <a title="http://blogs.msdn.com/somasegar/" href="http://blogs.msdn.com/somasegar/">http://blogs.msdn.com/somasegar/</a></p> <p align="justify">I will write more on VS 2010 when I have had a chance to play with it. </p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-51552727208383703412009-10-04T10:39:00.001-07:002009-10-04T10:46:40.511-07:00Using Table Value Parameters in SQL Server 2008<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:483942fc-7141-49d4-87c9-e05d61637b82" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/SQL+Server+2008" rel="tag">SQL Server 2008</a>,<a href="http://technorati.com/tags/Table+Value+Parameters" rel="tag">Table Value Parameters</a>,<a href="http://technorati.com/tags/.NET" rel="tag">.NET</a></div> <p align="justify">Table-valued parameter is another exciting new feature in SQL Server 2008. Essentially, TVP solves the problem of inserting multiple rows into the database. Various solutions have been implemented to achieve this, ranging from round trips for every row to comma separated values. With TVP, you can insert multiple rows with a single round trip to the server without having the additional overheads of composition and de-composition of values.</p> <p align="justify">TVP is not entirely new to SQL Server. SQL Server 2000 introduced the table variables which can be used to store a set of records. Where TVP builds on it, is the ability to pass them as input parameters to stored procedures or functions. This could not be done with table variables. </p> <p align="justify">Let us look at some of the features of TVP before we see how they can be used to insert data. </p> <ul> <li> <div align="justify">TVPs can participate in Set based operations.</div> </li> <li> <div align="justify">They are strongly typed</div> </li> <li> <div align="justify">You cannot perform DML operations on TVPs. They can be passed as only READ-ONLY parameters to stored procedures\functions</div> </li> <li> <div align="justify">Remember that TVPs are materialized in the TempDB. Essentially, it means that if you insert more rows into your TVP, the size of TempDB is what gets affected.</div> </li> </ul> <p align="justify">Having seen what TVP is, let us now move onto build an example which will actually use TVP. Here, I will show you a very common scenario that we use for multiple entries. We have an order and each order is comprised of multiple products. I have created the necessary tables using the scripts below</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> Create table dbo.Products</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> (</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> ProductId <span style="color: #0000ff">int</span> IDENTITY(1,1) PRIMARY KEY,</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span> ProductName varchar(250),</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5"> 5:</span> ItemRate <span style="color: #0000ff">decimal</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6"> 6:</span> )</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7"> 7:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8"> 8:</span> Create table dbo.Orders</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9"> 9:</span> (</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10"> 10:</span> OrderId <span style="color: #0000ff">int</span> IDENTITY(1,1) Primary key,</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11"> 11:</span> OrderDate datetime,</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12"> 12:</span> OrderStatus <span style="color: #0000ff">int</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13"> 13:</span> )</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14"> 14:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15"> 15:</span> Create Table dbo.OrderDetails</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16"> 16:</span> (</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17"> 17:</span> OrderId <span style="color: #0000ff">int</span> Foreign Key references Orders(OrderId),</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18"> 18:</span> ProductId <span style="color: #0000ff">int</span> Foreign Key references Products(ProductId),</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19"> 19:</span> Quantity <span style="color: #0000ff">int</span>,</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20"> 20:</span> Amount <span style="color: #0000ff">decimal</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21"> 21:</span> )</pre><br /><!--CRLF--></div><br /></div><br /><br /><p>I have also inserted data for the tables above. The next thing to do is to create a user defined type that will insert data into the OrderDetails table. </p><br /><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><br /> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> Create type dbo.OrderParam <span style="color: #0000ff">as</span> Table</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> (ProductId <span style="color: #0000ff">int</span>, Quantity <span style="color: #0000ff">int</span>, Amount <span style="color: #0000ff">decimal</span>)</pre><br /><!--CRLF--></div><br /></div><br /><br /><p></p><br /><br /><p></p><br /><br /><p>Now that the type has been created, let me go ahead and create a stored procedure that takes the just created type as an input parameter. </p><br /><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><br /> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> Create proc dbo.InsertOrders</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> @OrderItems OrderParam ReadOnly</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #0000ff">as</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span> begin</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5"> 5:</span> Declare @iOrderId <span style="color: #0000ff">int</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6"> 6:</span> Insert into Orders values (GETDATE(),0)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7"> 7:</span> Select @iOrderId=SCOPE_IDENTITY()</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8"> 8:</span> Insert into OrderDetails(OrderId, ProductId, Quantity, Amount)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9"> 9:</span> Select @iOrderId, ProductId, Quantity, Amount from @OrderItems</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10"> 10:</span> end</pre><br /><!--CRLF--></div><br /></div><br /><br /><p align="justify">Notice the fact that the TVPs have been declared read only. Also pay attention to the second insert statement. The values are inserted from the input parameter using a single select statement. The only remaining thing to do is to call this stored procedure from the front end. </p><br /><br /><p align="justify">For this purpose, I have created a data grid where users select the orders and save it into the database. Below is the code that goes into the Save Click routine.</p><br /><br /><div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><br /> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> SaveButton_Click(<span style="color: #0000ff">object</span> sender, EventArgs e)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #008000">//create a new datatable to hold the grid data</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span> DataTable OrderParam = <span style="color: #0000ff">new</span> DataTable();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5"> 5:</span> <span style="color: #008000">//the columns are the same as the TVP type</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6"> 6:</span> OrderParam.Columns.Add(<span style="color: #006080">"ProductId"</span>, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">int</span>));</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7"> 7:</span> OrderParam.Columns.Add(<span style="color: #006080">"Quantity"</span>, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">int</span>));</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8"> 8:</span> OrderParam.Columns.Add(<span style="color: #006080">"Amount"</span>, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">decimal</span>));</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9"> 9:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10"> 10:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11"> 11:</span> <span style="color: #008000">//loop through the grid to load data into the datatable</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12"> 12:</span> <span style="color: #0000ff">foreach</span> (DataGridViewRow item <span style="color: #0000ff">in</span> grdOrders.Rows)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13"> 13:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14"> 14:</span> DataGridViewComboBoxCell prodCell = (DataGridViewComboBoxCell) item.Cells[0];</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15"> 15:</span> <span style="color: #0000ff">if</span> (prodCell.Value != <span style="color: #0000ff">null</span>)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16"> 16:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17"> 17:</span> DataRow prdRow = OrderParam.NewRow();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18"> 18:</span> prdRow[0] = prodCell.Value;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19"> 19:</span> prdRow[1] = item.Cells[1].Value;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20"> 20:</span> prdRow[2] = item.Cells[2].Value;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21"> 21:</span> OrderParam.Rows.Add(prdRow);</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22"> 22:</span> }</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23"> 23:</span> }</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24"> 24:</span> <span style="color: #008000">//connect to the database to insert the values.</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25"> 25:</span> var connString = ConfigurationManager.ConnectionStrings[<span style="color: #006080">"DatabaseConn"</span>].ConnectionString;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26"> 26:</span> SqlConnection dbConn = <span style="color: #0000ff">new</span> SqlConnection(connString);</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27"> 27:</span> dbConn.Open();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28"> 28:</span> <span style="color: #0000ff">using</span>(SqlCommand cmd = <span style="color: #0000ff">new</span> SqlCommand(<span style="color: #006080">"InsertOrders"</span>, dbConn))</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29"> 29:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30"> 30:</span> cmd.CommandType = CommandType.StoredProcedure;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31"> 31:</span> cmd.Parameters.AddWithValue(<span style="color: #006080">"OrderItems"</span>, OrderParam);</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32"> 32:</span> cmd.ExecuteNonQuery();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33"> 33:</span> }</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34"> 34:</span> dbConn.Close();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35"> 35:</span> }</pre><br /><!--CRLF--></div><br /></div><br /><br /><p align="justify">The only thing worth noticing is that I have used a datatable which is quite logical because TVPs are tables anyways. As you can see, the savings in the number of round trips to the database is enormous. The USP of the TVPs lie in the fact that they are not hard to implement at all.  Have fun!!!</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-34578003894975655382009-10-02T22:04:00.001-07:002009-10-02T22:04:23.521-07:00Time well SpEnt<p></p> <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:971d5af2-4bd9-43ba-a398-443783b2337c" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/Quizzing" rel="tag">Quizzing</a>,<a href="http://technorati.com/tags/ASAP" rel="tag">ASAP</a>,<a href="http://technorati.com/tags/Chennai" rel="tag">Chennai</a></div> <p></p> <p align="justify">When a relatively unheard of theatre group comes forward to do a quiz, you are not sure what to expect. But ASAP did themselves only favours, be it the quiz or the short sketch they showcased just before the finals. The only disappointing thing for the evening was the number of teams participating. The quiz master put the number at 100 which seemed a bit low given that Chennai usually attracts a lot of participation. </p> <p align="justify">The quiz kicked off with 40 question prelims which seems to be the standard nowadays. It fittingly started with a question on the Bapu. </p> <p align="justify">There were questions on Anjali Tendulkar, the Olympics and Quick Gun Murugan. The toughest one seemed to be on the longest winning streak in sports history. I don’t think any of the 100 odd teams got this right. 8 teams qualified for the finals at the end of a low scoring prelims with  “When I was in London” comprising of Srinivasan and Rajagopal coming out tops. </p> <p align="justify">The questions in the finals were engrossing bringing out some brilliant answers from the teams. The quality of quizzing was again top-class. JK and Sriram were quick off the blocks but the team of V.V. Ramanan and Ramkumar eventually caught up leading the quiz into the last round. It came down to the last question to decide the winners and “When I was in London” bagged it with a brilliant answer on the word “FAIL”. </p> <p align="justify">Overall a very enjoyable evening. If anything, the finals just seemed to be one round too long but I dont think the ardent quizzers of Chennai would complain given the good quizzing that was in offing. Mention should also go to the quiz master Vinod and his colleague Ganesh for the research and the quality of questions.</p> <p align="justify">I hope that this now goes on to become an annual event and one more annual quiz gets added to the Chennai quizzing calendar. Way to go ASAP!!!</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0tag:blogger.com,1999:blog-3614688564147648057.post-42463146476888541112009-09-12T13:01:00.001-07:002009-09-12T13:01:51.302-07:00CLR Integration Changes in SQL Server 2008<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:cd441ca7-76cd-4e7d-a9c2-2f91d50fede9" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/SQL+Server+2008" rel="tag">SQL Server 2008</a>,<a href="http://technorati.com/tags/CLR+Integration" rel="tag">CLR Integration</a>,<a href="http://technorati.com/tags/Nullable+Types" rel="tag">Nullable Types</a>,<a href="http://technorati.com/tags/Database" rel="tag">Database</a></div> <p align="justify">CLR Integration was first introduced in SQL Server 2005. Though there was a lot of excitement when the feature was  introduced, it has since fizzled off. Personally, I think CLR Integration is one of the most underused features of SQL Server. For some reason, we have not been able to move away from extended procedures or UDFs to writing CLR Integrated code.</p> <p align="justify">In SQL Server 2008, there are two noticeable introductions to CLR Introduction. One is the support for LINQ and the other is the support for Nullable types. </p> <p align="justify">To enable CLR support in SQL Server, you first have to switch it on. This is done by running the sp_configure with clr enabled = 1.  One thing to keep in mind is when you are using nullable types, you cannot use the automatic deployment option within Visual Studio 2008. You will have to register your assemblies manually. I will briefly show you how it is done.</p> <p align="justify">Once you have turned the support for CLR on, the next step is to create your assembly. For this, I have a stored procedure written in Visual Studio. The code for the same is as below</p> <p align="justify"> </p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> <span style="color: #0000ff">using</span> System;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> <span style="color: #0000ff">using</span> System.Data;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #0000ff">using</span> System.Data.SqlClient;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span> <span style="color: #0000ff">using</span> System.Data.SqlTypes;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5"> 5:</span> <span style="color: #0000ff">using</span> Microsoft.SqlServer.Server;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6"> 6:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7"> 7:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8"> 8:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> StoredProcedures</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9"> 9:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10"> 10:</span> [Microsoft.SqlServer.Server.SqlProcedure]</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11"> 11:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> SearchEmployee(Int64? iEmployeeId, <span style="color: #0000ff">out</span> Int32? iVacationhours)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12"> 12:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13"> 13:</span> iVacationhours = <span style="color: #0000ff">null</span>;</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14"> 14:</span> <span style="color: #0000ff">if</span> (iEmployeeId !=<span style="color: #0000ff">null</span>)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15"> 15:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16"> 16:</span> <span style="color: #008000">//open the sql connection with the current context</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17"> 17:</span> <span style="color: #0000ff">using</span> (SqlConnection connection = <span style="color: #0000ff">new</span> SqlConnection(<span style="color: #006080">"context connection=true"</span>))</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18"> 18:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19"> 19:</span> <span style="color: #008000">//open the connection</span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20"> 20:</span> connection.Open();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21"> 21:</span> <span style="color: #008000">//build the query and execute it </span></pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22"> 22:</span> <span style="color: #0000ff">string</span> query = <span style="color: #006080">"Select VacationHours from HumanResources.Employee where EmployeeId = "</span> + iEmployeeId.ToString();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23"> 23:</span> SqlCommand sCommand = <span style="color: #0000ff">new</span> SqlCommand(query, connection);</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24"> 24:</span> SqlDataReader vacationReader = sCommand.ExecuteReader();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25"> 25:</span> <span style="color: #0000ff">using</span> (vacationReader)</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26"> 26:</span> {</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27"> 27:</span> vacationReader.Read();</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28"> 28:</span> iVacationhours = vacationReader.GetInt32(0);</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29"> 29:</span> }</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30"> 30:</span> } </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31"> 31:</span> }</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32"> 32:</span> </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33"> 33:</span>  </pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34"> 34:</span> }</pre><br /><!--CRLF--><br /><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35"> 35:</span> };</pre><br /><!--CRLF--></div><br /></div><br /><br /><p></p><br /><br /><p align="justify">The code just returns the vacation hours for a given employee. The point worth noting is that both input and output parameters are nullable types. The next step is to add the assembly to the SQL Server database. </p><br /><br /><p>The syntax to do that is </p><br /><br /><p><span class="lnum">   1: </span>create assembly NullableTypes from </p><br /><br /><div class="csharpcode"><br /> <pre><span class="lnum"> 2: </span><span class="str">'C:\Users\Administrator\Documents\Visual Studio 2008\Projects\NullableTypesExample\NullableTypesExample\bin\Debug\NullableTypesExample.dll'</span></pre><br /><br /> <pre><span class="lnum"> 3: </span>Go</pre><br /></div><br /><style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style><br /><br /><p>Once the assembly is registered, you can now proceed to create your stored procedure pointing it to the managed code by executing the statement below</p><br /><br /><div class="csharpcode"><br /> <pre class="alt"><span class="lnum"> 1: </span>Create Proc dbo.SearchEmployee(@a bigint, @b smallint output)</pre><br /><br /> <pre><span class="lnum"> 2: </span><span class="kwrd">as</span></pre><br /><br /> <pre class="alt"><span class="lnum"> 3: </span>EXTERNAL NAME NullableTypes.[StoredProcedures].SearchEmployee</pre><br /><br /> <pre><span class="lnum"> 4: </span>GO</pre><br /></div><br /><style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style><br /><br /><p>That is it!!! You are now all set to execute the stored procedure and check the results. For your reference, I am also giving below the statements to execute the procedure</p><br /><br /><p> </p><br /><br /><div class="csharpcode"><br /> <pre class="alt"><span class="lnum"> 1: </span>declare @outvalue smallint</pre><br /><br /> <pre><span class="lnum"> 2: </span>set @outvalue = 0</pre><br /><br /> <pre class="alt"><span class="lnum"> 3: </span>exec dbo.SearchEmployee 1,@outvalue output</pre><br /><br /> <pre><span class="lnum"> 4: </span>select @outvalue</pre><br /></div><br /><style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style><br /><br /><p></p><br /><br /><p>If I change the input value to null, null will be returned as output exhibiting support for Nullable Types.</p> Vaideeswaranhttp://www.blogger.com/profile/13193265232043179983noreply@blogger.com0