Jan 19 2012

Using Git with Subversion

Category: git | subversionMatt @ 14:52

Do you love Git?  Are you working on a project that’s using Subversion?  Well, did you know that Git actually integrates quite nicely with Subversion right out of the box?  I’ll show you what you need to know to get started with Git and Subversion, and I’ll show you the workflow I use for keeping my work in sync with everyone else on my team.

More...

Tags:

Jan 12 2012

AppHarbor Rocks. Seriously.

Category: Cloud | AppHarborMatt @ 16:02

You kids and your applications today.  Back in my day, we published our applications like real men!  We didn’t have these fancy, cloud-based services like Heroku and AppHarbor.  We couldn’t just type ‘git push origin’ and have our application magically show up online, ready to rock and roll.  We used to dread deploying our code because of all the hoops we would have to jump through.  Oh, how times have changed!

More...

Tags:

Jan 2 2012

SpecsFor 2.4 Released

Category: Matt @ 16:04

I just published a new version of SpecsFor to NuGet.  This version includes a couple of minor enhancements: parameterized context classes and a new Should extension method.

More...

Tags:

Jan 1 2012

Status Update on SpecsFor.Mvc

Category: SpecsFor | TestingMatt @ 15:04

Well, I missed my goal of having the 1.0 version of SpecsFor.Mvc available by the end of 2011, but it was not for lack of trying.  I’ve been dog-fooding it on a mobile web app (my first), and that’s resulted in a number of changes and improvements.  I’m quite pleased with how things are shaping up, but I’d like some feedback from someone who isn’t me on the changes.

More...

Tags:

Dec 26 2011

When Encryption Is Part Of Your Domain

Category: MiscMatt @ 16:25

We typically think of encryption as a cross-cutting concern or as a separate utility service that our applications use, not as something that’s an integral part of our application domain.  That way of thinking doesn’t always hold though.  In this short post, I’ll show you how I recently implemented support for encryption as a first-class citizen of my domain model. 

The Domain

Almost every application contains some element that needs to be encrypted or hashed (a user’s credentials, perhaps?).  In the past, I’ve never bothered encapsulating the encrypted value as its own separate data type.  I’ll still encapsulate the logic for encrypting or hashing in the domain, but I typically place that logic in the owning entity.  Here’s a User object from RageFeed as an example:

public class User
{
    public virtual Guid Id { get; set; }

    public virtual string Username { get; set; }

    public virtual string Email { get; set; }

    public virtual string PasswordSalt { get; set; }

    public virtual string PasswordHash { get; set; }

    ...snip...

    public virtual void SetPassword(string password)
    {
        GenerateNewSalt();

        PasswordHash = HashPassword(password);
    }

    public virtual bool IsThisTheUsersPassword(string password)
    {
        var hash = HashPassword(password);

        return hash == PasswordHash;
    }

    private string HashPassword(string password)
    {
        ...snip...
    }

    private void GenerateNewSalt()
    {
        ...snip...
    }

    ...snip...
}

There’s quite a bit of logic related to securely storing the user’s password in this redacted snippet.  While it’s fairly easy to test, it does make for a slightly less-cohesive domain model since there are now true business concerns mixed in with encryption concerns.  Since it was really only this one entity and applied only to the credentials, I decided at the time to leave the code in the User object instead of refactoring it to somewhere else. 

Encrypted Members as a First Class Citizen

On a recent application though, I found that I had several members that all needed to be encrypted.  I decided I would encapsulate the encrypted members as first-class data types instead of rolling the encryption responsibility into the owning entity.  The difference is subtle, but it greatly changes where behavior lives within the domain.  The entities with encrypted members become simpler and more cohesive since they no longer contain encryption-related logic.  That logic is now encapsulated separately from the owning entity and is far easier to reuse throughout the domain.

public class AccountDetails
{
    ...snip...

    public virtual EncryptedString ReallySecretNumber { get; private set; }
    
    public virtual EncryptedString AnotherReallySecretNumber { get; private set; }
    
    ...snip...
}

The actual data type doesn’t do much directly: 

public class EncryptedString
{
    public virtual string EncryptedValue { get; set; }

    public virtual string CertificateName { get; set; }

    public virtual string Key { get; set; }

    protected EncryptedString()
    {

    }

    public static EncryptedString Create(string value, IEncryptData encrypter)
    {
        var encryptionResult = encrypter.Encrypt(value);

        return new EncryptedString
        {
            EncryptedValue = encryptionResult.EncryptedText,
            CertificateName = encryptionResult.CertificateName,
            Key = encryptionResult.Key
        };
    }

    public virtual string GetDecryptedValue(IEncryptData encrypter)
    {
        return encrypter.Decrypt(EncryptedValue, CertificateName, Key);
    }
}

Instead, it depends on a separate service, IEncryptData, to provide the low-level encryption API it requires.  This keeps the domain decoupled from the low-level methods, making it easier to test, extend, and maintain in the future.

Which is Better?

As always, the answer is “it depends.”  The approach I took with RageFeed and Fail Tracker, where I embedded the logic within the owning entity, has never caused me issues.  However, I chose to go a different route as soon as I had multiple members that required encryption.  That feels like the right approach in this case. 

Tags:

Dec 22 2011

Inversion of Control Containers - Things Every Senior .NET Developer Should Know, Part 3

Category: ThingsYouShouldKnowMatt @ 12:47

If you tell me you’re a senior .NET developer, there’s a handful of assumptions I’m going to make about what you know.  The “Things Every Senior .NET Developer Should Know” series of posts will look at those things.  Today’s topic will cover Dependency Injection, Inversion of Control containers, and my favorite Inversion of Control container, StructureMap.

More...

Tags:

Dec 11 2011

SpecsFor.com Launched, SpecsFor 2.2 Released!

Category: SpecsFor | TestingMatt @ 14:25

The last month has been a busy one for SpecsFor.  I’ve added numerous new features to accommodate additional testing styles and to simplify testing challenges.  I’ve also been hard at work on a real site and some docs for SpecsFor.  I’m pleased to announce that SpecsFor.com is now live.  I’ve also shipped a new version of SpecsFor that simplifies the painful task of creating multiple mocks of the same type for injection into IEnumerable parameters.

More...

Tags:

Dec 6 2011

SOLID - Things Every Senior .NET Developer Should Know, Part 2

Category: ThingsYouShouldKnowMatt @ 08:48

If you tell me you’re a senior .NET developer, there’s a handful of assumptions I’m going to make about what you know.  The “Things Every Senior .NET Developer Should Know” series of posts will look at those things.  Today’s topic is everyone’s favorite set of principles, SOLID, a set of principles that can guide you towards creating more maintainable systems.

More...

Tags:

Dec 5 2011

Yet Another Approach to NHibernate Session-Per-Method-Call Using StructureMap

Category: Matt @ 16:29

There are several documented approaches you can follow to implement the session-per-method-call pattern with NHibernate and StructureMap.  The majority of these approaches fail to leverage the full capabilities of StrurctureMap and are therefore more complex than they need to be.  In this short post, I’ll show you how you can implement a simpler solution by utilizing StructureMap’s nested containers.

More...

Tags:

Nov 27 2011

SpecsFor.Mvc – Acceptance Testing Without Magic Strings

Category: SpecsFor | TestingMatt @ 05:56

Today I published the first preview release of the next member of the SpecsFor family: SpecsFor.Mvc!  SpecsFor.Mvc is a stand-alone library designed to simplify and streamline the creation of acceptance tests for ASP.NET MVC applications.  Read on to find out how easy it is to start crafting automated acceptance tests with SpecsFor.Mvc today!

More...

Tags: