Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- var validEmailList = new List<EmailAddress>()
- {
- new EmailAddress("a@b.com"),
- new EmailAddress("b@c.com", true)
- };
- IsValidPrimaryCollection(validEmailList)
- .Dump("One email marked as primary");
- var noPrimaryEmailList = new List<EmailAddress>()
- {
- new EmailAddress("a@b.com"),
- new EmailAddress("b@c.com")
- };
- IsValidPrimaryCollection(noPrimaryEmailList)
- .Dump("No email marked as primary");
- var multiplePrimaryEmailList = new List<EmailAddress>()
- {
- new EmailAddress("a@b.com", true),
- new EmailAddress("b@c.com", true)
- };
- IsValidPrimaryCollection(multiplePrimaryEmailList)
- .Dump("More than one email marked as primary");
- var validPhoneList = new List<PhoneNumber>()
- {
- new PhoneNumber("a@b.com"),
- new PhoneNumber("b@c.com", true)
- };
- IsValidPrimaryCollection(validPhoneList)
- .Dump("One phone marked as primary");
- var noPrimaryPhoneList = new List<PhoneNumber>()
- {
- new PhoneNumber("a@b.com"),
- new PhoneNumber("b@c.com")
- };
- IsValidPrimaryCollection(noPrimaryPhoneList)
- .Dump("No phone marked as primary");
- var multiplePrimaryPhoneList = new List<PhoneNumber>()
- {
- new PhoneNumber("a@b.com", true),
- new PhoneNumber("b@c.com", true)
- };
- IsValidPrimaryCollection(multiplePrimaryPhoneList)
- .Dump("More than one phone marked as primary");
- }
- public bool IsValidPrimaryCollection<T>(IEnumerable<T> items)
- where T : IHavePrimaryFLag => items.Count(i => i.IsPrimary) == 1;
- public interface IHavePrimaryFLag
- {
- bool IsPrimary {get;}
- }
- public class EmailAddress : IHavePrimaryFLag
- {
- public bool IsPrimary { get; }
- public int Id { get; set; }
- public string Value { get; }
- public EmailAddress(string value, bool isPrimary = false)
- {
- Value = value;
- IsPrimary = isPrimary;
- }
- }
- public class PhoneNumber : IHavePrimaryFLag
- {
- public bool IsPrimary { get; }
- public int Id { get; set; }
- public string Value { get; }
- public PhoneNumber(string value, bool isPrimary = false)
- {
- Value = value;
- IsPrimary = isPrimary;
- }
- }
Add Comment
Please, Sign In to add comment