Sunday, August 31, 2014

How to add NOLOCK hint to EntityFramework Queries

We were using EntityFramework (don't ask me why, but yea thats a reality) in one of our projects as the ORM tool.  In one of the obscene query generated by EntityFramework, we wanted to make sure that it does not acquire any locks on any tables involved.

Obviously, we should be running such a query using the ReadUncommitted transaction isolation level.  However, in certain cases that is really not an option.  In such a situation we need to add the NOLOCK hint explicitly to the generated query.  This post is about adding the NOLOCK hint to any query generated by EntityFramework.

How Do They Do It!

The solution that I found was pretty elegant and nicely abstracted in one class.  The solution described in this post is taken from this SO post.  Here are the steps involved in getting the job done.
  • Create subclass of DbCommandInterceptor
  • Override two methods in this derived class ScalarExecuting and ReaderExecuting
  • These methods will be called by EntityFramework just before executing the generated query.
  • In these methods we get an opportunity to update the entity framework generated query and add the NOLOCK hint if required.
Heres the code that will get the job done.
Basically all we are doing here is, looking for a pattern which matches table names in the entity framework generated query and replacing all the table names with <TableName> WITH (NOLOCK) hint.  Thats all that is needed really!
The thread static variable ApplyNoLock is kind of a switch that, controls whether the NOLOCK hint is to be applied or not.

The Interceptor can be added during the application start as follows
Thats all folks, its really a simple and elegant way to add the NOLOCK hint to any query generated by EntityFramework!

Thursday, July 31, 2014

How to generate strongly typed C# client for ASP.NET Web API

Recently, I was looking out for ways to consume the ASP.NET Web API via strongly typed C# client (i.e. a client similar to C# Web Service Client).

Initially I thought, Microsoft would have a utility to generate the client but, I was wrong, they don't have any such utility.  Why would they not provide such a utility is beyond me, I really don't understand why would they not expose such a utility, it would have benefited everyone who wants to consume Web API using strongly typed classes.

None the less, After a bit of googling, I found this awesome open-source project which does exactly what I needed.

The detailed instruction on how to use it can be found here.  In summary you need to do the following steps to generate the strongly typed C# client for ASP.NET Web API


How do they do it!

Setting up the Server
  • Install the NuGet package "WebApiProxy" into the project that hosts the Web API.  This package will host a proxy endpoint for your service.  This proxy endpoint can be used as the JavaScript client to invoke the Web API via JavaScript.  The endpoint will be hosted at URL "/api/proxies"
  • Once you have this installed in your Web API, if you browse the URL "/api/proxies" you should see a JavaScript client already generated!
  • This package also exposes the metadata necessary to automatically generate the C# client.
Creating the client
  • Create a Client project which will be used to invoke the Web API via C#.
  • Install the NuGet package "WebApiProxy.CSharp" into this project.  This package seamlessly integrates the proxy generation build task with the project build step.  
  • Every time you rebuild the project it will try and generate the C# client for the desired Web API.
  • The configuration required to generate the C# client is placed in a file called "WebApiProxy.config"
  • This is the simplest XML file I have ever seen.  It only expects you to provide the 
    • endpoint - The location of the Web API where it will be hosted.
    • namespace - The namespace in which the C# client will be generated.
  • That's about all the setup that is needed to generate the strongly typed C# client for a given Web API
Example
  • Imagine you have an EmployeeController on the Service side. 
  • We can invoke this Web API via our generated client as follows
  • As you can see its very straightforward and intuitive to invoke the Web API with strongly typed C# client.
Further Reading
  •  I went ahead and modified the source code to generate not just the Async method but also the Sync version of the Web API.  
  • This is done so that anyone who wants to invoke the Web API in a Sync manner does not have to deal with the Async behaviour.
  • The above code for Sync invocation can be reduced to the following
  • Updated source can be found here
  • TIP: In case if you are trying to modify the source to suite your needs, make sure that you kill the MSBuild process before you rebuild the Task and the Client.
Thats all folks!  

Monday, June 30, 2014

How to run Entity Framework Migrations using MSBuild and Migrate.exe

Recently, one of out client moved to Entity Framework.  Along with that, they also decided to use the Entity Framework Migrations.

On a DEV machine, Entity Framework Migrations can be executed very easily using the the Update-Database command in Package Manager Console.

Naturally, we do have a Continuous Integration (CI) server that builds and integrates the checked-in code.  We needed to run the migrations on the CI server as well.  For building our project we were using MSBuild.  I wanted to integrate Entity Framework Migrations with MSBuild so that the migrations can be executed outside the Visual Studio and on the CI server as well.

Looked like a no brainier task to me, I hoped that there will be an MSBuild task that would run the Entity Framework Migrations and my job would be done!  But to my surprise, Entity Framework Migrations is not fully thought through, it does not come with any task for MSBuild which can be executed out of the box.

Turns out that there is a Migrate.exe that could be used to execute the migrations.  This could then be invoked from MSBuild to get the job done.  So how do they do it, how to run Entity Framework Migrations using MSBuild and Migrate.exe

How do they do it!
  • Migrate.exe can be found under the tools directory of the Entity Framework package that comes along with NuGet.
  • We could create a simple task that can invoke migrate.exe using the Exec MSBuild task. 
  • However, simply running the above task will not get your the desired results.  When you run this task you might end up getting some weird exception.  I got the following exception
  • Turns out that to successfully run Migrate.exe, it needs to be copied to the directory where the DLL holding all the Entity Framework Migrations is located.
  • I don't understand why such a restriction is necessary.  Why could they not use the StartUpDirectory parameter or some other parameter to accept the full directory path of the DLL that has the migrations.  May be when someone wrote the code for Migrate.exe, he/she found it convenient to run it this way!
  • Anyways, so the fix to this problem was simple, first copy the Migrate.exe to the folder where the DLL is located and then run the Migrate.exe
  • Updated target code looks as follows.
As you can see, its not very straightforward to get the Entity Framework Migrations integrated with MSBuild, but with some hacks, we can get it working! 

Saturday, May 31, 2014

The over prized indian elite fresher

From the title its pretty obvious that this is going to be a very controversial post.  So, before I move anything further, let me make it very clear that views expressed in this post are my personal view and are by no means intended to harm the reputation of any organisation or individual.

Recently, I had an opportunity to work with some of the elite engineers who are fresh out of the college.  The colleges they have been too are some of the topnotch colleges of India.  Companies have to pay big money to hire them.

For the sake of this post, we can divide these engineers into two categories, the top 10% and bottom 90%.  Top 10% are some of the most "beautiful minds" of the country.  They are really of topnotch quality and surely value for money.  We will not talk about these 10% engineers in this post.

My concern is the long tail of bottom 90% engineers and this post is about them.  This post is not to criticise anyone, the intent of this post is to provide some advice that can help these folks.

What makes me qualified to give any advice?  

Nothing really, I am a nobody and my advice are just my views, whether anyone wants to follow it or not, is totally onto them.

Let's start looking at some of the concerns.

Long way to catchup

I think the colleges need to make a conscious choice here.  The curriculum of the colleges have almost been the same since it was first designed.

The IT industry moves at a very fast pace, what was cool and hip today becomes old and disgusting tomorrow.  Colleges need to take continuos feedback from the industry, incorporate relevant changes that into their curriculum.  New innovations made in the industry should be inducted at the grass root level.

If we do this then the engineers coming out of college will be better prepared for working in the industry.  Companies will also be benefited because they will not have to spend time, money and energy behind training the fresh graduates (at least cost to make these engineers productive will reduce significantly).

Hunger to learn

Some of these folks think that, because they finished college, their days of learning new things are over.  Now is the time is to chill out and relax!

I am sorry to say this but reality does not coincide with this.  If you really want to be a good developer then, the days of learning new stuff will never end.

It again boils down to the fact that, our industry moves at a very fast pace.  If we want to be good developers all our life then, we got to learn new things day in and day out no matter where you pass out from.

One needs to have a strong hunger to keep learning, as Mr. Jobs says "Stay Hungary, Stay Foolish"

An open mind

Fresh engineers are at the very beginning of their career.  One needs to have exposure in variety of tools and technologies to be a good developer.  Working on multiple technologies broadens our horizon.

Do not walk away from something because you have not done that in the past.  There will always be "first time" for anything that you have ever done in life.  Welcome every opportunity to work on something that you have not worked on!

Keep your feet on the ground

These engineers are passing out from some of the most elite colleges of India.  It is natural to have very high expectations, but just because one has passed out from a very good college does not mean they that you will never struggle in their carrier.  Be humble and modest, ask yourself just one question, Am I really worth everything I am getting?  

In all likely hood the answer to this question will be "No".  Work hard enough to turn the answer to this question from a "No" to a "Yes"

Too much philosophy, I guess.  But these were some of the points I wanted to put out in the open to get an opinion of others from the community.  Until we meet again, "Work Hard, Have Fun, Make History!"

Sunday, April 27, 2014

How to setup a fixed shutdown sequence with Spring

Recently, I was faced with an interesting problem.
  • Our application server was listening to a queue
  • The queue used to receive messages from different sources
  • The requirement was very simple, when the application server is going down (this could happen because of many reasons, may be because we brought the site down, or we are deploying an updated software on to our application server) we do not want to miss any message from the queue.
To achieve this we just need to obey the following rules when the application server is going down.
  • We should stop consuming any further messages from the queue.
  • We should wait for the processing to complete for all messages that have already started processing.
  • After these two rules are satisfied we should continue with the shutdown of the application server.
To implement these rules successfully, we need a way to hook into the shutdown sequence of our application server.  Our application was using Spring and I had a feeling that Spring must have a way to do this pretty easily.

After googling for a while, I found the perfect solution for our situation.  The solution was pretty simple and extremely elegant.

How Do They Do It!

Spring framework provides a callback interface called SmartLifeCycle.  This interface has a bunch of lifecycle methods that get called when the application server is booting up or shutting down.

All we need to do is to create a class that implements this interface and register it with the Spring context.  Spring guarantees to notify this bean about various events that occur during the lifecycle of the application server.  

This interface provides methods like, start, stop, isRunning etc. please refer the JavaDocs for more details.

To implement the above rules all we did was to create a bean called ApplicationLifeCycle that implemented the SmartLifeCycle interface.  I could implement the above rules in the stop method of the bean.

However, this still kind of solved only part of the problem.  I also wanted to make sure that my bean class ApplicationLifeCycle was the first class that was notified when the shutdown was initiated.  This was the missing link.  

The SmartLifeCycle interface provides a solution to this problem as well.  This interface extends another interface called Phased.  The Phased interface exposes a method called getPhase which returns an int value.

From the documentation it was not very clear what was the purpose of this method and what should be the value returned from this method.  Turns out that, this is an extremely useful method.  Spring uses this method to determine the order in which the beans will be notified about the life cycle events.  
  • Let's say we have two beans ApplicationLifeyCycle1 and ApplicationLifeCycle2.  
  • These beans both implement the SmartLifeCycle interface.
  • ApplicationLifeyCycle1 returns a value 10 from the getPhase method
  • ApplicationLifeyCycle2 returns a value 20 from the getPhase method
  • When the application server starts, spring will invoke the ApplicationLifeCycle1 "start" callback before calling the ApplicationLifeCycle2 "start" callback method.
  • When the application server stops, spring will invoke the ApplicationLifeCycle2 "stop" callback before calling the ApplicationLifeCycle1 "stop" callback method.
At this point, we had found our missing link.  All we wanted to do was make sure that our ApplicationLifeCycle class gets notified that the server is shutting down before anyone else.  All we had to do was return a higher value from the getPhase method.  We ended up returning the highest value possible i.e. Integer.MAX_VALUE and that was it!

Once we did this, Spring guaranteed that our bean will be the first bean to be notified when the server is going down (this is exactly what we wanted), while it will be the last bean to be notified when the server is starting (which we didn't really care about).

The sample implementation of SmartLifeCycle interface looks like this
Thats all folks, until we meet again happy coding!

Sunday, March 30, 2014

Passing CGFloat to methods resets the value of arguments to 0

Recently, I was faced a very weird issue with methods that have CGFloat as argument.

The Problem

We were using cocos2d for our iOS project.  The code was very simple, I was trying to invoke a method of class that takes two CGFloat arguments.

No matter what values I pass to doSomething it always receives 0 or some other garbage value.  Just to be extra sure, I even logged the values of the parameters passed before actually invoking the method.  The values got printed correctly, but the doSomething method always printed 0.

The Explanation

Well, I was stumped here.  This was like the simplest possible code.  Set the value of a variable, pass it to another method and BOOM the value gets reset!

Obviously, this was nothing to do with how I have written the code, others out there must be facing the same issue.  A bit of googling calmed my nerves.

CGFloat is just a typedef wrapper around either a float or a double. Whether it actually is a float or a double depends on the header where CGFloat is defined that your code compiled against.  The CGFloat definition looks something like this

Notice how CGFloat either becomes a float or a double, depending on whether the architecture is 64 bit or not.

All goes well, until you mix code compiled with this flag on, with other code compiled with this flag off (Such as including a binary library with other code). Effectively mixing 64 bit compiled code with 32 bit compiled code.

This can potentially lead to a case where we are passing doubles to methods that are expecting a float.  Since this is all happening at the byte level, no error is thrown and the bytes are just truncated, taking off all of the bits that actually described your value and leaving bits with nothing in them i.e. 0.

In my case, it was happening because I was trying to build a binary for both the Simulator and iPad Air Device.

The Solution

A clean and build should have solved the issue, but I decided to change the method arguments to float instead of CGFloat.  Once I did this change, everything worked as expected!
Thats all folks!  Until we meet again, enjoy your time.


Sunday, February 23, 2014

How to play game voices sequentially when background music is running on iOS

I was recently hacking my way through an open-source iOS game and I was faced with two interesting problem.
  • There are multiple voices and background music that are played at different times.  It was becoming extremely difficult to hear the voices clearly when the background music was turned on.
  • To add to that, there are certain voices that needed to be played in sequence.  For example, when user reaches "Level" "n", we want to play a voice "Level" and then the level number where the user has reached.  For example, if the user has reached Level 3, we want to play "Level" and then "3" since we do not have a distinct voice for every level (may be that's not even scalable).
In this post, we will see how we fixed both this issues using a small wrapper class.

How Do They Do it?

What I ended up doing was to create a small wrapper class that had following main responsibilities:
  • Fades-out the background music to slower volume whenever a voice is playing
  • Fades-in the background music to original volume when voices have finished playing
  • Manage a queue or a playlist of voices which are played in sequence.
We made use of the CDAudioManager class its delegates to implement this class.  Once we had this class in place, it made task really simple.  The code was well encapsulated and we could use it throughout the system consistently.

Without wasting any more time, let's have a look at how actually its done!

Create the Singleton AudioManager

This class needs to act as a service to the application, hence we are going to make it a singleton class.
Initialize Fields
  • Next step is to initializes the CDAudioManager 
  • Build NSMutableArray that will hold the queue of voices for us.  
  • We will also be registering AudioManager as a delegate to the CDAudioManager class.  Hence, we will implement the protocol CDLongAudioSourceDelegate
Enqueue Voices
  • This is where we build the queue of our voices.  
  • If the audioSource is not busy playing any other voice, then we simply play the voice.  When we play the voice we will also make sure that we fade-out the background music to a lower volume.
  • Else we will add the voice to the queue of voices
Implement the Delegate
  • This is where it all comes together.
  • We implement the protocol CDLongAudioSourceDelegate, this protocol has two optional methods
    • cdAudioSourceDidFinishPlaying
    • cdAudioSourceFileDidChange
  • We are only interested in implementing the cdAudioSourceDidFinishPlaying method.  This method will be called every time CDAudioManager finishes playing a voice.
  • Every time this method is called, we know that CDAudioManager has finished playing a voice we can now play the next voice from the queue.
  • If the queue is empty, then we simply fade-in the background music to its original volume.
That's about all that we need to do!  We have successfully setup the AudioManager for voices in our game.  It will nicely play voices one after the other and fade-out the background music when the voice is playing.

For sake of completion and easy understanding, here is the full code for the class
Have some Fun!