Skip to content

Instantly share code, notes, and snippets.

@maybe-joe
Created September 15, 2016 23:16
Show Gist options
  • Save maybe-joe/1676cd52d8e5f5e2a395ec011f6d8ecb to your computer and use it in GitHub Desktop.
Save maybe-joe/1676cd52d8e5f5e2a395ec011f6d8ecb to your computer and use it in GitHub Desktop.
Duct typing and using
/*
* This is a silly little test to remove the word using from data access classes.
* Beware that null will be returned if the dynamic object doesn't match the return type of the calling function.
*/
private dynamic IHateTheWordUsing(Func<UmbracoDBDataContext, dynamic> command)
{
using (var context = new UmbracoDBDataContext(sqlConnection))
{
return command(context);
}
}
public Product GetProduct(Guid productId)
{
return IHateTheWordUsing(context =>
{
var product = context.products.FirstOrDefault(x => x.Id == productId);
if (product == null)
{
return null;
}
return new Product
{
Id = product.Id,
Name = product.Name,
Description = product.Description
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment