Exemple #1
0
        /// <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);
        }