Created
March 6, 2020 07:45
-
-
Save oshea00/098358abd7b1fe55d26ae28299c6f828 to your computer and use it in GitHub Desktop.
Code In English
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
void Main() | |
{ | |
// Requirement: I want an Account that can have any type of identifier: | |
// Could a business analyst understand this code? | |
var accountWithInteger = new Account<int> { | |
Id = 3, | |
Name = "has integer key"}; | |
var accountWithString = new Account<string> { | |
Id = "Stringy", | |
Name = "has string key" }; | |
Assert.IsTrue(() => accountWithInteger.Id is string) | |
.Dump($"{accountWithInteger}?"); | |
Assert.IsTrue(() => accountWithString.Id is int) | |
.Dump($"{accountWithString}?"); | |
var accountList = new List<object> { | |
accountWithInteger, | |
accountWithString | |
}; | |
accountList.Dump(); | |
Assert.IsTrue(() => accountList | |
.OfType<IsAccount<int>>().Any()) | |
.Dump("Are there any accounts with integer ids?"); | |
Assert.IsTrue(() => accountList | |
.OfType<IsAccount<string>>().Any()) | |
.Dump("Are there any accounts with string ids?"); | |
} | |
interface IsAccount<Key> { | |
Key Id {get; set;} | |
string Name {get; set;} | |
} | |
class Account<Key> : IsAccount<Key> | |
{ | |
public Key Id { get; set; } | |
public string Name { get; set; } | |
public override string ToString() | |
{ | |
return $"Account Id = {Id.ToString()}, Name = {Name}"; | |
} | |
} | |
class Assert { | |
public static bool IsTrue(Func<bool> assertion) { | |
return assertion() == true; | |
} | |
} | |
If you use FluentAssertions this could be even clearer.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Paste into Linqpad :)