Latest Microsoft Blogs

Browse by Tags

Related Posts

  • Interface Inheritance Esoterica

    I learned something new yesterday about interface inheritance in .NET as compared to implementation inheritance. To illustrate this difference, here’s a simple demonstration. I’ll start with two concrete classes, one which inherits from the other. Each class defines a property. In this case, we’re dealing with implementation inheritance . public class Person { public string Name { get; set; } } public class SuperHero : Person { public string Alias { get; set; } } We can now use two different techniques to print out the properties of the SuperHero type: type descriptors and reflection. Here’s a little console app that does this. Note the code I’m showing below doesn’t include a few Console.WriteLine calls that I have in the actual app. static...


  • Neat VS10 Feature: Pinning A Debugger Watch

    I was stepping through some code in a debugger today and noticed a neat little feature of Visual Studio 2010 that I hadn’t noticed before. When debugging, you can easily examine the value of a variably by highlighting it with your mouse. Nothing new there. But then I noticed a little pin next to it, which I’ve never seen before. So what do you see when you see a pin? You click on it! As you might expect, that pins the quick watch in place. So now I hit the play button, continue running my app in the debugger, and the next time I hit that breakpoint: I can clearly see the value changed since the last time. I think this may come in useful when walking through code as a way of seeing the value of important variables right next to where they are...


  • A RouteHandler for IHttpHandlers

    I saw a bug on Connect today in which someone offers the suggestion that the PageRouteHandler (new in ASP.NET 4) should handle IHttpHandler as well as Page . I don’t really agree with the suggestion because while a Page is an IHttpHandler , an IHttpHandler is not a Page . What I this person really wants is a new handler specifically for http handlers. Let’s give it the tongue twisting name: IHttpHandlerRouteHandler . Unfortunately, it’s too late to add this for ASP.NET 4, but it turns out such a thing is trivially easy to write. In fact, here it is. public class HttpHandlerRouteHandler<THandler> : IRouteHandler where THandler : IHttpHandler, new () { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new THandler...


  • Software Externalities

    If you’re a manufacturing plant, one way to maximize profit is to keep costs as low as possible. One way to do that is to cut corners. Go ahead and dump that toxic waste into the river and pollute the heck out of the air with your smoke stacks. These options are much cheaper than installing smoke scrubbers or trucking waste to proper disposal sites. Of course, economists have long known that this does not paint the entire picture. Taking these shortcuts incur other costs, it’s just that these costs are not borne by the manufacturing plant. The term externalities describes such spillover costs. In economics an externality or spillover of an economic transaction is an impact on a party that is not directly involved in the transaction. In such...


  • ASP.NET MVC 2 Preview 2

    Today we just released ASP.NET MVC 2 Preview 2 for Visual Studio 2008 SP1 (and ASP.NET 3.5 SP1), which builds on top of the work we did in Preview 1 released two months ago . Download Page Release Notes Roadmap Some of the cool new features we’ve added to Preview 2 include: Client-Side Validation – ASP.NET MVC 2 includes the jQuery validation library to provide client-side validation based on the model’s validation metadata. It is possible to hook in alternative client-side validation libraries by writing an adapter which adapts the client library to the JSON metadata in a manner similar to the xVal validation framework . Areas – Preview 2 includes in-the-box support for single project areas for developers who wish to organize their application...


  • Successive Method Calls With MoQ

    One area where using MoQ is confusing is when mocking successive calls to the same method of an object. For example, I was writing some tests for legacy code where I needed to fake out multiple calls to a data reader. You remember data readers, don’t you? Here’s a snippet of the code I was testing. Ignore the map method and focus on the call to reader.Read . while (reader.Read()) { yield return map(reader); } Notice that there are multiple calls to reader.Read . The first couple times, I wanted Read to return true . The last time, it should return false . And here’s the code I hoped to write to fake this using MoQ: reader.Setup(r => r.Read()).Returns( true ); reader.Setup(r => r.Read()).Returns( true ); reader.Setup(r => r.Read()).Returns...


  • Html Encoding Code Blocks With ASP.NET 4

    One great new feature being introduced in ASP.NET 4 is a new code block ( often called a Code Nugget by members of the Visual Web Developer team ) syntax which provides a convenient means to HTML encode output in an ASPX page or view. <% : CodeExpression %> I often tell people it’s <%= but with the = seen from the front. Let’s look at an example of how this might be used in an ASP.NET MVC view. Suppose you have a form which allows the user to submit their first and last name. After submitting the form, the same view is used to display the submitted values. First Name: <% : Model.FirstName %> Last Name: <% : Model.FirstName %> < form method ="post" > <% : Html.TextBox( "FirstName" ) %> <...


  • 7 Stages of new language keyword grief

    My last post on the new dynamic keyword sparked a range of reactions which are not uncommon when discussing a new language keyword or feature. Many are excited by it, but there are those who feel a sense of…well…grief when their language is “marred” by a new keyword. C#, for example, has seen it with the var keyword and now with the dynamic keyword. I don’t know, maybe there’s something to this idea that developers go through the seven stages of grief when their favorite programming language adds new stuff ( Disclaimer: Actually, I’m totally making this crap up ) 1. Shock and denial. With the introduction of a new keyword, initial reactions include shock and denial. No way are they adding lambdas to the language! I had a hard enough time with...


  • Fun With Method Missing and C# 4

    Warning : What I’m about to show you is quite possibly an abuse of the C# language. Then again, maybe it’s not. ;) You’ve been warned. Ruby has a neat feature that allows you to hook into method calls for which the method is not defined. In such cases, Ruby will call a method on your class named method_missing . I showed an example of this using IronRuby a while back when I wrote about monkey patching CLR objects . Typically, this sort of wild chicanery is safely contained within the world of those wild and crazy dynamic language aficionados, far away from the peaceful waters of those who prefer statically typed languages. Until now suckas! ( cue heart pounding rock music with a fast beat ) C# 4 introduces the new dynamic keyword which adds...


  • Simpler Transactions

    The .NET Framework provides support for managing transactions from code via the System.Transactions infrastructure. Performing database operations in a transaction is as easy as writing a using block with the TransactionScope class. using (TransactionScope transaction = new TransactionScope()) { DoSomeWork(); SaveWorkToDatabase(); transaction.Complete(); } At the end of the using block, Dispose is called on the transaction scope. If the transaction has not been completed (in other words, transaction.Complete was not called), then the transaction is rolled back. Otherwise it is committed to the underlying data store. The typical reason a transaction might not be completed is that an exception is thrown within the using block and thus the Complete...


Page 1 of 10 (95 items) 1 2 3 4 5 Next > ... Last »
Microsoft Communities