Archive for category .Net

C# Acrobatics : Lambdas and Expression Methods as a replacement for NVelocity

I’ve been very quiet recently. (I’m trying to not be so loud, Scott. 🙂 ) You see, I’ve been writing a lot of ASP.Net code for a site I’m working on. And, to be honest, I’ve been having a lot of trouble. The source code for .Net has been very helpful, and I’ve learnt a lot about what’s going on under the covers of ASP.Net because of it.

(Note: I won’t comment here on the quality of the code I’ve found – I’ll leave that up to you to judge. But in any case, I’ve been trying to build on top of it.)

One thing I’ve found to be important is the reliance on Web Controls. (It’s got something to do with javascript libraries, but that’s another story.) Getting away from the "standard" way to do ASP.Net isn’t easy though. Even the ninjas on the ASP.Net MVC team seem to be having trouble. However, with the magic of lambdas and extension methods in C#, I think I might have just about managed to get something usable. I thought I’d publish my work here, and see what comments I got.

I think it’s best to start with what my ASP.Net code looks like once I’ve got everything working. (Notice I still have some Web Controls in there, but that’s because I’ve not worked out how to do sorting of data without web controls yet.)

The inspiration for this was taken from the improvements made to NVelocity by the gurus on the Castle Project. I thought it looked great, and I’d like something similar, but I didn’t really want to learn a whole new scripting language and integrate it into my working environment just for rendering a bit of HTML. So I built some C# classes to do a similar thing for me instead. It’s not as nice as NVelocity, but it’s okay for now.

Warning: The following code may contain statements of a disturbing nature to more sensitive readers. We cannot be held responsible for any confusion, delusion or mental illness caused by this code.

It starts by taking a collection of Task objects, and calling the extension method "ForEach" on them:

<% Tasks.ForEach(sections => {
   sections.NoData = tasks => {
%>
   <p>
   Hey, you've got nothing to do.</p>
   <p>
<%
   };
   sections.BeforeAll = tasks => {
   %>
  <table class="task-list">
    <tr class="task-list-header">
      <th>
        <asp:LinkButton runat="server" CommandName="Sort" CommandArgument="StartDate"
          Text="Started" />
      </th>
      <th>
        <asp:LinkButton runat="server" CommandName="Sort" CommandArgument="DueOn"
          Text="Due" />
      </th>
      <th>
        <asp:LinkButton runat="server" CommandName="Sort" CommandArgument="Priority"
          Text="Priority" />
      </th>
      <th>
        <asp:LinkButton CssClass="task-description" CommandName="Sort" CommandArgument="Title"
          Text="Description" runat="server" />
      </th>
<th> </th> </tr> <% }; sections.Before = task => { %> <tr class="<%= this.tableCssClasses.Next() %>"> <% }; sections.Each = task => { %> <td> <div class='calendar calendar-icon-<%= task.StartMonth %>'> <div class="calendar-day"> <%= task.StartDayOfMonth %></div> </div> </td> <td> <div class='calendar calendar-icon-<%= task.DueMonth %>'> <div class="calendar-day"> <%= task.DueDayOfMonth %></div> </div> </td> <td> <%= task.Priority %> </td> <td class="task-title"> <a href='<%= Href.For("~/Tasks/{0}/Show.aspx", task.ID) %>'><%= task.Title %></a> </td> <td> <asp:Button ID="Button1" runat="server" CssClass="button" CommandName="Delete" Text="Mark Done" /> </td> <% }; sections.After = task => { %> </tr> <% }; sections.AfterAll = task => { %> </table> <% }; }); %>

It might take a while to grasp what’s going on here. The code actually starts using an Extension method to IEnumerable that looks like this:

public static void ForEach<T>(this IEnumerable<T> enumerable, ForeachSectionSetter<T> sectionSetter) {
  if (enumerable != null) {

    if (sectionSetter != null) {
      ForeachSections<T> sections = new ForeachSections<T>();
      sectionSetter(sections);

      if (enumerable.Count() == 0) {
        if (sections.NoData != null)
          sections.NoData(enumerable);
        return;
      }

      if (sections.BeforeAll != null)
        sections.BeforeAll(enumerable);

      int itemIndex = 0;
      T previousItem = default(T);

      foreach (T item in enumerable) {
        if (sections.Before != null)
          sections.Before(item);
        if (itemIndex % 2 == 1 && sections.Odd != null)
          sections.Odd(item);
        if (itemIndex % 2 == 0 && sections.Even != null)
          sections.Even(item);
        if (itemIndex > 0 && sections.Between != null)
          sections.Between(previousItem, item);
        if (sections.Each != null)
          sections.Each(item);
        if (sections.After != null)
          sections.After(item);
        itemIndex++;
        previousItem = item;
      }
      if (sections.AfterAll != null)
        sections.AfterAll(enumerable);
    }
  }
}

The delegate ForEachSectionSetter is used by the calling method with a lambda expression. As a parameter it receives an ForeachSections object, which looks like this:

public class ForeachSections<T> {
  public Action<T> Each { get; set; }
  public Action<IEnumerable<T>> BeforeAll { get; set; }
  public Action<T> Before { get; set; }
  public Action<T,T> Between { get; set; }
  public Action<T> Odd { get; set; }
  public Action<T> Even { get; set; }
  public Action<T> After { get; set; }
  public Action<IEnumerable<T>> AfterAll { get; set; }
  public Action<IEnumerable<T>> NoData { get; set; }
}

