Created
August 18, 2015 20:49
-
-
Save AndyStewart/1790c564c775a72f49da to your computer and use it in GitHub Desktop.
Null Handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace Nulls | |
{ | |
public class Repository | |
{ | |
private readonly List<Person> _persons = new List<Person>() | |
{ | |
new Person { FirstName = "Bob", Surname = "Smith"}, | |
new Person { FirstName = "George", Surname = "Brown"} | |
}; | |
public IMaybe<Person> GetByName(string name) | |
{ | |
var foundPerson = _persons.FirstOrDefault(q => q.FirstName == name); | |
if (foundPerson == null) | |
return new Nothing<Person>(new Person {FirstName = "Unknown", Surname = "Unknown"}); | |
return new Just<Person>(foundPerson); | |
} | |
} | |
public class Nothing<T> : IMaybe<T> | |
{ | |
public Nothing(T defaultValue) | |
{ | |
Value = defaultValue; | |
} | |
public T Value { get; } | |
public bool HasValue() | |
{ | |
return false; | |
} | |
} | |
public class Just<T> : IMaybe<T> | |
{ | |
public T Value { get; private set; } | |
public Just(T value) | |
{ | |
Value = value; | |
} | |
public bool HasValue() | |
{ | |
return true; | |
} | |
} | |
public interface IMaybe<T> | |
{ | |
T Value { get; } | |
bool HasValue(); | |
} | |
public class Person | |
{ | |
public string FirstName { get; set; } | |
public string Surname { get; set; } | |
public override string ToString() | |
{ | |
return FirstName + " " + Surname; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xunit; | |
namespace Nulls | |
{ | |
public class NullTests | |
{ | |
[Fact] | |
public void ShouldReturnValue() | |
{ | |
var repository = new Repository(); | |
var result = repository.GetByName("Bob"); | |
Assert.True(result.HasValue()); | |
} | |
[Fact] | |
public void ShouldReturnUnFoundRecord() | |
{ | |
var repository = new Repository(); | |
var result = repository.GetByName("Jenkins"); | |
Assert.False(result.HasValue()); | |
} | |
[Fact] | |
public void ShouldPrintOutNameOfNotFoundPerson() | |
{ | |
var repository = new Repository(); | |
var result = repository.GetByName("Jenkins"); | |
Assert.Equal(result.Value.FirstName, "Unknown"); | |
Assert.Equal(result.Value.Surname, "Unknown"); | |
} | |
[Fact] | |
public void ShouldConcatNamesOfTwoPeople() | |
{ | |
var repository = new Repository(); | |
var jenkins = repository.GetByName("Jenkins"); | |
var bob = repository.GetByName("Bob"); | |
var bothNames = jenkins.Value + bob.Value.ToString(); | |
Assert.Equal("Unknown UnknownBob Smith", bothNames); | |
} | |
[Fact] | |
public void AddTwoNumbers() | |
{ | |
var setValue = new Just<int>(1); | |
var unsetValue = new Nothing<int>(0); | |
var total = setValue.Value + unsetValue.Value; | |
Assert.Equal(1, total); | |
Assert.False(unsetValue.HasValue()); | |
Assert.True(setValue.HasValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment