Choosing the right abstractions

Choosing the right abstractions can be hard.   Lets take a good look at the next piece of code. 1: public class WrongAbstraction 2: { 3: public void Execute() 4: { 5: IEnumerable<Product> products = GetProducts(); 6: PrintProducts(products); 7: } 8:  9: private void PrintProducts(IEnumerable<Product> products) 10: { 11: foreach (var product in products) 12: { 13: Console.WriteLine(product.ProductName); 14: } 15:  16: Console.WriteLine("Total number of products {0}", products.Count()); 17: } 18:  19: private IEnumerable<Product> GetProducts() 20: { 21: using (var ctx = new NorthwindEntities()) 22: { 23: return ctx.Products.ToArray(); 24: } 25:  26: } 27: }   That looks quite … Continue reading Choosing the right abstractions

Unit testing a ASP.NET WebAPI 2 controller

Earlier this year I wrote a blog post about how to unit test ASP.NET WebAPI controllers. It turned out that in order to do a good job of testing the public ApiController methods you would need to quite a bit of setup. Most of the time this was just about providing a HttpConfiguration object but sometimes, like in the case of responding to a post request, quite a bit more would be required. One of the goals of the ASP.NET WebAPI 2 was to make testing of controllers easier. This has been achieved by adding a new response interface named … Continue reading Unit testing a ASP.NET WebAPI 2 controller

Displaying local times using the HTML5 <time> element and Moment.js

One common problem with displaying local dates an times in a browser is that you get no information about the current time zone a browser is in. The browser will tell you the preferred language through the Accept-Language HTTP header but there is no such header for the users time zone. This means that it is easy enough to display a DateTime in the servers time zone but not in the users time zone. Now if your server is in the same time zone as your users this isn’t much of an issue.. However that is quite often not the … Continue reading Displaying local times using the HTML5 <time> element and Moment.js

Unit testing code depending on the ASP.NET WebApi HttpClient

In a previous post I showed how to unit test an ASP.NET WebAPI Controller. But with a REST service there is both a client and a service component. Assuming for a moment the client part is also written in C# we should test that as well. In this case the client application contains the following class to load books from the REST WebAPI controller: 1: public class BooksClient 2: { 3: private readonly HttpClient _httpClient; 4:  5: public BooksClient(HttpClient httpClient) 6: { 7: _httpClient = httpClient; 8: BaseUrl = new Uri("http://localhost:63895/api/books/"); 9: } 10:  11: public Uri BaseUrl { get; … Continue reading Unit testing code depending on the ASP.NET WebApi HttpClient

DotNed podcast: Ronald Harmsen over Async/Await en Tasks

In deze podcast spreekt Maurice de Beijer met Ronald Harmsen. Ze hebben het over de nieuwe async en await keywords in C# 5. Daarnaast hebben ze het uitgebreid over verschillende vormen van asynchroon programmeren zoals de ThreadPool, Task en IO completion ports. Links: Blog : http://www.ronaldharmsen.net/ Task Parallel Library and Servers: http://bradwilson.typepad.com/blog/2012/04/tpl-and-servers-pt1.html Scaling Up with Task Parallel Library: http://aviadezra.blogspot.nl/2009/04/task-parallel-library-parallel.html Heisenbug: http://en.wikipedia.org/wiki/Heisenbug http://channel9.msdn.com/Tags/task+parallel+library http://channel9.msdn.com/search?term=ronald+harmsen Met dank aan onze sponsor RedGate.

Using SignalR for real time communication on the web

In a previous blog post I mentioned how exited I was about WebSockets and the future with real time duplex communication over the Internet. Unfortunately the current support for WebSockets, both on the client and on the server, is still somewhat limited making this a thing of the future. Does that mean we can do this yet? Not quite.   SignalR to the resque Even if a pure Web Socket solution isn’t possible yet there are perfectly good alternatives. And the one I really like is SignalR as it allows for the same kind of application today. Check out a … Continue reading Using SignalR for real time communication on the web

Query composition with the ASP.NET Web API

Having the ASP.NET Web API as a REST service returning data is kind of nice but to be efficient on the wire we don’t want to return more data that required only to discard it in the client.   As we have seen in a previous post just returning a collection data was real easy.   As it turns out changing the service so the client can filter data is almost just as easy. In the previous example we returned an IEnumerable<Product>. All we need to do is change this to return an IQueryable<Product> instead of an IEnumerable<Product> and most … Continue reading Query composition with the ASP.NET Web API