Last active
February 25, 2020 09:21
-
-
Save rommsen/c88165e5d461b681352ba180526b580f to your computer and use it in GitHub Desktop.
Value Objects
This file contains 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
namespace ddd_patterns | |
{ | |
public class AddressBook | |
{ | |
public readonly PhoneNumber HomeNumber; | |
public readonly PhoneNumber MobileNumber; | |
public readonly PhoneNumber WorkNumber; | |
public readonly ValidEmailAddress Email; | |
public AddressBook(PhoneNumber homeNumber, PhoneNumber mobileNumber, PhoneNumber workNumber, ValidEmailAddress email) | |
{ | |
HomeNumber = homeNumber; | |
MobileNumber = mobileNumber; | |
WorkNumber = workNumber; | |
Email = email; | |
} | |
public AddressBook WithNewEmailAddress(ValidEmailAddress email) | |
{ | |
return new AddressBook(HomeNumber, MobileNumber, WorkNumber, email); | |
} | |
} | |
} |
This file contains 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
using System; | |
namespace ddd_patterns | |
{ | |
public class AttendeeId | |
{ | |
private readonly Guid Id; | |
public AttendeeId(Guid id) | |
{ | |
Id = id; | |
} | |
} | |
} | |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ddd_patterns | |
{ | |
public class Money | |
{ | |
private readonly decimal Value; | |
public Money(decimal value) | |
{ | |
Value = value; | |
} | |
public Money Add(Money money) | |
{ | |
return new Money(Value + money.Value); | |
} | |
} | |
} | |
This file contains 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
namespace ddd_patterns | |
{ | |
public struct Name | |
{ | |
public readonly string FirstName; | |
public readonly string Surname; | |
public Name(string firstName, string surname) | |
{ | |
FirstName = firstName; | |
Surname = surname; | |
} | |
public override string ToString() | |
{ | |
return $"firstName:{FirstName};;surname:{Surname}"; | |
} | |
} | |
} |
This file contains 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
namespace ddd_patterns | |
{ | |
public class PhoneNumber | |
{ | |
public readonly string Number; | |
public PhoneNumber(string number) | |
{ | |
this.Number = number; | |
} | |
} | |
} |
This file contains 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
using System; | |
namespace ddd_patterns | |
{ | |
public class Attendee | |
{ | |
private readonly Guid _id; | |
private readonly Name _name; | |
private readonly AddressBook _addressBook; | |
public Attendee(Guid id, Name name, AddressBook addressBook) | |
{ | |
_id = id; | |
_name = name; | |
_addressBook = addressBook; | |
} | |
} | |
} |
This file contains 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
using System; | |
namespace ddd_patterns | |
{ | |
public class TicketId | |
{ | |
private readonly Guid Id; | |
public TicketId(Guid id) | |
{ | |
Id = id; | |
} | |
} | |
} | |
This file contains 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
using System; | |
namespace ddd_patterns | |
{ | |
public struct ValidEmailAddress | |
{ | |
public readonly string Address; | |
private ValidEmailAddress(string email) | |
{ | |
Address = email; | |
} | |
public ValidEmailAddress FromUnsafe(string email) | |
{ | |
if (!IsValid(email)) | |
{ | |
throw new EmailIsNotValidException(); | |
} | |
return new ValidEmailAddress(email); | |
} | |
private static bool IsValid(string email) | |
{ | |
// your not working regex here | |
return true; | |
} | |
} | |
public class EmailIsNotValidException : Exception | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment