Posts

Showing posts from 2013

Pictures of Autumn

Image
Some pictures from one of my lunchtime walks this week. This is my favorite time of year.

Mocking an IQueryable Return Value‏

When following the Repository pattern it’s often a good idea to return an IQueryable representing the data you wish to expose. But how can you mock this data when writing your unit tests? The AsQueryable() method provides a way.  In the example below, I create a short list of mock data to be returned from my mock repository. I then setup my mock repository to return that list as an IQueryable using the AsQueryable () method. List  mockContacts =  new  List (6); mockContacts.Add( new  Contact { Id = 1, Email =  "bonscott@acdc.com"  }); mockContacts.Add( new  Contact { Id = 2, Email =  "brianjohnson@acdc.com"  }); mockContacts.Add( new  Contact { Id = 3, Email =  "angusyoung@acdc.com"  }); mockContacts.Add( new  Contact { Id = 4, Email =  "malcolmyoung@acdc.com"  }); mockContacts.Add( new  Contact { Id = 5, Email =  "cliffwilliams@acdc.com"  }); mockContacts.Add( new  Contact { Id = 6, Email =  "philrudd@acdc.com"  });   _

A Funny Thing Happened When I Left My Whiteboard Unattended

Image
I just got a new whiteboard, which is currently standing propped-up against a wall, still in the plastic packaging material, awaiting installation (Installation? Does one "install" a whiteboard? Anyway...). A friend of mine decided to take the opportunity to scrawl a message on it while it's waiting to get hung on the wall.

Retro Halloween Goodness

Image
Among the things I enjoy in life are retro/nostalgia, autumn, and Halloween, so when I saw the following at Target today I couldn't help but smile.

Missing or Malformed Service Bindings Can Hose ALL Your Service Calls

I learned something interesting yesterday. After we did a new production deployment at work for one of our websites, we ran into a few issues that hadn't occurred in QA or staging. The first was that, for some reason, calls from the website (which is an ASP.NET MVC 4.5 site) to one of our WCF services, was failing. The service in question is used to retrieve images from Sharepoint. Later, we found another issue, wherein attempts to contact a different service for the purposes of sending email were failing. As it turned out, the two issues, while seemingly unrelated, were caused by the exact same problem. In addition to the endpoints for the two aforementioned services, our Web.config file also contained an endpoint for a third service, that is not yet in use, but is defined and in development. But somehow, the binding for this service didn't make it into the production version of the Web.config file, and therefore when the app tried to make use of any service, an exception wa

The "TypeError: n.curCSS is not a function‏" Error

Yesterday at work we deployed a new version of one of our websites to production after a successful test cycle in our QA and staging environments. However, once the site was deployed to production, a script error, "TypeError: n.curCSS is not a function‏", would appear whenever an attempt was made to display a jQuery UI dialog. This left me scratching my head. It had worked fine in QA and staging, and the code in question had been around for months. It definitely had something to do with a difference in the production environment, but what? The error in question was occurring in a JavaScript bundle being created by the website (which is an ASP.NET MVC 4.5 site) on start-up. I could view the source and see the offending line. After Googling the error message, I learned that it was related to an incompatibility between versions of jQuery and jQuery UI -- but again, this had worked perfectly in the other environments, and for quite some time. How could it be an incompatibilit

Using LINQ to Group By Multiple Values

I recently wrote some code using LINQ in which I had to perform a grouping by multiple values. It had been a little while since I'd made use of LINQ's GroupBy method and couldn't remember how to group by multiple values. The answer is to use anonymous types. Here's an example of how to group a list of Car objects by their Make and Model properties: var  carGroups = cars.GroupBy(x => new  { Make = x.Make, Model = x.Model });  And here's an example of how to group the same list by the same properties, but only when the group contains 5 or more objects: var  carGroups = cars.GroupBy(x => new  { Make = x.Make, Model = x.Model })                 .Where(grp => grp.Count() >= 5); The properties of your anonymous object can be accessed via the group's Key property. Here's an example, using the groups we created in the previous example: foreach  ( var  group  in  carGroups) {      Console .WriteLine(          String .Format( "Make {0}, Mo

JSON Object Property Hierarchy Bug in Handlebars

I recently ran into a bug with the Handlebars client-side templating system. This issue was that Handlebars wasn't picking up a property on the JSON object I passed into it -- or, more specifically, it wasn't picking up the value while within an "unless" conditional block. It was picking up the value outside of the block. The value in question was nested within another object of the object I passed in. After some digging, it turns out that, when within an "unless block", you need to add another ../ to the hierarchy. To illustrate the fix, here is an example of some code that won't work. In the following example, we are 2 levels deep within the object's properties, and want to use the Id property that resides at the root level. {{#unless IsDefault}} {{../../Id}} {{/unless}} To get the code to work, add an additional "../" as shown below. While this doesn't accurately represent the object's structure, it does fix th

How to Enable jQuery Client-Side Validation in Dynamically Rendered Partial Views in ASP.NET MVC

Recently I was doing some work in an ASP.NET MVC 4 website and was rendering a partial view to a div element via a jQuery AJAX call. I found that when the partial was included in a view from a controller's action method, all the client-side validation worked correctly, but if I rendered the partial view client-side code that was, for example, just getting the partial view by itself and putting into a div element as I was trying to do, the client-side validation wouldn't work. After doing a little research, I learned that the client-side validation needed to be reset after the partial was rendered via the client-script. To that end, I wrote the following JavaScript function to allow quick and easy resetting of client-side validation in cases such as these. Just pass the CSS selector to the partial's parent form to the function shown below. This, of course, assumes that you also have references to the jQuery unobtrusive validation library. function  ResetValidation(formSe

An ASP.NET MVC ActionButton HtmlHelper Extension Method

Here's a quick ActionButton HtmlHelper extension method I wrote. Hopefully this will come in handy to someone. For more information on extension methods, check out Microsoft's MSDN document here , or check out one of my earlier posts here. public   static   MvcHtmlString  ActionButton(      this   HtmlHelper  htmlHelper,  string  value,  object  htmlAttributes,  string  actionName,       string  controllerName,  object  routeValues) {      if  ( String .IsNullOrWhiteSpace(actionName))          throw   new   ArgumentException ( "actionName is required." );      if  ( String .IsNullOrWhiteSpace(controllerName))          throw   new   ArgumentException ( "controllerName is required." );      var  button =  new   TagBuilder ( "input" );     button.MergeAttribute( "type" ,  "button" );     button.MergeAttribute( "value" , value);     button.MergeAttributes( HtmlHelper .AnonymousObjectToHtmlAttributes(htmlAttributes))

Odd Doctor Who VHS Tape Cover

Image
In my early teen years, I used to watch Dr Who on our local PBS affiliate on Saturday afternoons. I haven't seen it in years, but for some reason I was reminded of it one day this week. Later that same day, a co-worker mentioned it to me, and then a day or two later someone else brought it up. I thought it was kind of funny that this TV show I hadn't watched since I was a teenager was now frequently popping up. This, in turn, reminded me of a particularly odd VHS tape cover I'd seen when I was still a teenager. The third actor to play Dr. Who was a gentleman named Jon Pertwee. It seems like everyone's favorite Dr. Who is Tom Baker, who was the fourth actor to play the part, but I never saw much of Tom Baker's work, and my personal favorite is Jon Pertwee. And at some point, a VHS release of some of his best work was released, but it had an extremely odd photo of Mr. Pertwee on the cover:   I'm not sure what they were thinking when they chose this phot

A Generic Method for Consuming Data From RESTful WCF Service Methods

I recently added some new methods to an existing RESTful WCF service and needed to consume these methods from an application which had not previously made use of this service. To make this easier, I created the following generic method. It could probably use a little refinement (specifically some code to handle what happens if an attempt is made to cast the service method result to an incorrect type), but it does the trick. using  System.IO; using  System.Net; using  System.Runtime.Serialization; using  System.Text; namespace  RESTService.Helpers {      public   static   class   RESTServiceHelpers     {          public   static  T GetResultFromRESTServiceMethod<T>( string  url)         {             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);             request.KeepAlive =  true ;             request.Method =  "GET" ;              using  (HttpWebResponse response = (HttpWebResponse)request.GetResponse())             {                  En