Skip to content

Instantly share code, notes, and snippets.

@jmhdez
Created September 16, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhdez/15b3abe61814f95950f5 to your computer and use it in GitHub Desktop.
Save jmhdez/15b3abe61814f95950f5 to your computer and use it in GitHub Desktop.
Maybe<T> Sample
public IEnumerable<ProductSalesEntry> GetProductSales(Maybe<User> user, DateTime fromDate, DateTime toDate)
{
// El método puede recibir o no un usuario o un Maybe<User>.Empty.
// Si recibe un usuario, se pasa su Id a la consulta SQl, si no,
// se pasa 0 y la consulta SQL no filtrará por usuario;
// vamos, el típico where (user.Id = @userId or @userId = 0)
// Para hacer explícito que el usuario es un parámetro opcional del método, se
// define como un Maybe<User>. Se converte en un Maybe<int> para obtener el Id
// usando "select" (el bind de cualquier mónada, pero más C# friendly) y finalmente
// para desenvolver el valor encapsulado en el Maybe se usa el GetValueOr, que
// obliga a decidir explícitamente qué hacer en caso de que el Maybe sea vacío
return queryProvider.GetQuery("QueryUserSales")
.SetParameter("from", fromDate)
.SetParameter("to", toDate)
.SetParameter("userId", user.Select(x => x.Id).GetValueOr(0))
.List<ProductSalesEntry>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment