September 2009 Blog Posts
Tuesday, September 22, 2009 12:01 AM | Comments

This post was spawned as I was reviewing the latest Oxite source code on CodePlex. I’ve been reading any source code I could get my hands on lately looking for inspiration to cure my “winter coding boredom.” While it isn’t winter yet, it is fast approaching, which means less outdoors time and more computer time. I try to blog more, update my open source projects with a new release, and with any luck begin a new project worth getting passionate about. Today I'm going to hit the ground running with a short tip, again, on the yield keyword. The following code is an excerpt from the Oxite Blog class. Blog as it turns out, is a Cacheable Entity (ICacheEntity) where the designer of the interface determined that cached entities can specify any dependencies they may have. Clearly, the developer of the ICacheEntity interface understood iterators and realized that the consumer of...

Tuesday, September 8, 2009 10:59 PM | Comments

Today I began integration testing my SqlRepository while I experiment with an MVC prototype. I tried googling for a simple base class for integration testing a LINQ to SQL DataContext that would provide automatic rollback between each test, but after (minimal) searching, decided to simply write my own. Perhaps this will help someone. This base class was written for MS Test, but simply renaming the attributes accordingly would yield the same results on nUnit, xUnit, etc. The Code Simply inherit your test class from this base class and any changes done via the internal Context property will be automatically rolled back after each test runs. /// <summary> /// The base class will allow LINQ to SQL integration testing with automatic rollbacks after each test /// </summary> /// <typeparam name="TContext">The type of data context to use</typeparam> public abstract class RolledBackDataContextTests<TContext> where TContext : DataContext { private TContext _context; ...

Monday, September 7, 2009 9:55 PM | Comments

This was one of the first extension methods I wrote when C# 3.0 arrived, since I just stumbled across it on an older project I figured I might as well post it, if for nothing else but future reference. One downside to be aware of if you choose to use this method, is that it will not project the results on a IQueryable provider like LINQ to SQL, so please be aware the conversion will be done on the client side. The Code public static class DelimitedStringExtensions { public static string DefaultDelimiter = ", "; /// <summary> /// Convert a sequence of items to a delimited string. By default, ToString() will be called on each item in the sequence to formulate the result. The default delimiter of ', ' will be used /// </summary> public static string...

Friday, September 4, 2009 2:14 PM | Comments

LINQ and Expression Trees have enabled a ton of possibilities for us as developers, particularly in establishing some code-reducing convention based querying. I have found myself playing with Expressions in C# since their inception and will hopefully be publishing a few other posts I have queued up. For now, this post will simply demonstrate how to take advantage of a popular convention whereby each database table always has a single integer primary key named “Id” By taking advantage of this primary key convention I can save myself a ton of boilerplate code to query a table by ID. The Extension Method public static T GetById<T>(this IQueryable<T> query, int id) where T : class { // Create lambda parameter x of type T var param = Expression.Parameter(typeof(T), "x"); // Given x param, extract the Id property var...