Object Reflection In JavaScript

Reflection is a powerful technique that I’ve used a lot in C#. On several occasions I’ve found some uses for this technique in JavaScript too. Most recently, I was writing a test harness for a RESTFul web service to allow our QA folks a easy way to test it. Typically, when I’m writing these kinds of things I have a couple objectives in mind.

First, I don’t want to turn the testing tool into a maintenance item. That is, if there are updates to the software being tested (a web service in this case) I don’t want to have to update the tool.

Second, I don’t want to add another uncontrolled variable to the testing.   Meaning, I don’t want the testing tool to require extensive QA or to create a layer of possible bugs to be validated anytime QA finds an issue with the software being tested.

Finally, I don’t want to spend a lot of time on it and I’d like to reuse it. It’s an internal tool that is usually being used by a semi-technical person and has a very specific purpose, being pretty isn’t one of them. And, if I can use it again, for another web service in this case, double bonus!

In the case of the RESTFul Web Service testing tool I accomplished this in two ways.

First, I build the inputs for the service by parsing the web service schema (.xsd) dynamical. This way, if new inputs are added I don’t have to update the testing tool. I probably write a post on that part another day.

Second, I output the results (which are returned in JSON) to a web page using reflection after the JSON object is parsed to a JavaScript object.

Here is an example of how I do it:

Consider the following object. It has a set of properties, a nested object, and a function.

var obj = new Object;
obj.FirstName = “John”;
obj.LastName = “Smith”;
obj.FullName = “John Smith”;
obj.Address = “Main Street”;
obj.Phone = “999-999-9999″
obj.GetName = (function () {});
obj.History = new Object();
obj.History.PreviousAddress = “South Street”;
obj.History.PreviousPhone = “888-888-8888″;

All I want to do is print each property and it’s value to the screen. And, if there is a nested object I want to print it’s properties too. This will allow the return results to be validated.

To do this I use a script like this:

function DisplayObjectProperties(obj) {
for (prop in obj) {
var text = “”
if(typeof obj[prop] != “function” && typeof obj[prop] != “object”){
text = prop + “: ” + obj[prop];
}
else if (typeof obj[prop] === “object”) {
DisplayObjectProperties(obj[prop])
}
$(“body”).append(“<div>”+ text + “</div>”);
}
}

Nothing too much going on here, just a loop through the object and appending each property name and it’s value to an html element.  The important thing is to test the type, so you can handle nested objects, arrays, functions, etc however you might want to.  In my case, if there is a nested object I want to display it’s properties too.  I do that with a recursive call to the function passing the nested object.

Call it:  Just pass the object to the function it will do the rest.

DisplayObjectProperties(obj);

Results:

FirstName: John
LastName: Smith
FullName: John Smith
Address: Main Street
Phone: 999-999-9999
PreviousAddress: South Street
PreviousPhone: 888-888-8888

Returning Errors in a WCF RESTful Web Service

While writing web services I’ve used several different methods for returning errors. A very common method is to return null or if the response is a string return the word error or a message stating there was an error. That works, but I’ve never really been a big fan.

Example:

public object GetSomething(string param)
{
try
{
var returnObject = new object() = some code here
return returnObject;
}
catch (Exception ex)
{
return null;
  or
return new object();
 or
 some variation;
}

}

This method doesn’t really seem to comply with best practices for being RESTful or for OOP. It gets you by, but it’s kind of smelly.

More recently, especially with RESTful WCF Web Services that are returning complex objects, I’ve added DataMembers to the DataContract that include an error Boolean and error message along with the object(s) I’m returning as a DataMember.

Example:

[DataContract(Name="returnObject")]
public class returnSomeObjects
{
[DataMember]
public IList <someObject> { get; set; }
[DataMember]
public bool Error { get; set; }
[DataMember]
public string Message { get; set; }
}

Then in the error handling above:

catch (Exception ex)
{
return new returnObjects() {Error = true, Message = “ex.Message”};
}

I like this method more because it always returns the same object and the calling process can make a decision based on error being true or false. Even if you don’t have complete control of the returning object(s); for instance when wrapping a legacy class library with a service, you can just make the object a DataMember on the DataContract and away you go.

The problem with this method is the http response is a 200 ok, even though an error occurred. So, it’s not as smelly, but still not the best practice. Recently I’ve begun to favor a more organic approach. Something that is more RESTful in nature, that is modifying the http status code to reflect that an error has occurred.

Example:
catch (Exception ex)
{
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
response.StatusDescription = “A unexpected error occurred!”;
}

You could evaluate the exception and return codes based on certain situations, I use the above for unexpected errors. You could also make the response.StatusDescription the exception message. The calling process can now key off of the http status code and handle the situation as required by it’s own system and rules.

