Latest Microsoft Blogs

Browse by Tags

Related Posts

  • Metrics in software and physics

    Every so often, somebody points out how bad of a metric code coverage is . And of course, on its own, it doesn’t tell you much: after all, it’s a single number. How could it possibly reflect all the subtlety (or lack thereof) of your designs and of your testing artillery? Of course, within all the various *DD approaches, some better than others enable you to know whether or not your code conforms to its requirements, but I thought I’d take a moment to reflect on the general idea of a software metric and how it relates to the mothers of all metrics: physical ones, cause you know, I used to be a scientist. Proof: the lab coat on the picture. The theory of measurement is at the center of all experimental physics. This comes from the realization...


  • Using C# Dynamic to simplify ADO.NET Data Access

    Recently, I started playing around with C# dynamic, and blogged how it could be used to call static class members late bound .  Today, I was talking to Phil Haack , who I think had talked to ScottGu , and he mentioned that it would be cool to use dynamic to simplify data access when you work directly with SQL query.  So I thought I’d play around with that, and it didn’t take much code to make it work nicely. So the scenario is that you’re not using any fancy O/R mapper like LINQ to SQL or Entity Framework, but you’re directly using ADO.NET to execute raw SQL commands.  It’s not something that I would personally do, but there are a lot of folks who prefer this over the higher level data access layers. So let’s look at an example...


  • Using C# dynamic to call static members

    By now, you’ve probably heard that C# 4.0 is adding support for the dynamic keyword, which introduces some aspects of dynamic languages to C#.  I had not had a chance to really try it, but recently I was reading Bertrand Le Roy’s post on the topic, and was sort of looking for a good opportunity to use it. Today, I found a scenario which I thought it would work great for, but it turned out not to be supported out of the box! The scenario is to call static class members using dynamic.  That probably sounds crazy, so let’s look at an example.  Say you have these two classes: public class Foo1 { public static string TransformString(string s) { return s.ToLower(); } public static string MyConstant { get { return "Constant from...


  • Fun with C# 4.0’s dynamic

    There’s been some debate recently on the new “dynamic” keyword in C# 4.0 . As has been the case with many features before it, some love it, some hate it, some say it bloats the language, yadda yadda yadda. People said that about lambdas. Me, I’ll just use it where I see a use case, thank you very much. In the case of dynamic, another frequent comment is that a statically-typed language should not try to look like a dynamic language. Well, I just don’t believe in that distinction. Being dynamic is a trait that a language can have, and some have it more than others. But as soon as a language has a dictionary type or indexers, and most modern languages do, it starts having dynamicity. What people call a dynamic language is just one where it’s the...


  • 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...


  • Twitter contest: what can you code in 130 characters?

    I just posted the following snippet on Twitter. The exercise is to write meaningful and preferably cool code that fits in a Twitter message along with the #twitcode keyword, which leaves 130 characters. private static readonly byte [] _blankGif = Convert .FromBase64String( "R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAEBMgA7" ); I can’t wait to see what people come up with. Monitor the results from here: http://twitter.com/search?q=%23twitcode or from the Atom feed: http://search.twitter.com/search.atom?q=%23twitcode Read More...


  • Some ASP.NET compiler black magic

    In the work we’ve been doing with Rob on the Kona commerce app , our quest for extreme pluggability has led us to look at quite a few interesting features of ASP.NET compilation. Features I didn’t know about before Dmitry and David pointed them out for me. I thought I’d share… It starts with the <%@ Assembly src= %> and <%@ Reference virtualpath= %> directives which you may have seen show up in IntelliSense when building a page. But what are they doing exactly and what differentiates them? They both enable you to reference code that is in a different file in the site. With both of them, you get full IntelliSense on the referenced code, but they don’t reference the same kinds of files. @Reference is meant to reference a specific class...


  • Free C# and VB Coding Standards Reference Documents

    Clint Edmonson has gifted us for the holidays with free coding standards documents for C# and VB. [ GET THEM HERE ] Read More...


  • AutoFieldGenerators

    In 3.5 SP1 we added new properties to GridView and DetailsView which allows the page developer to change the way AutoGenerateColumns creates its columns. This feature is well know in Dynamic Data , but it is not tied to this technology. Dynamic Data takes advantage of this by looking at the meta data that users set on properties to generate columns. You too can roll your own IAutoFieldGenerator. Lets look at the interface: public interface IAutoFieldGenerator { ICollection GenerateFields( Control control); } The interface itself is pretty weird but it gets the job done. GenerateFields takes the control that we're generating the fields for, and expects to get some ICollection of stuff back. If we had the chance to redo this interface we'd...


  • Dynamic Sorting with Linq

    When trying to implement a Business Logic Layer (i will refer to this as BLL ) with linq one thing that is an annoyance is sorting. Lets say you had a BLL that was being used by ObjectDataSource and had a select method that does sorting and paging, then you'd probably be tempted to write something like this: private IQueryable < Product > SortBy( IQueryable < Product > source, string sortBy) { int desc = sortBy.IndexOf( "DESC" ); bool isDescending = desc >= 0; if (isDescending) { sortBy = sortBy.Substring(0, desc).Trim(); } switch (sortBy) { case "ProductName" : if (isDescending) { return source.OrderByDescending(p => p.ProductName); } source = source.OrderBy(p => p.ProductName); break ; case "UnitPrice"...


Page 1 of 4 (33 items) 1 2 3 4 Next >
Microsoft Communities