Created
September 15, 2016 23:16
-
-
Save maybe-joe/1676cd52d8e5f5e2a395ec011f6d8ecb to your computer and use it in GitHub Desktop.
Duct typing and using
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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