Skip to content

Instantly share code, notes, and snippets.

@oshea00
Created March 6, 2020 07:45
Show Gist options
  • Save oshea00/098358abd7b1fe55d26ae28299c6f828 to your computer and use it in GitHub Desktop.
Save oshea00/098358abd7b1fe55d26ae28299c6f828 to your computer and use it in GitHub Desktop.
Code In English
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;
}
}
@oshea00
Copy link
Author

oshea00 commented Mar 6, 2020

Paste into Linqpad :)

@joaopgrassi
Copy link

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