Skip to content

Instantly share code, notes, and snippets.

@mrvux
Created March 18, 2015 13:40
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mrvux/fd4d8db7e950727afa46 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Author>Julien Vulliet (julien@mrvux.com)</Author>
<Description>Snippet to create a simple domain primitive</Description>
<Title>Domain Primitive</Title>
<Shortcut>domainPrimitiveClass</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>classname</ID>
<ToolTip>Class Name</ToolTip>
<Default>Tuple</Default>
</Literal>
<Literal>
<ID>primitive</ID>
<ToolTip>Primitive Type</ToolTip>
<Default>int</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public class $classname$
{
private readonly $primitive$ value;
public $classname$($primitive$ value)
{
if (!IsValid(value))
throw new ArgumentException("value", "Put exception message here");
this.value = value;
}
public static bool IsValid($primitive$ candidate)
{
//Add your validation logic here
}
public static implicit operator $primitive$($classname$ value)
{
return value.value;
}
public override string ToString()
{
return this.value.ToString();
}
public override bool Equals(object obj)
{
var other = obj as $classname$;
if (other == null)
return base.Equals(obj);
return object.Equals(this.value, other.value);
}
/// <see cref="System.Object.GetHashCode"/>
public override int GetHashCode()
{
return this.value.GetHashCode();
}
]]>
}
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
@JulianMay
Copy link

Neat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment