JohnBierman.NET
.Net, C#, ASP.NET, SQL Server, and other *kewl* stuff . . .
JOHNBIERMAN.NET

Http Traffic Monitoring via "HttpWatch"

A great utility! Here's what they say about themselves:

"HttpWatch integrates with Internet Explorer and Firefox browsers to show you exactly what HTTP traffic is triggered when you access a web page. If you access a site that uses secure HTTPS connections, HttpWatch automatically displays the decrypted form of the network traffic"

Thanks to Travis Rogers for showing this to me.

http://www.httpwatch.com

EntityFramework 3.5: Adding a New Entity to an Existing .Edmx File

It's been awhile since my last blog posting. To rekindle the *blogging fire* I want to share a little trick that I learned today that will hopefully help the community at large (or at least those EntityFramework fellow compatriots).

The EntityFramework is a great technology framework that raises the level of abstraction for data programming. It offers a great graphical user interface for defining either simple or complex representation of your data. While the GUI is a great tool, some of the kinks have yet to be worked out of the 3.5 version of the framework.

And while it is extremely simple to create a new edmx file and define your entities, this posting will not cover how to do this. I will try to zero in on the *trick* that I learned today

Define the problem:

While creating a new edmx file and defining the entities is easy, it's a whole 'nother animal to update and existing edmx file. What would seem rather simple is not quite as straight forward in this version of the framework (at least in my experience). It would appear that you would only need to open the edmx file, right click on its design surface, and select the "Update Model from Database" menu item. I mean, after all, this launches a nice little "Update Wizard" that allows you to select the tables that you either want to add, update or delete.

Not so fast my friend! If you have an edmx file that has a modicum of complexity I have found this to be a painfully frustrating way to add a new data model to the file. I have spun many-a-cycle and invested many hours in trying to coerce the wizard to play nice, only to alas, nearly fall down, quivering in humble defeat.

But, my pride never allows me to succumb to such a challenge... defeat isn't an option!

What I have done in the past to add a new entity to an existing edmx file was to right click on the edmx file in Visual Studio Solution Explorer and select the "Open With" menu option. This opens the edmx file in an XML editor. I would then painstakingly  go through the file and add all of the appropriate entity definition declarative code that would not only define the conceptual .Net entity, but also define the data store and how it would be mapped over to my .Net entity.
This was a long, arduous process, and highly prone to error. I knew that there had to be a better way...

Trick/Lesson Learned:

Well, today I had an epiphany (or at least I like to think it was). Instead of fretting over the quirkiness of using the less that effective "Update Wizard", I decided to take a different approach...

I created a new edmx file (again, this posting does not describe the process of creating and edmx file) and added to this new edmx file only those entities that I eventually wanted to add to my existing edmx file. This allowed me to rely on the framework's creation wizard to create and edmx file. This wizard is rock solid, so why not allow it to generate all of the XML that I will eventually need to add to my existing edmx file; because after all, all an edmx file is, under the covers, is an XML file...

After creating the new file with the creation wizard, I then opened it up in the XML Editor. Then, after opening the newly created file in the XML Editor, I opened up my existing edmx file in a separate XML Editor (this is the file to which I wanted to add the new entities).

At this point, it was very straightforward: I simply added all of the appropriate XML markup from the newly created edmx file to the existing edmx file. It took only a matter of minutes.

Woohoo!!

Conclusion:

While there may be a more reliable way to add new entities to an existing edmx file that I'm not aware of, I have found that the current "Update Wizard" is less than reliable...and sometimes you need to roll up your sleeves and work with some good 'ol XML. And a surefire way to get it right first time, is to create a staging edmx file whose only purpose in life is to act as a surrogate for your existing edmx file. You can then use this staging file to add the appropriate XML to the existing edmx file, and continue on with your day.

Happy coding!

Using a Lambda Expression to Determine if a Number is Odd or Even

There's a simple way to use a lambda expression to determine if a number is either odd or even. Here's an example of how you can do this while iterating over a list of integers:

 

clip_image001

 

The key algorithm is n % 2 == 1. Simple is as simple does.

Great Visual of RIA Development

I recently saw this depiction of RIA development...

 

clip_image002

 

A picture is worth a thousand words!

Anonymous Methods as Parameters

If you are like me you hate writing the same mundane code over and over and over...puke!! You see this a lot when it comes to querying data from a database. There are a lot of nifty frameworks that consolidate this type of functionality and allows you, as a developer, to focus on the needs of the business.

If you are ever in a situation, however, wherein the powers that be do not condone the use of frameworks (as odd as that might be), here's a nifty little way to harness the power of Anonymous Methods as parameters in an attempt to isolate the mundane code in one single implementation (buckle up).

 

To retrieve a result set from a database, for example, all you need is a helper class that has one simple method with a signature like the following:

DbHelper.ExecuteReader(DataReaderHandler dataReaderHandler,
    IEnumerable<IDbDataParameter> parameters,
    string procedureName)

 

Notice the DataReaderHandler parameter. This is reference to a delegate that is declared inside the DbHelper class:

public delegate void DataReaderHandler(IDataReader reader);

 

If you wanted retrieve a list of user ids from a database, for example, the calling code that exercises the "ExecuteReader" method would look like this:

public List<int> GetUserIds()

{

   List<int> ids = new List<int>();

   List<IDbDataParameter> parameters = new List<IDbDataParameter>();

   // populate parameters (you can use a helper class to do this as well -- but I'm not going to cover that in this posting)

   DbHelper.ExecuteReader(
     delegate(IDataReader reader) { ids = PopulateUserIds(reader); },
     parameters,
     "SelectUserIds" ) ;

   return ids;

}

 

The magic happens when the executing code declares an anonymous method in-line:

delegate(IDataReader reader) { ids = PopulateUserIds(reader); }

 

When the code is executed the runtime passes the anonymous method as the first parameter. This delegate reference is used by the "DbHelper.ExecuteReader" method as follows:

public static void ExecuteReader(DataReaderHandler dataReaderHandler,
                                 IEnumerable<IDbDataParameter> parameters,
                                 string procedureName)
{

   using (IDbConnection connection = new SqlConnection(connectionString))
   {
     using (IDbCommand command = new SqlCommand(procedureName, connection))
     {
       command.CommandType = CommandType.StoredProcedure;

       foreach (IDbDataParameter parameter in parameters)
       {
         command.Parameters.Add(parameter);
       }

       connection.Open();

       using (IDataReader reader = command.ExecuteReader())
       {
         dataReaderHandler(reader);
       }
     }
   }

}

Things to note:

  1. The "connectionString" will ideally be read from a configuration file.
  2. This example uses both SqlCommand and SqlConnection objects. You can implement a different database provider depending upon your applications requirements.
  3. The ExecuteReader method has a "void" return type.
  4. The use of "using" statements ensures that connections are closed and that all objects are properly disposed of by the .Net garbage collector.

 

To cap off this little example, when the "dataReaderHandler(reader)" delegate is invoked inside the "ExecuteReader" method, the runtime routes the call to the "PopulateUserIds" method that was specified when the "DbHelper.ExectureReader" method was called. The "PopulateUserIds" method signature calls for one input parameter of type IDataReader. It uses this reader object to parse the result set and return the desired collection of user ids:

private static List<int> PopulateUserIds(IDataReader reader)
{
  List<int> ids = new List<int>();

  while (reader.Read())
  {
    ids.Add(int.Parse(reader["USER_ID"].ToString()));
  }

  return ids;
}

 

Now, as you can imagine, there is more database interaction then simply retrieving a result set. You can build out the DbHelper class to account for the return of scalar values, execute non-query procedures, inserting, updating, even interact with different database providers, etc...

The nice part about leveraging Anonymous Methods as parameters to framework level components is that it allows the framework to do all of the heavy lifting, while allowing you to have control over how the operation needs to be handled from a business perspective.

Applying this same technique to non-database centric activities helps you write less code and ultimately be more productive (IMHO).

We understood things 50 years ago...what about today?

This is a cartoon that was created 50 years ago about freedom...amazing the insight!

Impersonating IE from Firefox

Firefox allows you to impersonate IE. I’m currently running Firefox version 3.5.2.

To get "IE Tabs" you first need to open Firefox. Then, if you go to the “Tools >> Add-ons” section you can do a search for “IE Tab” by clicking the “Get Add-ons” menu option. When you include this add-on you will get a little icon in the lower right corner of the browser. By clicking the little icon you change the browsers engine from IE to Mozilla Firefox.

Firefox in “Mozilla” mode:

image

Firefox in “IE” mode:

image

The only visual difference is the little icon in the lower right corner of the browser and the subtle (and at times not-so-subtle) differences in CSS.

Microsoft SharedView

If you're looking for a poor-man's version of a desktop sharing application look no further than Microsoft SharedView. It is a really nice, lightweight desktop sharing application that not only allows you to easily create a sharing session and invite participants, but it also allows you to turn over control to another user (but be careful with this, of course ).

You can download Microsoft SharedView here:

http://www.connect.microsoft.com/site/sitehome.aspx?SiteID=94

The Story of the Old Assembly...

Once upon at time there was an assembly. It was a good assembly. It did it's job very, very well. It was an assembly that was very important to its fellow assemblies. It not only contained business related logic, but it also served as an implementation boundary to other satellite assemblies.

Well, one day, as the application was being tested by the thorough QA technician, things weren't behaving as they should. The ever important assembly seemed to be having "issues". The QA technician was testing specific scenarios that surely the ever important assembly could perform; but alas, the technician had to enter a "failure" report on the test cases that he was testing. The ever important assembly was devastated. It didn't know what went wrong. Surely the master developers who created him had done their job well. Surely the QA technician was testing as good as he had before... What was the problem??

Well, as time went on the assembly had to rely on the helpful hand of the development team to find out what it's problem was. Turns out, that the problem was simple once the symptoms were clearly understood.

The symptoms were somewhat puzzling, however. The nightly build process was working fine. The code compiled on every machine that it ran on. But something very interesting occurred: the timestamp of the assembly was almost 2 weeks old on the QA box. Hmmm...what could this mean? "Perhaps something was wrong with the build server", the assembly thought. Well, after hours of looking into the issue, and pouring through the build process, it became apparent what the problem was....

A little over a week prior, in the dark of night, a little gremlin snuck into the application solution and decided to mess with this ever important assembly. It opened up Visual Studio 2008, opened up the parent project, expanded the "References" folder, and right clicked on the assembly while it slept, and clicked "properties". While inside the "Properties" view the little nefarious gremlin changed the "Copy Local" value to "False".

Nearly two weeks passed before it became apparent that something was awry with the ever important assembly. Well, the developers came to save the day and changed the "Copy Local" value back to "True". The build ran successfully, and the old assembly was old no more...

Cheers filled the office air as the QA technician retested his scenarios! Pass, Pass Pass...was the chant!! All was well!

 

OK, the moral of the story, if you try to publish a project, and an assembly is not being updated (and it's satellite assemblies don't get published either), the FIRST thing that look at is the "Copy Local" property of  the assembly reference from within the parent application.

My How History Tends to Repeat

In 1961 the late great Ronald Reagan spoke out against socialized medicine. He was almost prophetic in his interpretation of government intentions. The arguments that he put forth so many years ago are before us again today. How can we not see that what he spoke back then is relevant today? And how is it that some in our society can look at our Founding Fathers as "antiquated" and "out of touch" with what our society needs today?

It will be hard to argue that times haven't changed since 1961. Technology, for example, has completely changed how the world does business. We are now more connected than ever before. Yet, the intentions of power hungry leaders and the corruption that enters the hearts of ill-willed men hasn't changed from the beginning of time. And if Ronald Reagan had it right so many decades ago, surely our Founding Fathers had it right when they penned our inspired Constitution.

Let us not look beyond the mark that they, our Founding Fathers, blazed for our generation, and let us look to the future with a learning eye always gazing on the past. It's amazing to me how middle-aged men and women, who have never lived under a tyrannical government like that of our fore-fathers, seem to "know better". The shear experiences that our predecessors lived through should not only garner our admiration, but also our learning eyes and ears; and I even dare say that we should pledge our lives, liberty, and our sacred honor to the principles that they forged in the Constitution by virtue of living through experiences that no one born and raised in the United States of America today has gone through. How arrogant a people we have become. To dismiss the lessons learned by our Founding Fathers in the refiners fire is a grave mistake indeed!

Please listen to what Mr. Reagan said so long ago, and search your heart as to whether or not there isn't a message for us here today in 2009.