Friday, October 28, 2011

Enabling Dependency Injection In Web Forms Pages and User Controls using Spring.NET

In one of the previous post, I mentioned, How MVC3 and Web Forms can exist side by side in one project.  We also saw, how we could Enable Dependency Injection using Spring.NET in an MVC3 Application.

But, What about the old Web Form pages and user controls?  Can we Enable Dependency Injection in them, so that we can create loosely coupled code?

Surely there is a way of doing this using Spring.NET.  In this post we will carry on from where we left and we will try to enable Dependency Injection even for Web Pages and User Controls.  Before moving any further, if you have not already seen my previous post of integrating Spring.NET with MVC3 Application, now is a good time to see it.

Without wasting any more time, let's see how we can enable Dependency Injection in Web Forms and User Controls using Spring.NET

How Do they Do it?

For sake of this post, let's say we have a web page called Default.aspx under the Pages directory.  The structure of the project looks somewhat like this

The project structure with Default.aspx
Our task is to inject the AccountService into the Default.aspx page.  As mentioned in the previous post AccountService is a class that was already configured in Spring.NET, it has one method UserCount and we are going to show the user count on the Default.aspx page.

The steps to enabled dependency injection in web-forms web pages using Spring.NET are as follows:
  • Add HttpHandler, called Spring.Web.Support.PageHandlerFactory for path *.aspx
  • Make code changes to Default.aspx to fetch the UserCount from AccountService
  • Configure the dependencies of Default.aspx in web.xml (web.xml is a spring config file which we created in the previous post)
Let look at each step in more detail.

Step - 1 - Add HttpHandler Spring.Web.Support.PageHandlerFactory for path *.aspx

This step is pretty straight forward all we need to do is, Add an HttpHandler, called Spring.Web.Support.PageHandlerFactory.  We need to configure it against the path *.aspx.  This is required so that, Spring.NET gets an opportunity to perform Dependency Injection when a request comes for any web-forms web page. 

We need to add the handler to the <httpHandlers> tag in the web.config.  Updated web.config looks like

Step - 2 - Make code changes to Default.aspx to fetch the UserCount from AccountService

Lets look at the Default.aspx page
As you can see the html code of Default.aspx is very simple. It just has a label called userCount which will hold the value of number of users in the system.

We will get the value of userCount from AccoutService. Let's look at the code of Default.aspx.cs file The code of Default.aspx.cs has no surprises, it has a property AccoutService. We will plan to injet an implementation of AccoutService using Spring.NET.  In the Page_Load method, we simply set the userCount label with the value returned by the UserCount method of AccoutService.

The only question left to answer is, how do we inject the AccoutService into the Default.aspx.cs. 

Step - 3 - Configure the dependencies of Page Default.aspx in web.xml


For configuring the AccoutService dependency in Default.aspx we have to add the following object declaration in web.xml
Notice that we simply have to add the declaration of Default.aspx with proper path in web.xml. Since, Default.aspx is located in the Pages directory, we have to refer it as ~/Pages/Default.aspx. We have to add all the dependencies for the page, just like how we do it for other Spring.NET objects.

All set, lets test our setup and browser the page Default.aspx to see the result.
Enabled Dependency Injection in web.forms using Spring.NET
As you can see, we are able to see the user count value on the page.  We have successfully Enabled Dependency Injection in Web-Forms Web Pages.

Lets quickly see how we can enable dependency injection in User Controls.  Steps to do that are as follows:
  • Create a user control called UserCount.ascx that invokes AccountService to get the user count
  • Update the Default.aspx to use the UserCount.ascx user control
  • Configure the UserCount.ascx in web.xml
Step - 1 - Create a user control called UserCount.ascx that invokes AccountService to get the user count 

We are going to create a very simple user control.  Let's call it UserCont.ascx.  This user control will simply fetch the user count using the AccountService and show it on the UI.
The code of UserCount.ascx is very simillar to what we have already seen in Default.aspx.  We are setting the value of userCount label by invoking the UserCount() method of AccountService instance.

Notice that, we are not instantiating AccountService anywhere in the user control. We will inject it using Spring.NET.  Before we look at, how exactly we do that lets update the Default.aspx to make use of the UserCount.ascx user control.

Step - 2 - Update the Default.aspx to use the UserCount.ascx user control

Let look at the updated Default.aspx
Nothing very alarming here. We simply are including the user control UserCount.ascx and showing it on the html.

Step - 3 - Configure the UserCount.ascx in web.xml

The final step is to configure the UserCount.ascx in web.xml.  Here is how we do it.
Notice that we registered the UserCount.ascx in Spring.NET configuration file. We simply have to specify the complete path of the UserCount.ascx.  Notice that we didn't have to change the configuration for Default.aspx at all.  Spring.NET is smart enought to inject the correct instance of UserCount.ascx in Default.aspx.

Another important thing to notice is that we have to add abstract="true" to the declaration of UserCount.ascx. 

All set, let's see the setup in action


Enabled Dependency Injection in user controls using Spring.NET
As we can see, we see two lines on the page showing the UserCount, first one is getting the user count directly from Default.aspx web page, while the second line is coming from the UserCount.ascx user control.

Mission Accomplished!

Saturday, October 22, 2011

2300 Miles US Road Trip - Part - 2

As mentioned in the previous post, we recently took a vacation in the US.  The previous post is about, my experiences on the trip in the first week.  In this post, I plan to share my experiences on the trip for the second week.

On a nice Sunday morning we decided to start our road trip. 

Destination Niagara:

We started our road trip from Chicago towards Niagara.  The distance of about 570 miles.  We were 4 adults and one kid traveling in an Honda Odyssey.  The drive took around 10 hours.  On the way we saw some very beautiful landscapes.  Here are a few images
Giant White cloud

Moon in all its glory

Metal Bridge on the way
Finally, at about 2030 hrs we reached our hotel at Niagara Falls.  I was really exited to finally see the Niagara Falls, I had read about them seen them on Discovery Channel, but as they say
Seeing is believing!
We decided to freshen up and grab a quick dinner and go straight to the falls to enjoy the night view of the falls.  I could hear the falls when we were at a distance of 0.5 miles (approx), simply thinking about the force of the falls was making me even more curious and exited.

When we reached the falls, I could not believe my eyes!  The falls were Beautiful, Gigantic, Fast and I don't have enough adjectives to describe them.  During the night, the falls are illuminated with lights of different color.  The lights, along with the mist created by the falls, make falls an amazing site to see.  Here are a few images
Fast moving falls along with very powerful floodlights

The lights keep changing their color which makes the scene even more beautiful.


On the other side where you see a tower like structure is Canada.  The two friendly countries are seperated by one bridge of around 0.2 miles.

The Bridge That connects US and Canada

The side from where the lights are flashed is Canada

The big tower on the Canadian side is a revolving hotel
After spending quite some time at the falls, we decided to head back to the hotel so that we can catchup on some sleep, the next day was going to be a long day

The next day, we start on a mission to explore Niagara Falls.  We decided to see a documentary on the history and mystery of Niagara Falls.  It was quite an interesting movie, did you know that, One Ms. Annie Edson Taylor, locked herself and her pet cat in a barrel and fell over the Niagara Falls and survived the fall! The movie was well scripted and was a fun watch!

Next we took a ride on the Maid of the Mist.  Its basically a boat ride that takes you very near the falls so that you can enjoy the view of the falls from its basin.

The Maid of the Mist boat that takes you near the fall

The American Falls And Bridal Veil Falls

The Horseshoe Falls

The Horseshoe Falls
 The boat ride was fun not only because we got to enjoy the beauty of the falls from very near but also because of hundreads of seagulls were flying around the falls. The entire landscape was really beautiful!
Seagulls flying around the falls
Next adventure was to go as near the fall as possible.  This time, a walk in the Cave of the Winds.

The Americans have build a wooden platform, just about 3 meters away from the falls, once you are there, you are bound to get completely drenched.  We experienced the force of falls pounding on top of our heads even when we were 3 meters away!

Cave of the Winds - The wooden platform 3 meters from the falls


Nearest point - You can't go any closer to the falls.  The force of falls was enormous.
As you can see from the images there is no way one can get out dry even with a raincoat on.  It was total fun!

We decided to spend the evening at the falls and enjoy its beauty, did a nature trail around the falls, overall had a fun filled day.  Here are some evening snaps of the Niagara Falls.


You can even see the dark spots on the moon

Destination Virginia Beach:

Next day, we decided to continue our trip.  Today's destination was Virginia Beach.  The distance of Virginia Beach from Niagara Falls was about 640 miles.  The journey took around 12 hours.

Our Hotel room had an awesome view.  The hotel was right in front of the beach.  There was nothing in between the hotel room and the beach!  We could see the beach from our hotel room terrace.  It was an amazing site!
Snap click from the hotel terrace
We could hear the waves from our room, the feeling was fabulous!  Next morning, I woke up early so that I could click some snaps of the sunrise 
Beautiful dawn at Sea
The sunrise and the reflection of sun in the ocean
Watched people feeding the seagulls, cycling at the beach, we even saw a few dolphins

The lady feeding the Seagulls

People cycling by the beach side

The small black spots that you see, are dolphins
I was pretty interested in doing some adventure activities at the beach, one activity that I wanted to do since a long time was Parasailing, we found a place and went for it.  The best part of Parasailing activity was when they dip you into the ocean!

Getting our feet wet while parasailing


Once we were back on solid ground, I saw my second love

Nice and Shining Harley Davidson
Yes, Harley Davidson, I had an opportunity to ride this bike once and from there on, I have fallen in love with it!

Destination Washington D.C.:



On the same day at about 1700 hrs we started our journey towards Washington D.C.  A journey of about 230 miles took us around 5 hours (caught in a jam on our way).

Next day we had a big list of places to visit.  We started early we saw the Capitol Building, White House, Lincoln Memorial, World war II Memorial, Washington Monument and finally the Air and Space Museum.

All these places are pretty close to each other.  One can easily walk and visit them.  But at the end of the day, I felt like I had no legs, I was dead tired.
The Capitol Building

The White House

Lincoln Memorial

Big Statue of Abraham Lincoln in the Lincoln Memorial

World War - II Memorial

Washington Monument


Air and Space Museum
Out of all these places, I liked the Capitol building and Air and Space Museum.

End of Journey:

Could not believe that it was already time to head home.  After a week of fantastic trip and wonderful memories it was time we start our journey towards Chicago.  A journey of about 750 miles took us around 13 hours.  On the way back, we also got a ticket for not wearing seat belts!

Had lots of fun with some very different/beautiful experiences (one of the "different" experience on the way back was, I lost my checked-in bag!)

Overall it was a fantastic and memorable trip!

Sunday, October 9, 2011

2300 Miles US Road Trip - Part - 1

Recently me and my family had an opportunity to visit the US for two weeks. I had some official work for about 4 days. We had planned a vacation for the rest of the time. Naturally, I wanted to visit lots of places, do lots of stuff when I was in the US. Some of the top places I wanted to visit were Niagara Falls, The White House, Virginia Beach, Pennsylvania, Florida, Yellowstone National Park, Hoover Dam, Las Vegas, The Grand Canyon, Sears Tower (Willis Tower) etc.

The list was endless and I knew we cannot cover all these places in just seven days. We had to decide on places that were practically possible and onces that I wanted to visit the most. We decided to do a road trip to some of the most appealing places (read that as places that were practically possible :)). We ended up doing a 2300 miles fantastic road trip, here is a post that summarizes my experiences on the entire trip and things that I loved the most.

Our journey started from the Pune airport.  We were supposed to fly from Pune to Delhi to Chicago.  We were visiting the Delhi Airport for the first, we had approximately 5 hours between flights, I thought that will be a good time to see the Delhi Airport.  I had heard lots of good things about it and boy, I was in for a surprise.

The Delhi Airport is one of the best International Airport that I have ever been to.  By the way, I have been to a lot of international airports (Dubai, Bharain, Addis Ababa, Israel, Frankfurt, Paris, Berlin, Zurich, Chicago, Minneapolis, Amsterdam etc.) and I can be sure that, this one was one of the best.

There were things to do, places to visit, shops to be explored, right at the Airport itself.  There are huge corridors, walkways, benches with palm trees around them, tint of indian culture etc.
Benches with palm trees around them

Huge corridors, walkways

Hand postures representing a dance form called Kathak
Out of all the shops at the Airport one that appealed the most to me had a healing pool in it.  Its like a small pool which has water till your ankles, people are supposed to remove their footwear and walk inside the pool.  The tiles in the pool would pressurize certain areas on your sole which gives you a very refreshing feeling.  This science is called Acupressure.  I did try it and felt totally incredible, felt really energized.

The healing pool with lots of bells hanging

As you can see people could walk in the pool and get rejuvenated
In the same shop there was live entertainment going on.

Person on left is playing flute and the other guy is playing tabla
The person with a long bamboo stick is playing a musical instrument called Flute and the other guy is playing a traditional Indian instrument called Tabla.  The sound of these instruments is very soothing and relaxing.

All around the Airport, there are huge beautiful metal sculptures which depict Indian culture

The Sculpture of lord Shiva
Delhi Airport had a western look as well.  There were recliner chairs with charging pods in the chair itself.  Nice, Shiny Mercedes was also lying around at the airport.  Over all it was a surprising experience.
Mercedes at the Delhi Airport

Next stop Chicago:

At Chicago on Monday - Labor Day - morning we visited a Swami Narayan temple near by.  The temple had a rich white facade with lush green lawn and blue fountains.  Under the clear blue sky with sparse white cloud cover it looked really beautiful!

The Swami Narayan Temple with white facade and lush green lawns

Blue colored fountains
Next stop Minneapolis:

As I mentioned earlier I had 4 days of official work.  I had to leave for Minneapolis on Monday.  But my trip does not stop here.  Even when I was at Minneapolis, my friends at work took me to some very nice restaurants in the evenings.
Pat sitting on the left hand side and Paul on the right


But by far the single biggest attraction of my stay at Minneapolis was the Base ball game!  My friends at Minneapolis had arranged tickets for a Base Ball game!

The game was between the home team i.e. Twins and Chicago White Sox.  Although the home team was not having a great phase they did win this game!  We had a very good view, our seats were right behind the batter, I could click some very good snaps



The road to the office was scenic, our office was around an artificial lake because of this the entire area had very good green cover with beautiful flowers and lovely trees!

Path way to the office


One evening one of my friend took me to the Mall of America!  Its supposed to be the second largest mall in the world and boy it was big!  There were hundreads of shops in fact it was so big that they had a mini amuzement part right inside the mall!  I did sit in one of the roller coster which looked the most scary roller coster it had a drop of around 93 degrees!

Back to Chicago:

After fishing my work in Minneapolis, I came back to Chicago on Saturday morning, we decided to visit Chicago down town in the evening.  Chicago downtown is just like any other city with lots of big buildings and beautiful lake near by.

We visited the Cloud Gate, Buckingham Fountain, Sears Tower, Navy Pier and oh yes, saw the fireworks at Navy Pier

The Cloud Gate is like a giant silver bean, it was quite a popular spot, we saw at least 6-7 newly wed couples near it.
The Cloud Gate





Snaps from the Buckingham Fountain, sears tower and navy pears
Buckingham Fountain


View from the Sears Tower



Fireworks at Navy Pier


At Sears Tower, there is glass compartment which is suspended, which means there is nothing below it - The Sky Deck!  You are standing on glass 110 stories high with nothing below you, Scary!

While heading home we even saw a opera it was quite interesting actually!
By this time one week had already passed.  I had lot of fun and some very first experiences in this memorable one week.

Next day we were supposed to leave for our road trip.  In my next post I will share my experiences about the places that we visited in the coming week!

Stay tuned folks!
Have some Fun!