Tuesday, December 2, 2014

Angular ng-include

If you need to bring one HTML file into another using Angular, you may want to take advantage of the ng-include directive (documentation can be found here).  Using ng-include will bring in the specified HTML file.

<div ng-include="'/Pages/SomeApp/Ordering.html'">

Be careful, though!  I ran into a problem with this directive that was driving me crazy.  I had used ng-include in a file that also had an ng-include in it and it caused me no small headache.  Others have done the nested ng-include so it may have just been my specific usage that was causing problems, but it's worth noting.

In case you're seeing something similar, I was forced to use IE 8 (you can peruse my hatred of that browser in other posts) and whenever the ng-hide class was applied, the entire screen went blank.  The DOM was technically still there, but you couldn't see anything.  Good times.  Or not.

Plain HTML as a Partial View

Sometimes you just need to include a plain HTML file in another MVC view.  It's easy enough.  Just use the code below and you should be all set.

@Html.Raw(File.ReadAllText(Server.MapPath("~/Pages/SomeApp/Ordering.html")))

Friday, November 7, 2014

Scope Objects in the Console

When you need to quickly print a scope value in the console (I use Chrome), you can use this helpful little guy:

angular.element("#elementId").scope()

Just a little bit of code that can help immensely.

Friday, October 10, 2014

Stayin' Alive

It's been a while since I posted.  I'm not sorry.  Life is busy.  You'll just have to learn to deal with missing me.  That's how our love works.

I recently had to figure out how to keep my .NET application "alive" or "awake" between requests.  This particular site belongs to a small business that is just getting started.  Since they're not sure how big their site might get we decided to stay small and use shared hosting from GoDaddy.  One of the problems they were encountering was that the site was always so "slow".  I couldn't figure out what they were talking about for quite some time until I finally hit the page early one morning, before anyone else had hit it for the day.  It was slow.  Obnoxiously slow.

IIS was recycling the app pools when no one visited the page for a period of time.  What I needed was a solution to make that stop happening.  I found a clever little solution that works for us, but has some obvious pitfalls.  H/t to Omar Al Zabir at Codeproject for this one.  If you actually clicked that link and read the original solution you may have noticed that it's quite old.  But who cares?  It works.

Basically, you create a cache item with a callback.  When the cache item expires and the callback fires, the callback hits your site.  I modified the solution a little bit to do what I needed.  I have a separate service from my website so I wanted to keep both of them alive.  Easy.  Check it out.

This is from the global.asax.cs from the web project:


That's pretty much the whole shebang right there.  Actually, that's not "pretty much" it.  That's it.  There are obviously some web.config values to add, but I think you can figure that out.

Tuesday, September 23, 2014

Angular JS ajaxComplete

I recently came across a problem where our ajaxComplete function wasn't being executed from jQuery after we started using Angular.  As it turns out (it was a bit of a "duh" moment) the issue was that when using Angular we were no longer using the jQuery ajax calls.  So the jQuery ajaxComplete function was never fired because no jQuery ajax calls were ever made.

That set me on a path of trying to figure out how to mimic the ajaxComplete behavior with Angular and this is what I came up with.  I haven't fully tested it, but wanted to put it up here before I forget so I can have a better starting point next time this comes up.

After the application has been created, configure the $httpProvider to have an intercept:

angularApp.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push(function ($q) {
        return {
            response: function (response) {
                return response;
            }
        };
    });
}
]);

This particular implementation won't do anything but act as a pass through, but it can be modified however necessary to meet your needs.

Monday, August 11, 2014

Getting Distinct from a List

Sometimes you have a list of complex objects that you need to pare down to be a list of distinct complex objects.  LINQ offers a way to do that, but unless you've done it before it's not immediately obvious as to what you need to do.  There are two ways to do it, and they're actually pretty easy to implement.

If the complex object is your own creation, just implement IEquatable where T is the type of the object.  Something like this:



Once you've implemented IEquatable on your class, you just have to invoke the .Distinct() call on your list.  Here you go:



Here are the results of the IEquatable approach:


If you find yourself in a situation where the complex object can't be extended to implement IEquatable, there's a solution for that, too.  You'll create an additional class that implements IEqualityComparer and then specify that when you invoke the .Distinct() call on your list.







Here are the results of using the IEqualityComparer approach:

Monday, August 4, 2014

Going Loopy for Performance (C#)

I may have written about this before, and if so, I apologize.  Wait, no I don't.  I couldn't find it and this blog is for me anyway.  I retract my apology.  Anyway, if you've ever wondered whether your loop is as fast as it can be, I have some answers for you.

I read one time that a for loop is faster than a foreach loop in C#.  I can buy that, I suppose.  But in the same article it said that iterating backward through a for loop is faster than iterating forward and I had a hard time getting on board with that.  So I wrote some tests.  That author was right.

I wanted to test a variety of options, so I did.  That's how I roll.  I ran each test over 100,000,000 iterations.

The first test was a regular for loop:

...and the results:


Next up I did the same thing but without previously specifying the size of the loop (I use .Count as the upper limit of the loop):

...and the results:


You can see that of the two options, it is slightly faster to set the upper limit in a variable.  "Slightly" here meaning 11 milliseconds over 100,000,000 iterations, or .00000011 milliseconds per iteration.

Moving on.  Next we have the reason for my testing in the first place: the reverse for loop:

...and the results:


Whoa!  That's a bit of an improvement.  37 milliseconds over 100,000,000 iterations (compared to our previous best time), or .00000037 milliseconds per iteration.

This time the reverse loop without a variable:

...and the results:


Based on the earlier results of the forward for loop, this makes sense.  Next was a foreach loop over a list:

...and the results:


Wow.  That's a significant degradation.  Our previous worst time (a forward for loop with a variable as our upper bound) was 240 milliseconds.  The foreach on a list (which is, in my opinion, the easiest loop to write) takes nearly twice as long to run.

Last up is a foreach loop on an array instead of a list:

...and the results:


The overall results certainly point to a reverse for loop with a variable as the lower bound is the fastest we can iterate through an array.

BUT WAIT, THERE'S MORE!  I found this last bit while I was writing this up.  It's an alternative way to do a decrementing for loop that's actually slightly faster than anything else up there.

...and the results:


I learned a couple of lessons on this one.  First, don't always go with the easiest thing to do because it may not be the best thing to do.  Second, minor functions can have major performance implications.  In this example, I just incremented a variable during the iteration, but in a real world scenario I may do anything from build a complex object to make a service call.  This basic example showed us a difference of 279 milliseconds over 100,000,000 iterations.  That is a really small number per iteration, but keep in mind that the only thing we were doing was incrementing a counter.  If we had been doing any real work that would have been a major performance hit.