Skip to content

Instantly share code, notes, and snippets.

@xiaomi7732
Created January 4, 2022 18:41
Show Gist options
  • Select an option

  • Save xiaomi7732/9ad1946506cf4b9c6f506192b3072735 to your computer and use it in GitHub Desktop.

Select an option

Save xiaomi7732/9ad1946506cf4b9c6f506192b3072735 to your computer and use it in GitHub Desktop.
Learn primitive obsession - attempt 1
InsightsInstrumentationKey iKey = "12345";
PrintResult(iKey);
iKey = "6e12e0b1-0cbd-4fe9-9b5d-2d8a46e672fe";
PrintResult(iKey);
iKey = Guid.NewGuid();
PrintResult(iKey);
void PrintResult(InsightsInstrumentationKey iKey)
{
Console.WriteLine("Value: {0}, Is valid: {1}", iKey, iKey != InsightsInstrumentationKey.Invalid);
}
public record InsightsInstrumentationKey
{
public Guid Value { get; init; }
public static InsightsInstrumentationKey Invalid = new InsightsInstrumentationKey() { Value = Guid.Empty };
public static implicit operator Guid(InsightsInstrumentationKey k) => k.Value;
public static implicit operator InsightsInstrumentationKey(string? stringGuid)
{
if (Guid.TryParse(stringGuid, out Guid successConverted))
{
return new InsightsInstrumentationKey() { Value = successConverted };
}
return InsightsInstrumentationKey.Invalid;
}
public static implicit operator InsightsInstrumentationKey(Guid guid)
{
return new InsightsInstrumentationKey { Value = guid };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment