SmoothFriction.nl

posts - 39, comments - 20, trackbacks - 0

c#

c#
TekPub's Mastering LINQ Challenge

I’m always up for a challenge. I still have to watch the Mastering LINQ videos up on TekPub (which is amazing by the way, I encourage you to check it out!), but someone (hi Tuna!) tweeted about a small challenge related to the series. Basically, the challenge is to use a chained LINQ statement to create a prime number filter, without using any custom functions. Without further delay, here’s my solution: var primes = Enumerable.Range(1, 30) .Where(x => x > 1) .Where(x => x%2 != 0 && x != 2) ...

posted @ Saturday, January 09, 2010 12:07 AM | Feedback (3) | Filed Under [ c# LINQ ]

ASP.NET MVC: Tricky modelbinding feature (?)

I just ran into a situation which could lead to very annoying latent bugs in your web application. Observe the following code. The view: <form method="post" action="Photo/Save"> <input type="text" name="Photo" /> <input type="text" name="Title" /> </form> The Controller action: public ActionResult Save(PhotoDTO photo) { // do something with the object return RedirectToAction("Index"); } The class: public class PhotoDTO { public string Photo { get; set; } public string Title { get; set; } }   At first sight, there’s nothing wrong with this code. You’d say when the form is posted, the PhotoDTO object gets...

posted @ Thursday, November 26, 2009 12:24 AM | Feedback (0) | Filed Under [ c# asp.net mvc ]

About the .Less project

I had a short twitter conversation with Aaron Jensen on the decisions we made developing .Less. As I’ve blogged about before, for me personally it was more of a learning project than something with a set goal, at first. That aside, I had one thing in mind: being able to use less during development, without having to worry about it, add a handler and go! Less.Net, v0.1 My first solution was to simply install ruby and wrap the whole system call in a HttpHandler. Very clunky, but it worked! I blogged about this a while ago: Using LessCSS...

posted @ Sunday, November 22, 2009 10:30 PM | Feedback (2) | Filed Under [ general c# asp.net .less ]

A tricky situation with the IEnumerable<T> interface

Recently I ran into a situation which isn't very obvious at first glance if you don't know how the Linq to objects extension methods work in C#. If you know how the IEnumerable<T> works in combination with the various extension methods in the System.Linq namespace however, you’ll quickly see what'll go wrong. Observe the following snippet: List<int> source = new List<int> { 1, 2, 3, 4, 5, 6 }; var filter = source.Where(x => x > 4); foreach(var listitem in filter) { // do something with the value // source.Remove(listitem); } This will throw an InvalidOperationException, stating that the collection has been changed. If you don’t...

posted @ Friday, November 06, 2009 5:29 PM | Feedback (0) | Filed Under [ c# ]

ASP.NET MVC: Access an action only by ajax

In a standard ‘web 2.0’ (god, I hate that buzzword) web application, you’ll often have ajax calls to your code. I prefer using jQuery, but any method will do. A proper ajax request will set a header value in the HTTP GET request, and ASP.NET is a good boy and catches that. By checking if the IsAjaxRequest() method on the request returns true. This feature can be utilized for something I like to do on my ajax-exclusive controller actions: restrict access in such a way that only ajax requests can call the action. Other actions will get a 404,...

posted @ Monday, October 05, 2009 12:00 PM | Feedback (1) | Filed Under [ c# asp.net mvc ]

ASP.NET MVC: Common viewdata

One issue I’ve heard a few times already when explaining ASP.NET MVC to others is how to deal with common viewdata which should be available to every page. Sure, you can simply add it to the viewdata in each and every controller action you call, but that’s a horrible violation of the DRY (Don’t Repeat Yourself) principle. It’s also very error-prone, because you’re very likely to forget to add it some time or another. A more elegant solution to the problem is to have a base controller class which you will use for all your controllers. This is a...

posted @ Friday, October 02, 2009 1:50 AM | Feedback (1) | Filed Under [ c# asp.net mvc ]