Posts from  January 2014


1.30.2014

Disable encoding with custom html builders in Razor without using IHtmlString

If you use a custom html builder, not marked with IHtmlString, and don't want to use the verbose @Html.Raw(builder) and would rather just use @builder, see this gist for how to set this up:


1.13.2014

Building a mongodb provider for the new ASP.NET Identity framework - Part 1

Microsoft is hard at work on a new ASP.NET Identity framework. I thought it would be fantastic to start working on a mongodb provider. In a series of posts I'll discuss adapting the framework to work with mongodb, the decisions I made and hopefully spark some interest and get some feedback.

I'm not going to spend a lot of time re-hashing the details of the framework, instead here are some links for background:

I setup a nuget package: Install-Package AspNet.Identity.MongoDB -Pre

Designing IdentityUser

One of the first things we need is a model of a user, IUser is the interface we must implement.

Naming it

The first decision I had to make was what to name this thing? I decided to follow the Entity Framework implementation's IdentityUser to make it easier for users to get up and running with the mongo implementation.

String v ObjectId in .net

In the latest pre-release of Microsoft.AspNet.Identity.Core 2.0.0-alpha1, the IUser interface has been updated to support a TKey generic parameter. Prior versions used a string for the user's Id. We could go ahead and use ObjectId for TKey, but to be honest, working with strings in .net applications is often easier, especially when model binding and serializing/deserializing objects. I'm going to stick with string for TKey:

public class IdentityUser : IUser<string>
{
    public string Id { get; private set; }

    public string UserName { get; set; }
}

ObjectId representation in mongo

Even though we're using strings in our application, I want the Id to be stored as an ObjectId. Mongo is optimized to deal with ObjectId and it's much more familiar to people who work with mongodb. To do this I decorated the Id property with a BsonRepresentation attribute:

public class IdentityUser : IUser<string>
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; private set; }

    public string UserName { get; set; }
}

Distributed Id generation

The client driver will assign an id if the Id is not set on an insert, but I wanted to save developers the time of waiting until after an insert to get the Id. In the spirit of decentralized creation of ids which is one of many reasons to use a doc db, I setup the IdentityUser type to generate an id whenever it's created. During de-serialization from mongo, this will be overwritten with an actual id when loading existing user documents.

public class IdentityUser : IUser<string>
{
    public IdentityUser()
    {
        Id = ObjectId.GenerateNewId().ToString();
    }

    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; private set; }

    public string UserName { get; set; }
}

Implementing IUserStore

The first service interface to implement in the new identity framework is IUserStore. This provides basic CRUD operations on user storage. In the spirit of keeping with EF naming, I named this UserStore. I created an IdentityContext to decide where users are stored, this may change as I develop the provider, let me know if you have ideas. There are lots of tests of this in the repository, I wrote the majority as integration tests around the UserManager type in the identity framework. UserManager orchestrates using the different identity service interfaces, like IUserStore, to provide a facade to consuming applications and frameworks. Anyways, here is the UserStore:

public class UserStore<TUser> : IUserStore<TUser>
    where TUser : IdentityUser
{
    private readonly IdentityContext _Context;

    public UserStore(IdentityContext context)
    {
        _Context = context;
    }

    public void Dispose()
    {
        // no need to dispose of anything, mongodb handles connection pooling automatically
    }

    public Task CreateAsync(TUser user)
    {
        return Task.Run(() => _Context.Users.Insert(user));
    }

    public Task UpdateAsync(TUser user)
    {
        return Task.Run(() => _Context.Users.Save(user));
    }

    public Task DeleteAsync(TUser user)
    {
        var remove = Query<TUser>.EQ(u => u.Id, user.Id);
        return Task.Run(() => _Context.Users.Remove(remove));
    }

    public Task<TUser> FindByIdAsync(string userId)
    {
        return Task.Run(() => _Context.Users.FindOneByIdAs<TUser>(ObjectId.Parse(userId)));
    }

    public Task<TUser> FindByNameAsync(string userName)
    {
        var byName = Query<TUser>.EQ(u => u.UserName, userName);
        return Task.Run(() => _Context.Users.FindOneAs<TUser>(byName));
    }
}

And here is the IdentityContext:

public class IdentityContext
{
    public MongoCollection Users { get; private set; }

    public IdentityContext(MongoCollection users)
    {
        Users = users;
        EnsureUniqueIndexOnUserName(users);
    }

    private void EnsureUniqueIndexOnUserName(MongoCollection users)
    {
        var userName = new IndexKeysBuilder().Ascending("UserName");
        var unique = new IndexOptionsBuilder().SetUnique(true);
        users.EnsureIndex(userName, unique);
    }
}

The consuming application passes a MongoCollection instance to store users. MongoCollection is thread safe so we're ok to share a single instance of it. The IdentityContext then ensures there is a unique index on the UserName field so we don't get duplicate users! This will create the index if it doesn't exist. This functionality may move at some point, but this seemed like a good place to start.

Extending IdentityUser

Part of the purpose of the new identity framework is to allow users to tack on their own data in a strongly typed fashion. This is where mongo shines, I added tests in EnsureWeCanExtendIdentityUserTests to verify this works, here's how one would do this:

