It's a nice way to end a busy year. Merry Christmas and Happy New Year.
Tuesday 18 December 2012
MongoDB Developer Course Success
It's a nice way to end a busy year. Merry Christmas and Happy New Year.
Tuesday 4 December 2012
Dynamic Typing and Extension Methods
'System.DateTime' does not contain a definition for 'ToUKFormat'
'ToUKFormat' is a custom extension method on DateTime that I wrote and was in-scope at compile time when I called it. So what caused the runtime exception? It turns out that you cannot call an extension method on an object/value that is dynamically typed. In my case, I was using the dynamic ViewBag in an ASP.NET MVC application to store a DateTime value - then in my view, I was retrieving that value from the ViewBag and calling a custom written extension method on it ('ToUKFormat'). In razor syntax, I was doing something similar to:
@ViewBag.Foo.Bar.ToUKFormat()I have given the solution further below but if you want to reproduce the problem in a simple console application, you can use the following code:
namespace DynamicRuntimeIssue { class Program { static void Main(string[] args) { dynamic today = DateTime.Today; Console.WriteLine(today.ToUKFormat()); // Exception... Console.ReadLine(); } } static class DateTimeExtensions { public static string ToUKFormat(this DateTime dateTime) { return dateTime.ToString("dd/MM/yyyy"); } } }When you run the code above, you will encounter the aforementioned exception. Of course, if you appropriately type the 'today' variable to a DateTime rather than dynamic, then you'll find that it all works as expected. However, if you cannot avoid the use of dynamic, like for example in my case where I was using ViewBag - then you can overcome this exception by calling your extension method as if it was any normal static method. You call the method and pass-in your object/value through as an actual parameter. Following on from the example above, you would therefore have:
Console.WriteLine(DateTimeExtensions.ToUKFormat(today));Rather than:
Console.WriteLine(today.ToUKFormat());If you want to understand why you can't call extension methods on a dynamic type, then go over to this stackoverflow question where someone else has had the same issue - you will particularly want to read Eric Lippert's response dated March 15 2011.
Saturday 24 November 2012
CRUD against MongoDB via C#
Throughout the post I'll be using a simple collection of documents where each document stores some basic information about an aircraft. If you load the MongoDB shell and execute the following statements then we'll have a common ground to start with:
'Manufacturer':'Lockheed Skunk Works',
'Model':'Blackbird SR-71',
'FirstFlight': new Date('12/22/1964')
})
db.planes.insert({
'Manufacturer':'Lockheed Skunk Works',
'Model':'X-55 ACCA',
'FirstFlight': new Date('06/02/2009')
})
using MongoDB.Driver;
using MongoDB.Driver.Builders;
{
public ObjectId Id { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public DateTime FirstFlight { get; set; }
}
Let's now take a look at how you can insert a new plane document into the planes collection using the driver. We'll first get a handle on the planes collection, I have the following four lines at the top of my Main method that gives me the required reference (planesCollection):
var server = MongoServer.Create(connectionString);
var mongotestdb = server.GetDatabase("mongotest");
var planesCollection = mongotestdb.GetCollection<Plane>("planes");
Manufacturer = "Unknown",
Model = "Tornado IDS/ECR",
FirstFlight = new DateTime(1974, 8, 14)
});
Query.EQ("Manufacturer", "Lockheed Skunk Works"))
.ToList();
Query.EQ("Manufacturer", "Unknown")
);
planeToUpdate.Manufacturer = "Panavia Aircraft GmbH";
planesCollection.Save(planeToUpdate);
Finally, to remove a set of documents based on a query from the planes collection - in this case we'll remove all planes manufactured by Lockheed from the collection:
Query.EQ("Manufacturer", "Lockheed Skunk Works")
);
Friday 9 November 2012
Initial thoughts on NoSQL with MongoDB
Friday 5 October 2012
C# How To: Convert Integer to Base Binary, Octal, Denary or Hexadecimal
Today, I came across an overload of the Convert.ToString method which surprisingly accepted a parameter called toBase. I wish I had come across this earlier... The overload accepts the bases 2, 8, 10 or 16 and throws an ArgumentException if one of these are not passed in. Examples are below:
Console.WriteLine("{0}", Convert.ToString(10,8)); // Outputs "12"
Console.WriteLine("{0}", Convert.ToString(10,10)); // Outputs "10"
Console.WriteLine("{0}", Convert.ToString(10,16)); // Outputs "a"
Console.WriteLine("{0}", Convert.ToString(10,3)); // Throws ArgumentException
Thursday 4 October 2012
The Pragmatic Programmer
The topics covered range from general tips that the authors learnt from their experiences, some useful tools you should know about as a programmer, how to think when you're writing code, how to write testable code and much more. It's a mixed bag of eight chapters, with each chapter broken down into smaller digestable sections. It was the perfect companion to someone who commuted into university on the train.
I know this post isn't relevant to C# per se, but thought it may encourage you to get hold of the book and learn some useful practices that you can apply in your projects.
Sunday 15 July 2012
Implementing Convert.ToInt32(string)
public static int ConvertToInt32(string integerAsString) { var result = 0; var positiveSigned = integerAsString[0] == '+'; var negativeSigned = integerAsString[0] == '-'; for (var i = (positiveSigned || negativeSigned) ? 1 : 0; i < integerAsString.Length; i++) { var currentCharAsInt = (int) integerAsString[i] - (int) '0'; if (currentCharAsInt < 0 || currentCharAsInt > 9) throw new ArgumentException( string.Format("The String value '{0}' " + "is not in a recognisable format.", integerAsString)); result *= 10; result += currentCharAsInt; } return (negativeSigned) ? -result : result; }
Tuesday 22 May 2012
MVC Model Binding to List of Complex Objects
Recently, I had a requirement to create a form-based partial view in an MVC3 application that permitted a user to select multiple options using standard HTML checkboxes. What this essentially meant was that I needed MVC to automatically bind a complex list of objects to the argument of an action method.
After doing some reading on this, I found that the DefaultModelBinder supports this as long as the form fields are named in such a way that the model can distinguish one complex object from another - this can be achieved by using a unique index when creating the form. The example below shows exactly how this can be done - I'm using MVC3 with the Razor view engine.
Imagine we need to display a list of options to the user, the user can select multiple options from the list and then post the form back to one of your action methods on the server side. In this scenario, we're going to display a list of planes to the user, the user can select their favourite plane(s) and then click a submit button. We'll start by defining our model - a "complex" but self explanatory class called PlaneModel.
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public bool IsFavourite { get; set; }
public override string ToString()
{
return string.Format("{0} - {1}", Manufacturer, Model);
}
}
For the sake of brevity, I won't use a partial view (but the method should be the same if you want to use a partial view in your case). We'll create a new controller called PlaneController, with one initial action method "Index". In this action method, we'll new-up some instances of PlaneModel, store them in a list-based collection and then pass this collection as a model to a strongly-typed Index view. The Index action method would therefore look like:
public ActionResult Index()
{
var planes = new List<PlaneModel>(); //Model
planes.Add(new PlaneModel {
Manufacturer = "Cessna",
Model = "C208B Grand Caravan" });
planes.Add(new PlaneModel {
Manufacturer = "Douglas",
Model = "DC-3" });
planes.Add(new PlaneModel {
Manufacturer = "Piper",
Model = "J-3 Cub" });
planes.Add(new PlaneModel {
Manufacturer = "Mooney",
Model = "M20J" });
return View(planes);
}
Notice that the action method maps to our HTTP GET request. So, whilst we're still in our controller, we'll write the POST action. The key thing to remember here is that our post action will accept a list of PlaneModel objects.
public ActionResult ProcessFavouritePlanes(List<PlaneModel> model)
{
foreach (var planeModel in model)
{
if (planeModel.IsFavourite)
Debug.WriteLine("Favourited: {0}", planeModel);
else
Debug.WriteLine("Not favourited: {0}", planeModel);
}
return View(model);
}
So, all I'm doing in the POST action is iterating through the planes in the model (which will be passed back from the view) - and hopefully the IsFavourite property should have been bound to the correct values that the user selects using checkboxes.
Now onto the important part - the creation of our view. Create a strongly typed Index view (i.e., a generic list of type PlaneModel). If you're using Visual Studio as your IDE, you can right-click within your Index action method and select the option "Add View" - this should bring up a modal dialog. Leave the view name as "Index", check the "Create a strongly-typed view" option and type:
in the "Model class" text box (note that you will probably need to prefix the PlaneModel class name with its fully qualified namespace as the generic type parameter to List - if you don't do this you'll get a runtime error when navigating to the Index view). You can now click "Add" and the view will get created under the conventional folder structure.
The view logic will be a simple mixture of standard HTML and C# in Razor syntax:
Please select your favourite plane(s):<br />
@using (Html.BeginForm("ProcessFavouritePlanes",
"Plane",
FormMethod.Post))
{
for (int i = 0; i < Model.Count; i++)
{
@Html.CheckBoxFor(m => m[i].IsFavourite)
@Model[i].ToString()
@Html.HiddenFor(m => m[i].Manufacturer)
@Html.HiddenFor(m => m[i].Model)
}
<input type="submit" value="Go!" />
}
Notice that we're iterating through each PlaneModel object using a C# for-loop. This allows us to use the incrementing index and display each option from the model. Also note the use of the hidden fields for the Manufacturer and Model properties - these are here to ensure that they're passed back to the DefaultModelBinder on the server side - taking these two lines out will mean that we'll get PlaneModel objects with blank values for those two properties when the form is posted to the POST action. You should now be able to test if this is all working by hitting a breakpoint on the POST action, running the application and selecting some options. You'll find that the model binder will automatically bind the selected checkboxes and update the model passed into the action.
To understand why this works, we can take a look at the rendered HTML sent back to the client for our form:
<input name="[0].IsFavourite" type="checkbox" value="true" />
<input name="[0].IsFavourite" type="hidden" value="false" />
Cessna - C208B Grand Caravan
<input name="[0].Manufacturer" type="hidden" value="Cessna" />
<input name="[0].Model" type="hidden" value="C208B Grand Caravan" />
<br />
<input name="[1].IsFavourite" type="checkbox" value="true" />
<input name="[1].IsFavourite" type="hidden" value="false" />
Douglas - DC-3
<input name="[1].Manufacturer" type="hidden" value="Douglas" />
<input name="[1].Model" type="hidden" value="DC-3" />
<br />
<input name="[2].IsFavourite" type="checkbox" value="true" />
<input name="[2].IsFavourite" type="hidden" value="false" />
Piper - J-3 Cub
<input name="[2].Manufacturer" type="hidden" value="Piper" />
<input name="[2].Model" type="hidden" value="J-3 Cub" />
<br />
<input name="[3].IsFavourite" type="checkbox" value="true" />
<input name="[3].IsFavourite" type="hidden" value="false" />
Mooney - M20J
<input name="[3].Manufacturer" type="hidden" value="Mooney" />
<input name="[3].Model" type="hidden" value="M20J" />
<br />
<input type="submit" value="Go!" />
</form>
Notice how the framework added index prefixes to the name attributes of the input elements. The use of this index-based naming convention for the input elements allows the DefaultModelBinder in MVC to distinguish between each complex object - and therefore seamlessly create a correct representation of our model that is passed to the POST action - very neat!
Wednesday 11 April 2012
ASP.NET MVC3 Attributes
Having played with MVC2 in the past, the work on this project has been smooth sailing. I'm particularly enjoying the level of control that the MVC development model provides over the resulting HTML sent back to a users browser. The bloated HTML from the Web Forms model (quickly pointing the finger at the ViewState hidden field) is something I don't have to painfully scroll over when inspecting the generated HTML.
I particularly like how MVC makes use of attributes on action methods, things like the AuthorizeAttribute are a nice and clean way of executing repeated functionality without it really getting in the way of your usual controller or action level logic.
The main purpose of this post is therefore just for me to continuously document the useful attributes I come across. My intention is to update the table below whilst working on the project mentioned above.
Attribute Name | Notes |
---|---|
System.Web.Mvc.AuthorizeAttribute | Applied on actions that require the user to be authenticated. If the user is not authenticated, they are automaticallly redirected to the login page. Can also be used at the controlller (class) level - which applies it on all actions within the controller. |
System.Web.Mvc.ChildActionOnlyAttribute | Applied on actions that shouldn't be invokable directly through the browser. Use this when the action returns inline HTML markup e.g. partial views. Can only be called via Action or RenderAction HTML extension methods. |
System.Web.Mvc.NonActionAttribute | Use this when you don't want the MVC framework to treat a method in your controller class as an action. By default, the framework will treat all public methods in a controller as an action method. |
System.Web.Mvc.OutputCacheAttribute | Used to cache the output of an action. Has a number of useful named parameters, one of which is "Duration" that lets you specify the number of seconds to cache the output for. |
Saturday 3 March 2012
NDataFlow - Open Source .NET Dataflow Library
class Program : DataflowComponent { static void Main(string[] args) { new Program().Run(); } //First method in the flow [Operation] public void DataSource(OutputThe example shows that there is little work needed to get a simple dataflow setup and running. You inherit the Run method by deriving from the NDataFlow.DataflowComponent class. Then, if you've setup your method and attributes correctly using the LinkAttribute it's a simple case of calling Run to start your dataflow. In this case, the first method in the dataflow would be DataSource, whose output is sent to FilterForLondonPeople and finally whose output is sent to the OutputResults method.output) { //Imagine retrieving people from a real data source //e.g. database, xml file, csv file, etc. output.Emit(new Person() { Name = "Alice", City = "London" }); output.Emit(new Person() { Name = "Bob", City = "New York" }); output.Emit(new Person() { Name = "Foo", City = "London" }); output.Emit(new Person() { Name = "Bar", City = "Sydney" }); } [Operation] public IEnumerable FilterForLondonPeople ([Link("DataSource")] IEnumerable input) { return input.Where (p => p.City.Equals("London", StringComparison.InvariantCultureIgnoreCase)); } [Operation] public void OutputResults ([Link("FilterForLondonPeople")] IEnumerable results) { using (var sw = new StreamWriter(@"C:\LondonPeople.txt", false) { foreach (var p in results) sw.WriteLine("{0} {1}", p.Name, p.City); } } }
Friday 10 February 2012
C# How To: Write a Fluent API
{
public string FirstName { get; private set; }
public string Surname { get; private set; }
private Person() { }
public static Person New()
{
return new Person();
}
public Person SetFirstName(string firstName)
{
this.FirstName = firstName;
return this;
}
public Person SetSurname(string surname)
{
this.Surname = surname;
return this;
}
public override string ToString()
{
return string.Format(
"I am {0} {1}",
this.FirstName,
this.Surname);
}
}
.SetFirstName("Ravi")
.SetSurname("Singh");
person.FirstName = "Ravi";
person.Surname = "Singh";
I personally prefer the fluent version. Earlier in the post, I mentioned that I had a mixed feeling - this mainly stems from the fact that it becomes difficult to debug code that uses a fluent interface. For example, if you have a statement in which a number of method calls are chained on an object, it's difficult to see the intermediary values returned by each method unless you step into each method call. This can be a bit of a pain when debugging. Other than this one problem, I've enjoyed working with fluent-based API's and will strive to write classes in a more "fluent" way.
Saturday 4 February 2012
C# Array ForEach Method
This post is primarily about the Action<T> delegate. You can use this delegate to store references to methods that return nothing (void) but have parameters. There are sixteen versions of this delegate, each taking an added parameter of type T (meaning you can store references to methods of up to sixteen parameters).
Both of these delegate types are provided as a convenience for the programmer. You could easily define your own delegate for your methods but it's now probably better practice to use Action<T> or Func<T, TResult> whenever you can. The example I wrote below uses Action<T> to create a nice little extension method for C# arrays (System.Array). It mimics the ForEach method on System.Collections.Generic.List<T>...
public static void ForEach<T>(this T[] sourceArray, Action<T> action) { foreach (T obj in sourceArray) action(obj); }You now have a cool way to perform an action on each item of an array without having to iterate through your array using a for loop or a foreach construct. The example use below defines a string array, then calls the ForEach, passing it a lambda expression that prints each item to console :
string[] cars = {"Porsche", "Ferrari", "Mercedes"}; cars.ForEach(c => Console.WriteLine(c));You could also pass in an anonymous method or a named method if you don't like lambda expressions. The following example call uses an anonymous method:
cars.ForEach(delegate(string car) { Console.WriteLine(car); });
Sunday 29 January 2012
C# How To: JSON Serialisation and Deserialisation
Imagine we have a simple class that models a person:
internal class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }We can create an instance of this class and then serialize the object into a JSON string using the JsonConvert class as follows:
var inputPerson = new Person() { FirstName = "Alan", LastName = "Turing", Age = 41 }; string json = JsonConvert.SerializeObject(inputPerson);The json string variable now contains the value:
{"FirstName":"Alan","LastName":"Turing","Age":41}
Reconstructing the object back from a JSON string is as simple as:
var outputPerson = JsonConvert.DeserializeObject<Person>(json);
Thursday 19 January 2012
String.ParseCsvToList Extension Method
public static List<string> ParseCsvToList(this string source) { var valuesArray = source.Split(new char[] {','}); return valuesArray.Select(value => value.Trim()).ToList(); }