Beispiel #1
0
 public ErrorMonad <int> Save(Customer customer)
 {
     if (Successful(customer))
     {
         return(ErrorMonad <int> .Valid(customer.CustomerId));
     }
     else
     {
         return(ErrorMonad <int> .Error("Failed to Save Customer"));
     }
 }
Beispiel #2
0
 public static Customer Create(string name, string phoneNumber)
 {
     if (ValidPhoneNumber.IsMatch(phoneNumber))
     {
         return(ErrorMonad <Customer> .Valid(Customer.Create(name, phoneNumber)));
     }
     else
     {
         return(ErrorMonad <Customer> .Error("Invalid PhoneNumber"));
     }
 }
Beispiel #3
0
 public ErrorMonad <U> AndThen <U>(Func <TaskFactory, ErrorMonad <U> > step)
 {
     if (_error != null)
     {
         return(ErrorMonad <U> .Error(_error));
     }
     else
     {
         return(step(_value));
     }
 }
Beispiel #4
0
 public static ErrorMonad <Customer> Create(string name, string phoneNumber)
 {
     if (ValidPhoneNumber.IsMatch(phoneNumber))
     {
         return(ErrorMonad <Customer> .Valid(new Customer(name, phoneNumber)));
     }
     else
     {
         return(ErrorMonad <Customer> .Error("THERE WAS AN ERRORRR"));
     }
 }
        public void DifferentReturnErrorIsNotEqual()
        {
            var value = "SomeErr";
            var monad = new ErrorMonad <Int32>(value);

            var asserterVisitor = new AssertErrorVisitor <Int32>
            {
                ExpectedError = "SomeOtherErr",
                AreEqual      = false
            };

            monad.Accept(asserterVisitor);
        }
        public void ReturnErrorIsEqualEvenForInt()
        {
            var value = "error";
            var monad = new ErrorMonad <Int32>(value);

            var asserterVisitor = new AssertErrorVisitor <Int32>
            {
                ExpectedError = value,
                AreEqual      = true
            };

            monad.Accept(asserterVisitor);
        }
Beispiel #7
0
        public void BindPropagatesTheError()
        {
            var errMessage  = "error message";
            var firstMonad  = new ErrorMonad <Int32>(errMessage);
            var secondMonad = firstMonad.Bind(val => new ValueMonad <Int32>(val + 1));

            var asserterVisitor = new AssertErrorVisitor <Int32>
            {
                ExpectedError = errMessage,
                AreEqual      = true
            };

            secondMonad.Accept(asserterVisitor);
        }