Skip to content

Instantly share code, notes, and snippets.

@rodion-m
Last active March 13, 2023 17:12
Show Gist options
  • Save rodion-m/11a039885a2f305a918d9b416574a306 to your computer and use it in GitHub Desktop.
Save rodion-m/11a039885a2f305a918d9b416574a306 to your computer and use it in GitHub Desktop.
Value object phone example
using System;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
namespace SimpleJournal.ValueObjects;
[Owned]
public class Phone
{
public string Value { get; private set; }
protected Phone() {}
public Phone(string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (!IsPhone(value))
{
throw new InvalidOperationException("Некорретный номер телефона");
}
Value = value;
}
public override string ToString() => Value;
private static bool IsPhone(string value)
{
var ruPhoneRegex = new Regex(@"\+79\d{2}\d{7}");
return ruPhoneRegex.IsMatch(value);
}
protected bool Equals(Phone other)
{
return Value == other.Value;
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Phone)obj);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static bool operator ==(Phone? left, Phone? right)
{
return Equals(left, right);
}
public static bool operator !=(Phone? left, Phone? right)
{
return !Equals(left, right);
}
}
private static void Example()
{
var phone1 = new Phone("+79311111111");
var phone2 = new Phone("+79311111111");
if (phone1.Equals(phone2))
{
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment