Skip to content
This repository has been archived by the owner on Oct 14, 2019. It is now read-only.

Monads for objects

sergun edited this page Aug 7, 2012 · 1 revision

Do

Before

if (data != null)
{
  Console.WriteLine(data);
}

After

data.Do(d=>Console.WriteLine(d));

With

Before

string workPhoneCode;

if (person != null)
{
  if (person.Work != null)
  {
    if (person.Work.Phone != null)
    {
       workPhoneCode = person.Work.Phone.Code;
    }
  }
}

After

string workPhoneCode = person.With(p=>p.Work).With(w=>w.Phone).With(p=>p.Code);

Return

Before

string workPhoneCode = "default code";

if (person != null)
{
  if (person.Work != null)
  {
    if (person.Work.Phone != null)
    {
       workPhoneCode = person.Work.Phone.Code;
    }
  }
}

After

string workPhoneCode = person.With(p=>p.Work).With(w=>w.Phone).Return(p=>p.Code, "default code");

If/IfNot

Before

if ((person.Age > 18) &&
   (person.LastName != null) &&
   (person.Work != null))
{
     Console.WriteLine(person);
}

After

person.If(p=>p.Age > 18).IfNot(p=>p.LastName == null).IfNot(p=>p.Work == null).Do(p=>Console.WriteLine(person));

Recover

Before

var person = new Person();

// do something

if (person == null)
{
  person = new Person();
}

Console.WriteLine(person.LastName);

After

var person = new Person();

// do something

Console.WriteLine(person.Recover(()=>new Person()).LastName);

TryDo/Catch

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch
{
}

After

var person = ... ;
// person.Work = null;
person.TryDo(p=>Console.WriteLine(p.Work.Address));

--

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch
{
  Console.WriteLine("Error");
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address)).Catch(e=>Console.WriteLine("Error"));

Checking exception type (via Type)

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch (NullReferenceException)
{
  Console.WriteLine("Error");
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address), typeof(NullReferenceException))
	.Catch(e=>Console.WriteLine("Error"));

Checking exception type (via predicate)

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch (NullReferenceException ex)
{
  if (ex.Message.Contains("some word") == false)
  {
    throw;
  }
  Console.WriteLine("Error");
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address), e=>e.Message.Contains("some words"))
	.Catch(e=>Console.WriteLine("Error"));

Ignore exceptions

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch
{
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address)).Catch();

TryWith/Catch

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  var address = person.Work.Address;
  Console.WriteLine (address.TrimLeft());
}
catch
{
}

After

var person = ... ;
// person.Work = null;
person.TryWith(p=>p.Work.Address).Catch().Do(p=>Console.WriteLine(p));

Exception matching and handling policies such as for TryDo.