Archive for category Design

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

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:


(Click the image to make it larger.)

Read the rest of this entry »

, ,

No Comments

How to Update Data with LINQ-to-SQL

When learning LINQ-to-SQL, it’s not immediately obvious how to do an update. Querying is easy, and there are methods for inserting and deleting. Updating usually occurs by modifying an object already known to the DataContext and then calling SubmitChanges on the context.

var product = (from p in dataContext.Products
               where p.ProductID == 1
               select p).Single();

product.Name = "Richard's product";


It’s nice to see that MSDN documentation actually addresses the obvious arising question:

Q. Can I update table data without first querying the database?

A. Although LINQ to SQL does not have set-based update commands, you can use either of the following techniques to update without first querying:

  • Use ExecuteCommand to send SQL code.
  • Create a new instance of the object and initialize all the current values (fields) that affect the update. Then attach the object to the DataContext by using Attach and modify the field you want to change.

Read the rest of this entry »



ASP.Net MVC Corollary – What to do?

Dude! I got quoted! And by none other than Rob Conery of SubSonic fame.

It seems like my last post caused quite an unexpected stir. Thanks to both Rob and Scott for taking the time to answer me. I really appreciate it.
Read the rest of this entry »

, , ,

No Comments

Does the ASP.Net MVC Framework Frustrate You?

Silverlight, ASP.Net Ajax, LINQ, Astoria, ASP.Net MVC – there’s just tons of stuff coming out of Microsoft. And that’s not to mention the stuff people have seemingly forgotten about – ASP.Net Futures with IronPython (AWOL), Patterns and Practices Web Client Software Factory, WPF, WF, WCF and CardSpaces. Now the trend seems to be functional programming languages, especially with F#.

There’s just a bit too much for me.

What I really need is guidance. My customers don’t ask me what to they should use in 6 months; they ask me how to use what already exists. They don’t want “cool”; they want stability and consistency. And while the ball keeps moving, we can’t grab it.
Read the rest of this entry »

, , ,


Calculating the Fibonacci Sequence with C# 3.0

Scott Hanselman just posted his latest article in his weekly source code series. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons:

  1. I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it out. (Doh!)
  2. It makes me question what I thought about coding.

You see, I spend a lot of time refactoring. Sometimes too much. I have to ask myself why. It’s usually so that I can come back to the code later, and still understand what I was trying to do. Most of the time, conciser is better. But not always!

And that’s what I see in this post.

Read the rest of this entry »

, , ,


Using LinqPad to Create a Time-Selector Drop-Down List

I am really getting into LINQ now! I think it’s fantastic. I recently wanted to develop a quick drop-down list in ASP.Net which allows a user to select a time of day from a list. The times are 15 minutes apart, so the list would look like this:


… and so on.

Before LINQ, I would have done this with a for loop, like this:

List<string> times = new List<string>();
for (int hour = 0; hour < 24; hour++)
  for (int minute = 0; minute < 60; minute++)
    if (minute % 15 == 0)
         "{0:00}:{1:00}", hour, minute));

That’s not difficult, although it’s not so easy to understand. I would have to write a small console app or test to make sure I had done it correct though.

I thought this might be a good opportunity to use LinqPad. It’s a great tool. You can use it to test a LINQ statement in a live window, so I thought I’d give it a go.

First I needed a LINQ statement to test.

Read the rest of this entry »

, , ,


A simple Mix-in with C# 3.0

Heard of mix-ins? They’re an alternative to multiple inheritance, made popular recently by Ruby.

Basically, you can use them to “mix in” methods from an interface with their implementations into a class.

In Ruby you can do this by including a module in a class. In C#, you do it by implementing an interface and defining an extension method for the interface.

Here’s a simple example.

First, define the interface. In this case, it won’t have any special features, so the interface is empty. We’ll call it IDebug, as it is going to let us call a method to get details of the object it is implemented on.

public interface IDebug

After the interface is set, define a static class with an extension method for the interface. We’ll just define one method here, called “GetTypeInfo”.

public static class DebugExtensions
  public static string GetTypeInfo(this IDebug debug)
    return String.Format("{0} ({1}): {2}"
	, debug.GetType().Name
        , debug.GetHashCode().ToString()
        , debug.ToString());

The method returns the name of the class (not the interface) where the interface is implemented, plus a few extra bits of information.

Now implement a couple of classes which implement the interface.

class MyClass : IDebug
  public override string ToString()
    return “I am an instance of MyClass”;

class MyOtherClass : IDebug
  public override string ToString()
    return “I am an instance of MyOtherClass”;

Now, magically, the method “GetTypeInfo” is included with the class as an extension method.

In the method you call this from, you then need add a “using” declaration for the namespace of the extension class.

After you’ve done that you can call the method from the mix-in.

var myObj = new MyClass();
var myObj2 = new MyOtherClass();

The output of this is:

MyClass (7995840): I am an instance of MyClass
MyOtherClass (56251872): I am an instance of MyOtherClass

No Comments

Qualities of a .Net Application Design

