Problem solve Get help with specific problems with your technologies, process and projects.

Explore the utility of .NET coding patterns

Design patterns assist anyone working in an object-oriented environment. Here Ed Tittel describes 15 coding patterns from The Code Project written specifically for .NET developers.

In the old-fashioned variety of architecture -- the kind that applies to buildings, houses and other physical structures -- Christoper Alexander's famous books The Timeless Way of Building (1979) and A Pattern Language (1977) helped to revolutionize that field. Their fundamental insight was that certain kinds of structural usages or arrangement recur repeatedly in the most workable and successful structural designs. These are known as "design patterns" and represent a compressed type of best design practice that all wise architects incorporate into their own work.

Erich Gamma, Richard Helm, Ralph Johnson, and John Vissides took this idea and ran it deep into the software realm with the publication of their own seminal work Design Patterns: Elements of Reusable Object-Oriented Software in 1995. In the intervening 11 years since its publication, design patterns books for programs have proliferated wildly -- a quick search at Amazon on software titles that include "Design Patterns" in their names produces 61 hits, of which more than 40 are unique.

Suffice it to say that design patterns is an idea whose time has come, and whose application serves developers well in all kinds of object-oriented environments, including .NET and its related programming languages. That probably explains why six titles from the aforementioned list target .NET specifically (visit Amazon and search on ".NET design patterns" to see them for yourself). It's probably no accident that three of these books tackle VB.NET, two deal with general .NET design patterns, and the last with C#.

There's another online resource that fits into this picture as well. It's the Code Project's Commonly Used .NET Coding Patterns pages. Free to anyone who cares to stop by and inspect its contents, this site makes an interesting and convincing demonstration of the power and utility of the design pattern concept. It works with the .NET Code Document Object Model, aka CodeDom, which is of particular interest to those working with C# and ASP.NET.

There, you'll find the following patterns described and exemplified (with links to each one for easy reference or access):

  • The BeginProcess/EndProcess pattern: This is used to start and stop, or to begin and end, process initialization in the .NET environment.
  • The Cursor Lock pattern: This is used to lock the Cursor property within a Windows Form.
  • The Delegate pattern: This is used to work with event handlers in the Common Language Runtime (CLR) where delegates are created using object and EventArgs, including a custom-crafted EventArgs class.
  • The Event pattern: This is a pattern from System.Windows.Forms where events require special invocation methods.
  • The Flags pattern: This recurs throughout the CLR, where enum is flagged using FlagsAttribute, especially to allocate values.
  • The For Each pattern: Built into C#, but not native, this pattern iterates over any collection that implements the System.IEnumberable interface.
  • The Get Property/Field pattern: This recurs throughout the CLR, whenever a private field is exposed using a property with a get accessor.
  • The Is Instance Of pattern: Though this is built into C# as the is keyword, it's not implemented in CodeDom. The pattern determines if an object's type implements, inherits, or matches the queried type.
  • The Lock pattern: Another pattern built into C#; this locks a resource using the System.Threading.Monitor class.
  • The Observer pattern: This often-used code pattern lets subscribers receive notification of any change to a monitored value, using an event to announce the change.
  • The Singleton pattern: This permits a class to have only a single instance in an application. It's thread-safe, and can follow a lazy- or pre-loading implementation.
  • The Using pattern: Also one built into C#; this uses a resource that implements the System.IDisposable interface, and releases it when finished using it.
  • Binary Operator patterns: This extends normal CodeBinaryOperatorExpression to add the additional operator BooleanExlusiveOr.
  • Unary Operator patterns: This extends normal CodeBinaryOperatorExpression to add unary operators BooleanNot and BooleanIs.
  • Compound Assignment patterns: This extends normal CodeAssignStatement to add the following operators: Add, Subtract, Multiply, Divide, Modulus, BitwiseAnd, and BitwiseOr.

    Those interested in learning more about design patterns are advised to check out the general Design Patterns software book cited earlier here, or perhaps to dig into the general or C# .NET books that are easy to find on Amazon. Those ready to dig in will find useful ammunition at the Code Project's Web site.

    Ed Tittel is a writer and trainer whose interests include XML and development topics, along with IT Certification and information security. E-mail with comments, questions, or suggested topics or tools to review. Cool tools rule!
  • Dig Deeper on .NET Architecture Best Practices

    Start the conversation

    Send me notifications when other members comment.

    Please create a username to comment.