You might ask, why modify the status code? If you just simply throw the error you achieve something very similar, that is a HTTP status code other than 200 ok. The issue, for me at least, is you get a 400 bad request. Which is a little misleading to the caller and not exactly true if somewhere in my data or logic layer an error has occurred.

So I like to add a little control. You can evaluate for invalid parameters or data and still return the 400 bad request when appropriate, but for unexpected errors, I like returning the 500 Internal Server Error.

I should mention I’m using this for internal web services so I do have more latitude in what I show to the calling processes and systems, however, I see no reason why using standard http response codes would be a problem in a public API.

Additionally, this method adds more consistency to what a calling process will get when errors occur. For example, with the previous methods if your service is unreachable they would get a different response then if it is reached and a custom error is returned. In other words, they can always evaluate the response codes that are native to http giving you a more RESTful response.


Floating Sidebar With CSS

Floating sidebars are a great method for keeping menus, share buttons, and other information in front of users as they scroll down the page. There are a number of jQuery plugins that work great for this, but in some cases jQuery might be overkill. For example, if you need to keep informational or help text that is static in front of the user.

A simple way to create a floating sidebar is to use CSS. Take a look at the side bar on the left and scroll down the page this jsfiddle test. Notice, it stays in view the entire time. To achieve this all we have to do is create a div with the following class.

.sideBar
{
position: fixed;
z-index: 1000;
}

The magic is “position: fixed”, it’s what makes the sidebar stay where you want it to. Depending on what else you have on the page, and perhaps if you want it to float outside the container the div is in, I like to use z-index to make sure it overlaps how I want it to.

Now that you have it floating you need to position the div and style it by adding another class like below. In this case the div will float on the left and start below the header of the page.

.sideBar.boxLeft
{
width: 300px;
float:left;
padding: 0px 0px 0px 0px;
left:75px;
background-color:#eee;
top:30%;
}

There you have it, in just a few minutes you have a quick and easy solution for creating a floating sidebar.


Add CSS Class Recursively With jQuery

The Function:

function addCSSClassRecursively(topElement, CssClass) {
    $(topElement).addClass(CssClass);
    $(topElement).children().each(
            function() {
                 $(this).addClass(CssClass);
                 addCSSClassRecursively($(this), CssClass);
            });
}

Pretty simple. The first line adds the class to the element passed in. Next, using the jQuery.children() and jQuery.each() functions it iterates through each child element adding the CSS Class and then calls itself to add the CSS class to each of the child’s children elements.

Calling it:

$(function() {
   addCSSClassRecursively($('#div1'), 'MasterCssClass');
});

In my implementation I call the function when the DOM is fully loaded, using the jQuery.ready() function. I just pass in the parent or top level element I want to start adding the class at and the functions does the rest.


Turn On The Music And Run

Over my running years I’ve gone back and forth between no music while I run and having music.  Usually, when I run inside on the treadmill I listen to music to pass the time and keep it interesting.   However, when I run outside, most often, I leave music at home.   Mostly, I do this because I find music a distraction.  Technical issues and mechanical failures aside, I find it also distracts me from my run.  I find that without music I “stay in my run” and get more out of it.  I’m able to concentrate on my form and pace better.

Having said that, sometimes music might be just what you need. I’ve been struggling with running a lot lately.  I’ve struggled to find time. I’ve struggled to set goals.  And I’ve struggled to improve.  Today,  I decided to change it up and take some music with me on my run.  And to my surprise it really helped.  It took my mind of some aches and pains when I got started and to my surprise didn’t seem to distract me as much as it has in the past.  Perhaps I’ve grown as a runner more than  I thought?

The moral of the story is, you’re a runner so get out there and run!  And, if you need to, turn on the music.


Invoke base class methods on unknown types

I recently built a system that dynamically builds content for sending messages to users after certain events.  As is with most of these types of systems the content is mostly static and stored in a repository with “holder values” that are replaced at run time from a data source.  The architecture of this system is one that is very configurable.  Meaning, a new message can be created using configuration files no additional coding necessary.  One of the challenges this presented was how to handle formatting of data types that are not known until run time.

Use Case:

The content may look something like: “Your bill with a billing date of _date for _amount is overdue.”  The holder values are _date and _amount; the data source might store the amount as a number and the date including the time (100 & 12/31/2012 08:35:55).  However, this data would need to be formatted as $100.00 and 12/31/2012 for a user facing message.

The system needed to handle any possible formatting of data without knowing at build time what they might be.  To accomplish that I store a pointer to a data source for each “holder value”, which includes a mapping to a base class method (and the required parameters) of the underlying data type.  Then, when replacing holder values with the  real values from the data source I apply the method using reflection.

Here’s some sample code and a quick explanation:

public static string InvokeSomeMethod(string method, string parameters, string value)

        {
            Object v = ConvertValue(value);
            var pList = ParseMethodParameters(parameters);
            var rt = v.GetType().GetMethod(method, GetParameterTypes(pList)).ReturnType.FullName;
            return InvokeMethod(v, rt, method, pList);
        }
}

“Let me explain. No, there is too much. Let me sum up”:

We can’t just invoke a method all willy nilly like, first we need to know a few more things.

  • What type are we dealing with?
  • What is the return type of the method? (Beware of overloads)

A little more detail:

Type:

The values I’m dealing with are strings, however, I may need to execute a method on it’s “true” type.  In order to get the type I call ConvertValue (line 1 above) which passes the value through a series of tests to determine what type it is and convert it.  I won’t bore you with the entire method, but this should give you an idea what I mean.

 public static object ConvertValue(string value)
        {
            if (ParameterIsInt(value))
            {
                return Convert.ToInt32(value);
            }
            if (ParameterIsDouble(value))
            {
                return Convert.ToDouble(value);
            }
              if (ParameterIsDate(value))
            {
                 return Convert.ToDateTime(value);
            }
        }
}

Each sub-function does something like this:

 public static bool ParameterIsDate( string value)
        {
            DateTime d;
             bool result = DateTime.TryParse(value,  out d);
             if (result)
            {
                 return true;
            }
             else
            {
                 return false;
            }
        }

Return Type and the Parameter List:

Remember above where I said beware of overloads when getting the return type.  The parameter list has to be parsed and passed when getting the method return type in order to get the correct method. When I parse the list I reuse the convert value function to get the correct type of the parameter, this is key to the getting the method.  And of course the parsed parameters are passed when invoking the method.

Conclusion:

So far this method (as part of a larger highly flexible system) has worked very well and allowed us to create a lot of new content with little to no new lines of code.  One down side, the person building the configurations has to  know the base class methods and parameters (absent a user interface for the content management) to format the data.  Another issue was the convert value function.  When I originally built the system I just added the half dozen or so common data types.  However, if a new one comes up some coding will have to be added.  I do have a possible solution to that issue.  In some other similar systems I’ve used Convert.Change Type to change strings to the “real type”.  This works well as long as you know what type you want.  If not, your still forced to call the various try parse methods to get the type.  In a system like the one were using, we could leverage the configuration to pass a type to the Convert.ChangeType method and get rid of the try parse convert to methods.  I’ll probably add that the first time I have a new data type come up, so far it hasn’t.


Extension Methods and Generics: Match a String To a Enum

To match a string value to a enum you have a couple options.

  1. You could loop through the enum values until you find a match.
  2. Or a better option, use the Enum.Parse method to convert the string to the equivalent enumerated object.

However, when exchanging data with third parties you might find the need to convert between strings and enums often.

With a little help from generics I wrote a quick extension method to make this even easier.

Example:

public static T FromStringToEnum<T>(this String stringValue, T enumToGet)
{
    if (!typeof(T).IsEnum) return default(T);
    return (T)Enum.Parse(typeof(T), stringValue); 
}

public enum Number { one, two }

[TestMethod]
 public void FromStringtoEnum_EnumReturned()
{
    string f = "one";
    var e = f.FromStringToEnum( new Number());
    Assert.AreEqual(Number.one, e);
}

[TestMethod]
 public void FromStringtoEnum_NonEnumPassed_DefaultReturned()
{
    string f = "one";
    var e = f.FromStringToEnum("");
    Assert.AreEqual( null, e);
}

The first line validates that T  is an Enum, if not it will return the default value of the type.   The second line is the implementation of the parse method using generics to get the type of the enum.


			

Mask a String Using Linq Aggregate Method

There are a lot of different ways to mask sensitive data like passwords and account numbers when displaying to users.  Most of them seem to include some sort of loop and/or a regular expression and get the job done. Recently, I wrote a simple function using Linq that I thought was worth sharing.

Here it is:

public static string MaskString(this string stringToMask, string mask, 
 int show)
{
     return stringToMask.ToCharArray().Aggregate("", (maskedString, nextValueToMask) 
          => maskedString + (maskedString.Length < stringToMask.Length - show 
          ? mask : nextValueToMask.ToString()));
}

Explanation:
The function takes in the string being masked, the masking character, and the number of characters to show. It assumes the masking starts on the left and it is written as an extension method.

It’s pretty simple really. The string is converted to an array so the Linq Aggregate function can be used to string the array back together replacing each character along the way with the mask until the show point in the string is reached.

