Guest User

Untitled

a guest
May 9th, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. void Main()
  2. {
  3. var validEmailList = new List<EmailAddress>()
  4. {
  5. new EmailAddress("a@b.com"),
  6. new EmailAddress("b@c.com", true)
  7. };
  8.  
  9. IsValidPrimaryCollection(validEmailList)
  10. .Dump("One email marked as primary");
  11.  
  12. var noPrimaryEmailList = new List<EmailAddress>()
  13. {
  14. new EmailAddress("a@b.com"),
  15. new EmailAddress("b@c.com")
  16. };
  17.  
  18. IsValidPrimaryCollection(noPrimaryEmailList)
  19. .Dump("No email marked as primary");
  20.  
  21. var multiplePrimaryEmailList = new List<EmailAddress>()
  22. {
  23. new EmailAddress("a@b.com", true),
  24. new EmailAddress("b@c.com", true)
  25. };
  26.  
  27. IsValidPrimaryCollection(multiplePrimaryEmailList)
  28. .Dump("More than one email marked as primary");
  29.  
  30. var validPhoneList = new List<PhoneNumber>()
  31. {
  32. new PhoneNumber("a@b.com"),
  33. new PhoneNumber("b@c.com", true)
  34. };
  35.  
  36. IsValidPrimaryCollection(validPhoneList)
  37. .Dump("One phone marked as primary");
  38.  
  39. var noPrimaryPhoneList = new List<PhoneNumber>()
  40. {
  41. new PhoneNumber("a@b.com"),
  42. new PhoneNumber("b@c.com")
  43. };
  44.  
  45. IsValidPrimaryCollection(noPrimaryPhoneList)
  46. .Dump("No phone marked as primary");
  47.  
  48. var multiplePrimaryPhoneList = new List<PhoneNumber>()
  49. {
  50. new PhoneNumber("a@b.com", true),
  51. new PhoneNumber("b@c.com", true)
  52. };
  53.  
  54. IsValidPrimaryCollection(multiplePrimaryPhoneList)
  55. .Dump("More than one phone marked as primary");
  56. }
  57.  
  58. public bool IsValidPrimaryCollection<T>(IEnumerable<T> items)
  59. where T : IHavePrimaryFLag => items.Count(i => i.IsPrimary) == 1;
  60.  
  61. public interface IHavePrimaryFLag
  62. {
  63. bool IsPrimary {get;}
  64. }
  65.  
  66. public class EmailAddress : IHavePrimaryFLag
  67. {
  68. public bool IsPrimary { get; }
  69. public int Id { get; set; }
  70. public string Value { get; }
  71.  
  72. public EmailAddress(string value, bool isPrimary = false)
  73. {
  74. Value = value;
  75. IsPrimary = isPrimary;
  76. }
  77. }
  78.  
  79. public class PhoneNumber : IHavePrimaryFLag
  80. {
  81. public bool IsPrimary { get; }
  82. public int Id { get; set; }
  83. public string Value { get; }
  84.  
  85. public PhoneNumber(string value, bool isPrimary = false)
  86. {
  87. Value = value;
  88. IsPrimary = isPrimary;
  89. }
  90. }
Add Comment
Please, Sign In to add comment