Skip to main content

Posts

Creating API with MVC ApiController part 3 - moving to asynchronous code

In my two previous posts ( part 1 and part 2 ) I described both simple and more advance approach of creating Rest-full API in MVC 4. Today I want to take another step forward and go a little deeper  in the creation of API. In this post I`m going to describe how we can create asynchronous API functions which bring better performance to our applications. The first thing that need to be understand is an asynchronous operation in .NET Framework. In .NET Framework 4.0 one of the most important changed was introducing Task  class.The Tasks in System.Threading.Tasks namespace are a method of fine grained parallelism, similar to creating and using threads, but they have a few key differences and the main difference is that Tasks in .NET 4.0 don’t actually correlate to a new thread, they are executed on the new thread pool that is being shipped in .NET 4.0. More about task you can read here  but to understand this article all you need to understand about the Task<T> class is that th

Creating API with MVC ApiController part 2

In my previous post  I wrote about first steps in creating Rest-full API by using ApiController . Now it`s time to make next step and go a little bit dipper inside web services created in MVC. In this post I want to describe two very important aspect: creating a real life scenario for web service implementation of POCO entity extend presented scenario and make it asynchronous To complete this tutorial one more class is needed. This class is a simple fake of some database which  is wrapper around a very few collections and allow all CRUD operation. Moreover the implementation of this fake database uses a singleton design pattern to prevent creating instance of it each time and maintain state between web service calls. Code Snippet /// <summary>          /// Represents a fake database.          /// </summary>          public sealed class FakeDbContext         {              private static volatile FakeDbContext instance;              private

Creating API with MVC ApiController part 1 (with introducing to MVC design pattern)

Introduction to MVC In the last few years a software architecture changed very heavily. From desktop (forms) application which were very popular in Windows XP users move to SOA and now completely start using web based solution hosting in a cloud . Of course such evolution is not something bad, just the opposite in my opinion because web based application are highly scalable, better secured and available from each place in the Earth where user has an Internet connection. Such a big changes in software architecture cause that also programming model has to change. As response for this need, IT companies start working for on new technologies and improving existing ones. APIs became very popular tool  in a developer hands and whole communication became much more lightweight (by switching from XML to JSON) - mainly because the emerging market of mobile devices. In a new MVC version 4 also Microsoft introduce some improvements which allow developers creating web services (APIs) in well kn

Asynchronous actions in ASP.NET

An asynchronous operations become very popular in modern programming because by using its developers can take full advantage of multicore processors and perform several operation at the same time. Multithreding exists in ASP.NET since 2.0 version but it was very sophisticated to use it. However starting from .NET 4.5, ASP.NET is fully compatible with all these great features . To demonstrate how to start with the asynchronous operation in ASP.NET 4.5 I`ve created very simple solution which consist of three projects (Picture 1.): AsyncPageService - REST service based on a ApiController ; provides function which will be called by the website. AsyncPageWebsite - main ASP.NET website. Contracts - contains shared type between service and website. Picture 1. Solution projects. In my solution data is served by very simple REST web service, which is build by using  ApiController  from ASP.NET MVC. Inside this I`ve created two controllers (first return N random integers and the sec

Finding gaps in time

Let assume that we want to create a simple scheduler which allow user to plan meeting as long as he want with 1 minute precision. There is multiple ways to make such implementation but in this post I want to present an approach I used recently. In my implementation I`ve created separated type which represents any time period. I called it Range and it`s consists of three properties (one is read-only). Main idea behind creating this time is encapsulate state and end date of used defined time period. /// <summary> /// Represents range in time. /// </summary> public class Range { /// <summary> /// Gets or sets represents range start time. /// </summary> public DateTime StartTime { get ; set ; } /// <summary> /// Gets or sets represents range end time. /// </summary> public DateTime EndTime { get ; set ; } /// <summary> /// Gets ra