NuGet Package: FluentValidation

I came across a pretty neat nuget package that may very well be my go-to validation logic from now on. The package is called FluentValidation and seems to be a very flexible and usable fluent approach to object validation. There's not much else to say about it other than go check it out and give it a spin. There is a quick example on the github page, but the api is very discoverable. I made a quick LinqPad script to play with it, and I like what I am seeing so far.

A small validation library for .NET that uses a fluent interface and lambda expressions for building validation rules.

read more

DataReader Extension GetNullable

It has always been a minor pain that fields in a database can be null while the same value type in .Net cannot be. If you wanted to read a nullable DateTime field in the database into your object's DateTime property, the only choice was to check it for null first then read the field if not null.


if (!dr.IsDBNull(3))
    obj.Date = dr.GetDateTime(3);

I like to use extension methods to prevent the need to lookup ordinals every time I read from the data reader. I figured why not go ahead and write nullable extension methods for value types as well.


obj.Date = dr.GetNullableDateTime("datefield").GetValueOrDefault();

This approach would work, but it requires an extension method for every value type which will be read from the database. Instead, it would be nice to only have to write the logic once and be able to reuse it for every value type.

To do this, I wrote an extension method using generics and a predicate to check for null then read the value if not null. Note that the column name is passed to this method but the predicate takes the ordinal of the column. This is simply to prevent an extra wasted ordinal lookup inside the predicate.


public static T? GetNullable<T>(this IDataReader source, string column, Func<int, T> predicate) where T : struct
{
    int index = source.GetOrdinal(column);
    if (source.IsDBNull(index))
        return null;
    else
        return predicate(index);
}

Now the usage of this method is just as clean as the previous approach, but it lends itself to every value type. Additionally, if the property being set was nullable already then you could leave off the GetValueOrDefault() method call.


obj.IntValue = dr.GetNullable("intvalue", dr.GetInt32).GetValueOrDefault();
obj.Date = dr.GetNullable("datefield", dr.GetDateTime).GetValueOrDefault();

read more

Fixed: Visual Studio Fails to Start

I have had a recurring problem on my PC for a while where after some period of time Visual Studio will fail to start and show a generic error message box. I have multiple versions of Visual Studio and all would fail to load, none telling me exactly what the problem was. I experienced a similar problem trying to start SQL Server Management Studio. Every time this happened, the only thing that could fix it was a reboot. I had tried a few times to search online for similar problems with Visual Studio, but could find nothing useful. Not knowing where to start in troubleshooting, I settled with rebooting occasionally.

read more

Concurrent Operations with Reactive Extensions

Today I encountered a scenario which I could describe with words much better than I could describe with code. I was reading results from a REST API, reading a few thousand records over multiple requests. I was saving these records to a database and didn't want to wait until all records were pulled before saving, so I implemented some LINQ magic to enumerate the items in between each HTTP request.

The basic idea is this:
Request first batch -> Save batch -> Request next batch -> Save batch (repeat until no more records)

This can be achieved with LINQ through the magic of deferred execution.

IEnumerable<IList<Record>> recordBatches = GetBatches();
IEnumerable<Record> records = recordBatches.SelectMany(x => x);
records.Select(x => Save(x)).ToList();

The code was fairly straightforward and worked well, but I kept feeling like I should be making the next HTTP request at the same time that I was enumerating the previous results and saving to the database. Saying that I wanted to do this work in parallel was much easier than actually writing the code to do so.

My previous experience with trying to make multiple things happen concurrently was to use the Task framework with async/await. I started down this path but quickly realized I didn't have a good way to put the results from each HTTP request into my enumerable. I tried to use deferred execution with yield return but you can't do that from an async method. I then tried to use LINQ to aggregate all the task operation results, but I kept running into needing to wait on the task before I could return the results which defeated the whole purpose. While researching online to see if anyone had a solution to such a situation, I discovered a whole new way of thinking about collections and this kind of operation. I discovered the Reactive Extensions and IObservable.

I will admit that I had heard of IObservable before, but I never really knew what benefits it provided or why one would use it. It took me a few minutes of reading and thinking before I was able to mentally apply it to my situation, but once I got it figured out I realized it was exactly what I needed. It really turns on its head the way you are used to thinking about collections and enumerating data.

Instead of populating a list or defining an enumerable then returning it, Observable allows you to return an object that will later be populated asynchronously. The async delegate given when creating the Observable will push items into it as they are available. This means you can separate the loading of your data from the processing and even do it in parallel!

Concurrently:
1. Request first batch -> Request next batch (repeat until no more records)
2. Enumerate batches observable, saving each batch as it becomes available

public IObservable<IList<Record>> GetRecordBatches()
{
    return Observable.Create<List<Record>>(async obs =>
    {
        //Loop to read issues from the api 
        //then push them onto the observable
        while (!done)
        {
            var records = await QueryRecords();
            obs.OnNext(records);
        }
    });
}

When the consumer of the IObservable object tries to enumerate it, it will enumerate as far as it can, then block until the next item is available. The Reactive Extensions even have the ability to return an IObservable as an IEnumerable which can help integrate it into existing code without having to change many method signatures. The code from the first example could still work with the IObservable.ToEnumerable().

IEnumerable<IList<Record>> recordBatches = GetRecordBatches().ToEnumerable();
foreach (var batch in recordBatches)
{
    SaveBatch(batch);
}

read more

Disposing of Interfaces

Sometimes you run across situations where there is just no good solution. No matter which option you choose, there are downsides. I found one such situation today when trying to design an interface. The code below are simple examples I wrote for the purposes of this post.


public interface INotifier
{
    void Notify(string message);
}

I had written my initial implementation, which happened to use a resource that needed disposing.


public class FileNotifier : INotifier, IDisposable
{
    private FileStream stream;

    public FileNotifier(string path)
    {
        this.stream = File.OpenWrite(path);
    }

    public void Notify(string message)
    {
        //use the stream
    }

    public void Dispose()
    {
        if (stream != null)
        {
            stream.Dispose();
            stream = null;
        }
    }
}    

Of course I went to put this object in a using block only to realize that the interface through which I using this object was not disposable, so it would not compile.


//Will not compile because INotifier is not IDisposable
using (INotifier notifier = new FileNotifier(@"C:\notify.txt"))
{
    notifier.Notify("example");
}

read more

WaitTimeout – A Solution For Waiting

I recently found myself needing to periodically check the status of another operation, waiting for it to finish. In pseudocode, the basic operation is similar to this:


while (!complete)
{
    //check the status
    if ([completed])
        complete = true;
    else
        //sleep 500ms
}

I did not want to wait forever, though, so I wanted to implement a timeout. I could have used a timestamp, then checked the elapsed time with each iteration, but I wondered if there was a more reusable approach. I decided to write my own class to handle this admittedly simple situation.


public sealed class WaitTimeout : IDisposable
{
    private Stopwatch sw;
    private TimeSpan timeout;

    private WaitTimeout(TimeSpan timeout)
    {
        this.timeout = timeout;
        this.sw = new Stopwatch();
        sw.Start();
    }

    public static WaitTimeout Start(TimeSpan timeout)
    {
        WaitTimeout wt = new WaitTimeout(timeout);
        return wt;
    }

    public bool TimedOut
    {
        get
        {
            if (this.sw == null)
                throw new ObjectDisposedException(this.GetType().Name);
            return sw.Elapsed >= this.timeout;
        }
    }

    public void Dispose()
    {
        if (this.sw != null)
        {
            this.sw.Stop();
            this.sw = null;
        }
    }
}

read more

Finding Versions of mscordacwks.dll

In my post on Debugging Different SOS Versions With WinDBG, I wrote how you often will need a different version of mscordacwks.dll, but I skipped over the part about how to obtain the version you need. Of course the natural answer is to get it off the machine from which the dump file was obtained. Unfortunately, that is often easier said than done. I have previously looked online to try to find copies with little luck, but today I ran across a site that attempts to index every version of the file.

read more