/// <summary> /// StrToRomanNumberWithError /// </summary> public static RopResult <RomanNumber, string> StrToRomanNumberWithError(string input) { var digitsWithErrs = input .ToCharArray() .Select(CharToRomanDigitWithError); var errors = digitsWithErrs .Where(r => !r.IsSuccess) .Select(r => r.FailureValues.First()); if (errors.Any()) { return(Rop.Fail <RomanNumber, string>(errors)); } else { var digits = digitsWithErrs .Where(r => r.IsSuccess) .Select(r => r.SuccessValue) .ToList(); var romanNumber = new RomanNumber(digits); return(Rop.Succeed <RomanNumber, string>(romanNumber)); } }
public static RopResult <String10, DomainMessage> CreateFirstName(string firstName) { var opt = String10.Create(firstName); return(Option.Match( opt, Rop.Succeed <String10, DomainMessage>, () => Rop.Fail <String10, DomainMessage>(DomainMessage.FirstNameIsRequired()) )); }
public static RopResult <CustomerId, DomainMessage> CreateCustomerId(int customerId) { var opt = CustomerId.Create(customerId); return(Option.Match( opt, Rop.Succeed <CustomerId, DomainMessage>, () => Rop.Fail <CustomerId, DomainMessage>(DomainMessage.CustomerIdMustBePositive()) )); }
public static RopResult <EmailAddress, DomainMessage> CreateEmail(string email) { var opt = EmailAddress.Create(email); return(Option.Match( opt, Rop.Succeed <EmailAddress, DomainMessage>, () => Rop.Fail <EmailAddress, DomainMessage>(DomainMessage.EmailIsRequired()) )); }
private static RopResult <SimpleRequest, string> EmailNotBlank(SimpleRequest input) { if (input.Email == "") { return(Rop.Fail <SimpleRequest, string>("Email must not be blank")); } else { return(Rop.Succeed <SimpleRequest, string>(input)); } }
private static RopResult <SimpleRequest, string> Name50(SimpleRequest input) { if (input.Name.Length > 50) { return(Rop.Fail <SimpleRequest, string>("Name must not be longer than 50 chars")); } else { return(Rop.Succeed <SimpleRequest, string>(input)); } }
/// <summary> /// Return the customer with the given CustomerId, or null if not found /// </summary> public RopResult <Customer, DomainMessage> GetById(CustomerId id) { var db = new DbContext(); // Note that this code does not trap exceptions coming from the database. What would it do with them? // Compare with the F# version, where errors are returned along with the Customer var result = db.Customers().Where(c => c.Id == id.Value).Select(FromDbCustomer).FirstOrDefault(); if (result == null) { return(Rop.Fail <Customer, DomainMessage>(DomainMessage.CustomerNotFound())); } return(result); }
/// <summary> /// Return all customers /// </summary> public RopResult <IEnumerable <Customer>, DomainMessage> GetAll() { var db = new DbContext(); var ropResults = db.Customers().Select(FromDbCustomer); if (ropResults.Any(r => !r.IsSuccess)) { return(Rop.Fail <IEnumerable <Customer>, DomainMessage>(DomainMessage.SqlCustomerIsInvalid())); } else { var goodResults = ropResults.Select(r => r.SuccessValue); return(Rop.Succeed <IEnumerable <Customer>, DomainMessage>(goodResults)); } }
/// <summary> /// CharToRomanDigitWithError /// </summary> public static RopResult <RomanDigit, string> CharToRomanDigitWithError(char ch) { switch (ch) { case 'I': return(Rop.Succeed <RomanDigit, string>(RomanDigit.I)); case 'V': return(Rop.Succeed <RomanDigit, string>(RomanDigit.V)); case 'X': return(Rop.Succeed <RomanDigit, string>(RomanDigit.X)); default: var msg = String.Format("{0} is not a valid char", ch); return(Rop.Fail <RomanDigit, string>(msg)); } }
/// <summary> /// Convert a DbCustomer into a domain Customer /// </summary> public static RopResult <Customer, DomainMessage> FromDbCustomer(DbCustomer sqlCustomer) { if (sqlCustomer == null) { return(Rop.Fail <Customer, DomainMessage>(DomainMessage.CustomerNotFound())); } var firstName = DomainUtilities.CreateFirstName(sqlCustomer.FirstName); var lastName = DomainUtilities.CreateLastName(sqlCustomer.LastName); var createName = Rop.Lift2 <String10, String10, PersonalName, DomainMessage>(PersonalName.Create); var name = createName(firstName, lastName); var id = DomainUtilities.CreateCustomerId(sqlCustomer.Id); var email = DomainUtilities.CreateEmail(sqlCustomer.Email); var createCust = Rop.Lift3 <CustomerId, PersonalName, EmailAddress, Customer, DomainMessage>(Customer.Create); var cust = createCust(id, name, email); return(cust); }
/// <summary> /// Create a domain customer from a DTO or null if not valid. /// </summary> public static RopResult <Customer, DomainMessage> DtoToCustomer(CustomerDto dto) { if (dto == null) { // dto can be null if deserialization fails return(Rop.Fail <Customer, DomainMessage>(DomainMessage.CustomerIsRequired())); } var firstName = DomainUtilities.CreateFirstName(dto.FirstName); var lastName = DomainUtilities.CreateLastName(dto.LastName); var createName = Rop.Lift2 <String10, String10, PersonalName, DomainMessage>(PersonalName.Create); var name = createName(firstName, lastName); var id = DomainUtilities.CreateCustomerId(dto.Id); var email = DomainUtilities.CreateEmail(dto.Email); var createCust = Rop.Lift3 <CustomerId, PersonalName, EmailAddress, Customer, DomainMessage>(Customer.Create); var cust = createCust(id, name, email); return(cust); }