public class ExtendedIdentityUser : IdentityUser
{
    public string MyUserField { get; set; }

    public void MyUserBehavior(){ ... }
}

And to use it:

var context = new IdentityContext(Users);
var userStore = new UserStore<ExtendedIdentityUser>(context);
var userManager = new UserManager<ExtendedIdentityUser>(userStore);

Next

Let me know what you think of the decisions so far. In subsequent posts I'll show how to implement more of the service interfaces and talk about the decisions I encounter. Check out the repository on github, it's full of unit and integration tests.

1.10.2014

C# async/await makes reactive testing expressive!

Isolating asynchronous behavior isn't always possible, especially when it comes to creating integration tests. However, with the new async/await features in c# 5, testing reactive interfaces is a breeze and can be very expressive.

FileSystemWatcher and events

FileSystemWatcher is commonly used to monitor for changes to a directory. It's been around since .Net 1.1. Whenever I work with event based interfaces, I prefer to wrap them with an Observable. Testing this often requires an integration test. Here's some code to convert the FileSystemWatcher.Changed event to an observable:

IObservable<FileSystemEventArgs> Changed = Observable
    .FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h => Watcher.Changed += h, h => Watcher.Changed -= h)
    .Select(x => x.EventArgs);

I'm using this to create an ObservableFileSystemWatcher adapter. I'll be referring to this in the following tests.

The following code is captured in this gist, look at the revision history to see the changes between each of the following samples.

Designing a test

Historically, testing asynchronous behavior required blocking calls and hackery to capture information for our assertions. Here's how we might be tempted to start testing the observable wrapper:

[Test]
public void WriteToFile_StreamsChanged()
{
    using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; }))
    {
        FileSystemEventArgs changed = null;
        watcher.Changed.Subscribe(c =>
        {
            changed = c;
        });
        watcher.Start();

        File.WriteAllText(Path.Combine(TempPath, "Changed.Txt"), "foo");

        Expect(changed.ChangeType, Is.EqualTo(WatcherChangeTypes.Changed));
        Expect(changed.Name, Is.EqualTo("Changed.Txt"));
    }
}

To test the Changed observable, we Subscribe to Changed and then capture the last result into a local changed variable. This way we can run assertions on the changed notification. Next, we Start the watcher, create a new file, and verify we get a notification.

However, when we run this test, it fails. The file notification is asynchronous and thus non-deterministic. We don't know if it will happen before or after our assertions are executed.

Waiting for the result

Historically, we'd have to modify this test to wait for the changed notification. We could use a ManualResetEvent to wait:

[Test]
[Timeout(2000)]
public void WriteToFile_StreamsChanged()
{
    using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; }))
    {
        var reset = new ManualResetEvent(false);
        FileSystemEventArgs changed = null;
        watcher.Changed.Subscribe(c =>
        {
            changed = c;
            reset.Set();
        });
        watcher.Start();

        File.WriteAllText(Path.Combine(TempPath, "Changed.Txt"), "foo");

        reset.WaitOne();
        Expect(changed.ChangeType, Is.EqualTo(WatcherChangeTypes.Changed));
        Expect(changed.Name, Is.EqualTo("Changed.Txt"));
    }
}

The reset will block the test at the call to WaitOne just before our assertions. When the changed notification happens, Set will be called in our subscriber and the test will complete. To be safe, the test has also been modified to Timeout after 2 seconds.

Now our test works, but it's not pretty :(

Making it expressive with async/await

Thanks to the new c# 5 async/await language features, we can fix this. Here's the new way we can write this test:

[Test]
[Timeout(2000)]
public async Task WriteToFile_StreamsChanged()
{
    using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; }))
    {
        var firstChanged = watcher.Changed.FirstAsync().ToTask();
        watcher.Start();

        File.WriteAllText(Path.Combine(TempPath, "Changed.Txt"), "foo");

        var changed = await firstChanged;
        Expect(changed.ChangeType, Is.EqualTo(WatcherChangeTypes.Changed));
        Expect(changed.Name, Is.EqualTo("Changed.Txt"));
    }
}

Observables are awaitable, which means we can wait (non-blocking) for the next item. In this case, we can use FirstAsync().ToTask() to create a Task, that upon completion, will contain the first result of the observable sequence. This task is named firstChanged above. This task could complete at any time. Of course in this case, nothing will happen until we create the test file. We're free to continue setting up the test as we've already indicated the desire to capture the first result!

Next, we Start the watcher, create the test file and then the magic, we use await to wait for the result of our firstChanged task. Once the task is complete, the changed notification will be captured into our changed variable. I love how readable this is 'changed equals wait for the first changed'! Once the first changed notification arrives, the assertions will run and the test will complete.

Strive for Expressive Tests

The historical strategy works, but the new style is more compact and doesn't require so much brain power to understand. This new style makes this test much easier to write and much easier to maintain. Try this out and see how it improves your own testing, let me know what you come up with!

1.4.2014

Refactoring: Pushing Behavior Into The Domain

"Reduce duplication, bugs and shotgun surgery, all while increasing testability by pushing behavior into the domain!"