More Info:
The Aggregate function was brought to my attention in a blog post from  By A Tool.  I’ve used variations of this for combining lists of strings and removing duplicates from delimited strings for a system I’ve been working on.  Just for fun I included those below to demonstrate other applications of the aggregate function.

More Aggregate Examples:

public static string CombineListToDelimitedString(this List<String>listOfStrings, 
     string delimiter)
{
 return listOfStrings.Distinct().Aggregate("", (inner, outer) => inner +
         (!inner.Contains(outer.ToString()) ? outer.ToString() + delimiter : "")).TrimEnd( 
 new char[] { Convert.ToChar(delimiter) });
}

public static string RemoveDuplicatesFromDelimitedString(this string delimitedString,
 string delimiter)
{
 return delimitedString.Split(new char[] { Convert.ToChar(delimiter) }).Distinct().Aggregate("", (inner, outer) => inner
         + (!inner.Contains(outer.ToString()) ? outer.ToString() + delimiter : "")     ).TrimEnd(
         new char[] { Convert.ToChar(delimiter) });
}

Conclusion:
Linq never seems to disappoint when looking for a cleaner and cooler looking way of doing things. It just goes to show, there are more than two ways to skin a cat. Or in this case, “mask” a cat.

Yep, that just happened…


Host RESTful WCF(3.5) Service – IIS 6.0 – HTTP 404 – File not found

I ran into this issue today, the fix is  simple and very easy to overlook.  I thought I’d share it.

HTTP Error 404 – File or directory not found.

Internet Information Services (IIS)

I was setting up a WCF REST service on a production server .  The service had been successfully running on two other servers in development and staging.  It runs in the Net 3.5 framework and is hosted in II6 on windows server 2003.

This server has a number of Net applications running , however, it didn’t appear that any websites or web services had been hosted in IIS. So I assumed it was just a setup issue, but setting the website to allow “Directory browsing” served up the directory listing. I began to Google and found a number of posts and articles on the subject.

Other developers reported solving similar issues by running ASPNET_REGIIS, ServiceModelReg.exe, setting up .svc MIME type, and creating a wild card mapping to aspnet_isapi.dll. I discounted the MIME type and wild card mapping since they were not required on our other servers. However, since we’d never hosted a website on this machine let alone a WCF service I decided to run the commands to register ASP.Net applications with IIS and the service modle registration tool. Neither of these solved the issue, so I played with directory security to make sure it matched our other servers, still no luck.

Finally, after a little more research I found that by default in II6 the ASP.net web service extension is set to a status of prohibited for security purposes.  The fix was as simple as opening Web Service Extensions in the IIS manager and setting the ASP.net extension to allowed.

The point is,I focused too much on the specifics of my problem and looked past the basic issue.  Which was that IIS had not been fully setup to host ASP.net applications.  I was “in the weeds” as they say.

Links:
http://msdn.microsoft.com/en-us/library/ms752252.aspx
http://msdn.microsoft.com/en-us/library/k6h9cz8h(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/ms732012(v=vs.90).aspx
http://www.jamesgoodfellow.com/blog/post/IIS-60—HTTP-404—File-not-found-Internet-Information-Services.aspx
http://support.microsoft.com/kb/248033
http://stackoverflow.com/questions/2009233/how-to-deploy-wcf-service-on-iis-6-0


Someone Is Always Faster – An Older Wiser Runner Knows This

It’s not like I’ve been running around with the illusion that I’m a fast runner, or faster than most, or even faster than many… Maybe faster than some. However, at my best I’m a middle of the pack runner.

During my run today I was reminded of this fact. It was about four  in the afternoon, sunny and in the low sixties. Perfect day for a run.  I was about two or so miles into it, feeling pretty good, I had just started to settle into my run.  Just then, some young guy blew past me, probably running a good minute faster.  To be fair, my day started off at 6am when my youngest got up.  And consisted of laundry, lots of playing with the kids, and cutting the grass. I’m not saying I would of kept up with this guy if I was just starting off my day.  I’m just saying my day had been pretty full and his had mostly likely just started.  Or, I’m just trying to save my ego…

Nevertheless, I stayed on pace and kept myself in my run.  However, it wasn’t so long ago that I would have tried to keep up, or at least would speed up so it wouldn’t look so easy to blow past me.  Just another futile effort to save my ego.   But, if I had, it would have been  to the detriment of my run.

You see,  I was on a six mile run. Which is not a terribly long distance, but for me lately that is a long run.  I’m trying to get my training back on track, it’s been off for probably more than a year now.  The point is, no matter if you’re on a training run or in a race you need to run your run and run your race. If you let somebody else dictate your run and get you off your game, you may not get from your run what you set out to….

Can’t talk about being wise without Yoda!


Follow

Get every new post delivered to your Inbox.

Join 439 other followers

%d bloggers like this: