I’ve been playing around with the word press features and wanted to post some source code. For whatever reason it didnt want to format things the way that I wanted and after an hour or so of googling, I found that they’ve implemented a pretty sweet code formatting feature. To give this a whirl take a look at the code below:
using System;
namespace MagicWebsiteAPI {
/// <summary>
/// Provides an interface that specifies a primary key or unique identifier for an object when stored in a storage medium such as a database or file.
/// </summary>
/// <typeparam name="TKey">The generic type that represents the type of data that the key is.</typeparam>
public interface IKeyed<TKey> where TKey : struct {
/// <summary>
/// Gets a value that uniquely identifies an instance of the object in a storage medium.
/// </summary>
TKey? PrimaryKey { get; }
}
}
What you’re seeing is an interface that I’ll commonly use in applications that I design. The interface defines a nullable generic property named PrimaryKey that is typically used to track an objects primary key value in a database. Its designed as nullable to support new objects that most likely do not have a primary key value assigned to them until after they are persisted to a database.
Additionally, the property is marked with only a get accessor. Typically I design my objects with static factory methods for object creation (ie, retrieving from the database) which allows me to set the underlying field value without exposing a public set accessor of the property. I’ll write more about this design pattern in another blog on another day.
Anyway, take a look at the original post that lead me to my first blog entry.