/// <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> /// 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); }