The calling method gets the chance to set the properties of this class before it is returned to the constructor of the ForEach method for processing. And because each property is already preset to a default value (Null in this case), the constructor can use the ForeachSections object just like a set of default or optional parameters. The caller can simply set values to the properties it needs, and ignore the rest.

If I had tried this another way, using overloadable constructors, it would have led to multiple constructors with indistinguishable signatures. If I’d have used property initializers, I wouldn’t have been able to run the whole routine without requiring a second call to the object, which actually wasn’t possible.

Basically, I couldn’t think of another way to do it.

The properties of the ForeachSections object are all delegates too. That means that we can use them with lambdas, which gives us lambdas inside of a lambda. (Hmm, very confusing!)

So what do you think? Could you use something like this? Can you make it simpler? Leave me a comment if you can.

, , , , ,

No Comments

Easy Data-loading with LINQ-to-SQL and LINQ-to-XML

.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new "X"-types, you can make working with XML really easy.

VB.Net 9 takes it one step further, and lets you write XML in your code without strings.

"Hey Rich, that’s old news," I hear you say. "And who’s interested in VB today anyway?"

Well, apparently there are a lot of VB-er’s still out there. I am mainly a C# developer myself, but I found that VB was perfect for a problem I had recently -  loading of XML data into a SQL Server table.

Read the rest of this entry »

, , , , ,

2 Comments

Design Guidelines for LINQ

Have you wondered if and when you should use the new LINQ features in .Net 3.5?

Like, where should I put a new extension method? Should I use Func<T> or a custom delegate? How do I best implement a mix-in (extension methods on an interface)?

Well, Mircea Trofin has just published a new draft of some LINQ design guidelines. You might just find your answers there.

, ,

No Comments

Read Word Documents in a Web Browser

image I love this! Not because of the features, but because of the way it works.

Tim Sneath just blogged about TextGlow – an online Word docx file reader. The docx format is XML, and Silverlight 2 apps can use LINQ-to-XML to parse it and format it for display inside the browser.

Amazing!

You can read more about it on Tim’s blog.

No Comments

Mix 08 WPF Scheduling Application

imageMix 08 seems to be much more mature than ever before. In previous years there were a lot of ideas being spoken about; this year there are much more implementations of those ideas available to look at. It seems like a lot of people have been working on the new technologies over the past year or so. Hopefully that will lead to the ripening of many technologies (especially WPF) to a point where we can actually use them.

As an example, there is a scheduling application available for Mix. Unfortunately it only runs on Vista, but I captured a video and made a few screenshots for you.

This is the opening screen, with an agenda for the 4 days of the event:

image

(Click the image to make it larger.)

Read the rest of this entry »

, ,

No Comments

Mix 08 Online Presentations

My favorite conference is about to start: MIX 08.

It’s a great conference for Microsoft developers with an interest in the web. Last year they had some great talks about user experience and architecture. There’s always something interesting to learn.

Unfortunately I can’t be there (as usual), but all the sessions will be online. Tim Sneath just posted details of where and when you can watch them:

  • The keynote with Ray Ozzie, Scott Guthrie will be available live at 9:30am Pacific / 5:30pm GMT on three streams: 750kbps, 300kbps, 100kbps.
  • The breakout sessions and panels will be online within 24 hours of each session at MIX08 sessions.

No Comments

ExtensionMethod.net – An Extension Methods Database

While surfing around tonight, I came across ExtensionMethod.net, a database of useful Extension Methods for C# 3.0 and VB 9. I thought it might be useful, so I added a few of my own extension methods.

There aren’t many there yet, but there are one or two on there from Scott Guthrie.

Have you got any code you could put up there? You could be one of the first if you go now.

2 Comments

LINQ or DIE

I just read a fantastic quote about LINQ from the book Pro LINQ: Language Integrated Query in C# 2008 by Joseph C. Rattz, Jr.:

I prefer to think of LINQ as a data iteration engine, but perhaps Microsoft didn’t want a technology named DIE.

I think they already built the technology for such a product, and put it in Vista. 🙂

It’s a great book. The first LINQ book I’ve found with a really detailed explanation of LINQ-to-SQL and the SQLMetal generation tool.

, ,

No Comments

Remove and Sort Those Ugly “using-Statements”

Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There’s a lesser-known feature which I really appreciate though – the “Remove and Sort Usings” command in the C# editor.

You activate the command by placing your cursor over the using statements and clicking on the right mouse-button.

image

Read the rest of this entry »

, , , ,

3 Comments

Refactoring C# Series: Aggregation of IEnumerable

I was recently reading Programming Ruby: The Pragmatic Programmers’ Guide, Second Edition, and came across this piece of example Ruby code:

[1,3,5,7].inject(0) {|sum, element| sum+element} -> 16
[1,3,5,7].inject(1) {|product, element| product*element} -> 105

Inject is a method which acts on an array by aggregating or accumulating the values within that array. It loops through the array, and for every item in the array, it performs a function. It then saves the result for the next iteration of the loop and eventually returns the aggregated value.

In C# 1.0 you would probably write such a method like this:

int sum = 0;
int[] list = new int[] { 1, 3, 5, 7 };
foreach (int item in list)
{
  // Perform some function, then save the result
  sum = sum + item;
}

It’s a bit long-winded, and if you wanted to make it reusable, you’d have a hard time.

In C# 3.0, you can do it just like you can in Ruby.

Read the rest of this entry »

, , , ,

1 Comment