I’m often asked to produce design documents for new applications. I can’t do this without discussing the advantages and disadvantages to each part of the design. A great way to do this is to focus on the desired qualities of the system you’re trying to build. Define the qualities you are trying to achieve, and design each part of the system to match those qualities as best as possible.

It helps to have a list of things to consider. Here’s a list of some of the qualities you need to address for a .typical Net application:

Development-Time Qualities

  • Modifiability – (Often called “flexibility“, and especially important for agile development.) How easily can the software be modified to additional requirements and changes? Can changes be localized to as little code as possible? Do public interfaces have to change? How are the parts of the application dependent on one another? Are the most-likely-to-change areas of the application depended upon to be stable? To what extent are concerns separated?
  • Reusability – Can units of the application be reused elsewhere? Does the code fit the “DRY” principle (Don’t Repeat Yourself)? Does the system make good use of existing standards? Reusing code from other frameworks and systems can help keep testing and maintenance down, but reduce modifiability.
  • Portability – Could the system be easily ported to other platforms? This is often not an issue for most enterprise applications, as business requirements typically change more frequently than the technical platforms, and the .Net framework takes care of most of the issues. For a web application, portability could also include browser compatibility. What happens if the users suddenly receive an upgrade to their browsers?
  • Testability – How easy is it to prove that the application functions correctly, and that the various parts of the application do what they are supposed to do in isolation of one another? Can the application be tested automatically? Can the application eventually be tested easily end-to-end as part of an integration test? Can the application be load-tested?
  • Buildability – Can the application be built on systems other than those in the development environment? How often can it be built? The more often it can be built, the better. What tools can be used to do the builds? Can they implement continuous integration by building at every check-in? Which third-party controls or components are used? What part do licenses for third-party products play in the ability to build centrally?
  • Maintainability – How easy is it to understand the software for people other than the original developers? How easy is it to correct defects? Is the system documented enough to make it understandable?
  • Debugability – How easy is it to find out where and why there is an error in the system? How easy is it to find what is going wrong in a live, productive system?
  • Extensibility – How easy is it to extend the functionality of the system beyond its original specification?

Runtime Qualities

  • Functionality – How well does the software help the users to do their work? Are there missing features which make the software useless? Does the software provide end-to-end support for a particular process?
  • Usability – How easy is it for users to understand and learn to use the software?  Is the interface intuitive? Is there a supportive help system? Does the user interface match the users’ needs? Are basic usability standards and conventions adhered to?
  • Performance – Does the system perform fast enough? How does it perform when multiple users are active? Where are the bottlenecks and latency in the system?
  • Concurrency – Is it possible for more than one user to use the system at a time? What is the performance impact of multiple sessions? Is state shared across the application, making the system unreliable?
  • Security – Does the system prevent unauthorized access or misuse? What happens if access is wrongly denied to a user? How easy can security be administrated? Does the STRIDE threat model show up any weaknesses? What is the potential outcome of a successful attack? 
  • Integrity – This can be related to security, but it also refers to the data integrity of the system. Is the data always kept in a consistent state, or is it sometimes possible to infer different states of the system because of inconsistencies in the system? Do transactions pass the ACID test?
  • Reliability – Can the users rely on the software? Will it always perform correctly? Is it still usable at peak usage periods?
  • Availability – How often and for how long is the system available for use? How do upgrades affect its availability? Is the system meant to be used across time-zones?
  • Scalability – How does the software cope with adding more users over time? Can the system cope with an increase in the volume of data? What happens if the users start to use the system more often? Must the software scale up or can it scale out?
  • Deployability – How easy is it to deploy the initial release of the software? How easy is it to release future releases? Does software need to be replaced, or just patched? How does the deployment affect scalability?
  • Ugradability – Can a new version be deployed without stopping the normal operation of the system? What must be changed or upgraded when a new version is released? What third-party software may be upgraded which would affect the software?
  • Correctness – Does the software do exactly what was specified?
  • Conceptual Integrity – How balanced, simple, elegant, and practical is the whole system? Is there a clear vision as to what the software should do? Is the design consistent?
  • User Responsiveness – Related to performance and usability; how does the user perceive the application to be responding? Does the system stop responding for long periods of time, especially when retrieving data? Does the latency of parts of the system lead to decreased usability? Does the software use multiple threads to improve responsiveness?
  • Interoperability – How easily can the system be used with other systems?
  • Robustness – How does the system react to abnormal conditions? Does it crash, or recover gracefully? Do error messages baffle users and decrease usability?


Software qualities must always be balanced against one another. There’s no point in having performance good enough for if it’s not needed, for example, especially if it makes maintenance harder. Each quality has a particular importance for the system being developed.

In general the highest priority qualities are:

  • Maintainability
  • Correctness
  • Reliability

Also important for .Net Object-Oriented design are:

  • Reusability
  • Extensibility

Most of what I focus on in my day-to-day work is focused on good maintainability and modifiability. Changes come along all the time, and the people paying for the software want them done fast. The faster you can adapt to their needs, the more they trust you, and the better your business relationship.