posts - 19 , comments - 8 , trackbacks - 0

December 2014 Entries

A timeout dictionary in C#
Overview My use case was a authentication system which stored user details. Fetching this information was expensive, so it was decided to only do this if the record was older than 15 minutes. The following code implements a dictionary were the values "timeout" after a given time period. The timeout dictionary The following code implements the dictionary. The constructor takes a date time provider which you can find in a previous post. This allows me to pass in a faster implementation of DateTime, ......

Posted On Friday, December 19, 2014 9:17 AM | Comments (1) | Filed Under [ C# TimeoutDictionary ]

Testing code with time dependency in C#
Introduction Testing code that is date or time dependent is clearly problematic! The first step is to create an interface which we will use instead of accessing the DateTime object itself. The interface using System; namespace JetBlack.Common.Timers { public interface IDateTimeProvider { DateTime Today { get; } DateTime Now { get; } long Ticks { get; } } } A native date time provider We can wrap up the standard date time object with the following code. using System; namespace JetBlack.Common.Timers ......

Posted On Thursday, December 18, 2014 4:55 PM | Comments (0) | Filed Under [ C# DateTime Testing ]

Array pooling in C#
Introduction The following class reduces garbage collection by maintaining a pool of allocated arrays. DisposableValue I wanted a simple way to manage the lifetime of the arrays. To do this I have borrowed an idea from reactive extensions using System; namespace JetBlack.Common { public class DisposableValue<T> : IDisposable { private readonly Action _dispose; public DisposableValue(T value, Action dispose) { _dispose = dispose; Value = value; } public T Value { get; private set; } public void ......

Posted On Thursday, December 18, 2014 3:57 PM | Comments (1) | Filed Under [ C# array ArrayPool ]

A Generic Assert Class for C#
Overview The following code provides a mechanism for creating asserts which throw a generic exception. My initial motivation was to express in a more concise way common argument and result checking code. For example the following code: public string Read(Stream stream, Encoding encoding, int count) { if (stream == null) throw new ArgumentNullException("stre... "The stream must be created."); if (encoding == null) throw new ArgumentNullException("enco... "The encoding must be specified."); if ......

Posted On Thursday, December 4, 2014 1:25 PM | Comments (0) | Filed Under [ C# Assert Generic ]

Powered by: