Background
I have always been amazed at how many applications crash when they don't have data in them, i.e. an empty database table produces exceptions because an objection collection call returns NULL. This pattern will prevent this (up to a point).
Advanatages
- Calling layers don't have to worry about NULLS
- Its simply and it works without creating to much clutter.
- Empty collections are easy to work with
- If you say you are returning a collection you better return one, even if it is empty.
public static List
{
// CREATE AN EMPTY LIST
var result = new List
// GO TO DB AND GET RESULTS
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("test");
var collection = database.GetCollection
var getall = collection.FindAll().ToList();
// Add results if any to the collection
result.AddRange(getall);
// RETURN LIST (WHICH MAY BE EMPTY)
return result;
}
No comments:
Post a Comment
Comments are welcome, but are moderated and may take a wee while before shown.