Quick Resource Helper Class

The production app I’m currently working on involves reading a client-provided extract (on the magnitude of hundreds of thousands of lines), transforming that data, and importing it into our system for processing. This happens once a day. The current process involves importing this information into staging tables on the DB side, then running stored procedures to transform the data.

As part of an effort to both improve efficiency as well as add in scalability, the DB team has decided this process should import into session-level temporary tables. This will allow multiple imports to go on at once if need be, as well as give some performance enhancements. The software side of this requires that the importing software opens a connection, and then creates the temporary tables on that connection. From there, the software reads the client-provided extract in about one thousand lines batches, and uses SqlBulkCopy to transfer it to SQL server.

Most of the architecture in this problem was laid out already, and not having any previous experience dealing with data sets this large, I followed the rest of the team’s leads. The temporary table creation needed to be done on the software side, and the scripts themselves needed to be part of the project. Not wanting to rely on folder paths to read in the data, and really not wanting to have string variables that held the scripts, I opted to leave the scripts intact, but use them as embedded resources. I haven’t worked with embedded resources before, honestly, so I did find out a couple of things.

The main call you’ll need is:

Assembly.GetAssembly(yourAssembly).GetManifestResourceStream(fullResourceName)

The tricky thing can be the file path. It uses periods to denote folder paths to load your project. Not difficult, but not the most obvious thing either. I wanted to “hide” that from my consuming apps, as they really don’t care about how the resource accessor likes to make it’s names. They just want their file. They do know about the project structure of the assembly that has their resource, however.

The next thing that I accidentally did wrong was the assembly – you need to provide the assembly that the resource lives in, for obvious reasons. Given that this helper class lives in a “Utilities” type of project, it’s no shock that as soon as I refactored my class out, my tests broke.

So, with those two thoughts in mind, here’s the API I wound up with, along with the full class:

public static class ResourceHelper
    {
        public static string GetResourceAsString(Type typeOfCallingObject, string namespaceName, string fileName)
        {
            return GetResourceAsString(typeOfCallingObject, string.Format("{0}.{1}", namespaceName, fileName));
        }

        public static string GetResourceAsString(Type typeOfCallingObject, string namespaceName, string folderName, string fileName)
        {
            return GetResourceAsString(typeOfCallingObject, string.Format("{0}.{1}.{2}", namespaceName, folderName, fileName));
        }

        public static string GetResourceAsString(Type typeOfCallingObject, string namespaceName, string[] folderPaths, string fileName)
        {
            var fullFolderPath = new StringBuilder();

            if(folderPaths != null && folderPaths.Count() > 0)
            {
                foreach (var folderPath in folderPaths)
                {
                    fullFolderPath.AppendFormat("{0}.", folderPath);
                }

                // Remove the trailing period
                fullFolderPath.Remove(fullFolderPath.Length -1, 1);
            }

            return GetResourceAsString(typeOfCallingObject, string.Format("{0}.{1}.{2}", namespaceName, fullFolderPath, fileName));
        }

        public static string GetResourceAsString(Type typeOfCallingObject, string fullResourceName)
        {
            var fileStream = Assembly.GetAssembly(typeOfCallingObject).GetManifestResourceStream(fullResourceName);

            if (fileStream == null)
            {
                throw new Exception(string.Format("Resource '{0}' not found!", fullResourceName));
            }

            var sqlStreamReader = new StreamReader(fileStream);

            return sqlStreamReader.ReadToEnd();
        }
    }

Note that most times, the first parameter is liable to just be “GetType()”. I could’ve maybe made it an extension method, or made the type a generic parameter, but it really felt like overkill for such a small class. Here’s some examples of calling it (from my unit tests):

ResourceHelper.GetResourceAsString(GetType(), "UtilitiesTests.RootResource.txt");
ResourceHelper.GetResourceAsString(GetType(), "UtilitiesTests", "RootResource.txt");
ResourceHelper.GetResourceAsString(GetType(), "UtilitiesTests", "ResourcesFolder", "FolderResource.txt");
ResourceHelper.GetResourceAsString(GetType(), "UtilitiesTests", new[] { "ResourcesFolder" }, "FolderResource.txt");
ResourceHelper.GetResourceAsString(GetType(), "UtilitiesTests", new[] { "ResourcesFolder", "NestedFolder" }, "NestedResource.txt");
 

Yeah, this is all extremely basic, but thought it still might be useful for someone. I had fun doing it, none-the-less.

New job, new challenges!

I know. It’s crazy. I had been dissatisfied with my old position for an extremely long time, and it eventually came to a boiling point, leading me to switch jobs in the middle of THE RECESSION!!! ZOMGS!!!!!

Things worked out amazingly, though. I had my new job before my severance even ran out. Nothing like getting paid to nurse my crack addiction play World of Warcraft.

I’m the start of what is basically a new team. I’ve been here a little over 3 weeks now. The previous team consisted of a single developer, who has been here for about eight years. He’s moving to more of a management position, while me and another developer (a Web/UI focused developer) will be working for him. There’s also 2 DBAs that we work closely with, but aren’t technically on this team. We steal a lot of their time though.

My role is “Senior .NET Software Engineer”. What do they mean by “Engineer” exactly, you ask? Well, that’s a good question that took me a bit to find out myself. I think I can explain it best by thinking of what an engineer by any definition really does – makes things work! My job is to make it work, however I feel that should be accomplished. My boss helps critique my proposed solutions, typically by giving me information about how many of the external systems interact with what I’m working on. That’s where a lot of the complexity is currently. The company deals with the health care supply chain. Not only is it my first foray into the health care arena, which is complex in and of itself, but the supply chain has many unique factors and forces at play at the same time. It’s a lot to take in. I’m finally getting a good base grounding (although not very in-depth yet) of how the entire system works.

My current “day-to-day” focus involves refactoring an existing system made for a single client to make it extendable for the second client it needs to support. I’m finally getting a chance to put to use a lot of the great information I had read recently. The code is pretty straight forward, so while it’s not hard to understand what it’s doing, it’s very hard to break up and make extendable. I’ve been referencing some books pretty frequently to help me make the right decisions, however, and I’m excited about how it’s turning out. My boss is liking my work as well (which is very important to the checkbook).

The work here is also extremely data heavy. Collecting and processing the data is a huge concern. Working with tables that contain millions of records, and importing, exporting, and converting them. So I’m learning a lot of information about how to write efficient SQL and designing secure databases.

On top of that, the company is fully committed to doing Scrum in a proper fashion. That’s great – but of course, each company will have it’s own “spin” on it. This is an extremely large company, and we’re actually a remote office, so it involves a lot of phone conferencing. So that’s been another challenge to learn how to work with properly.

All in all, however, this is going pretty great. The work is challenging. It’s a new business domain, which is a great change of pace, if nothing else. I’m being allowed to propose architecture improvements, and implement them. My boss is committed to making sure we have good hardware and the latest development tools. He’s also always looking to the future – we want to wrap up the current project, then move on to the next. Then the next. Then the next.

So, I hope to blog a little here and there about what I’m doing. I figure most people will find it mind-numbingly boring or introductory. Hopefully not though. A lot of it will be difficult to explain or may not make sense. We’ll see how it goes.

Oh, and a freaking huge 11.5% raise. It was over the top end of the range I was asking for. And a bonus plan that actually pays about 10% of your salary a year (and yes, people actually get their bonus). Take that recession!

Keeping scroll position between redirects

Had need of this on a project, and while I thought it’d be a large hassle, Elijah Manor was able to point me to a very simple to us javascript to do it – Scrollsaver.

All you do is include it, and it does the rest. It uses a cookie and an ID of your element to keep track of your last position. Very nice!

Silverlight – YOUR WORLD IS OVER

So, Sliveright THREE is in beta now, and it’s set to take over the entire web! If your site isn’t made in Silverlight, guess what? IT SUCKS. IT COMPLETLEY FAILS AS A WEBSITE. PERIOD.

This site? SUCKS.

Amazon.com? SUCKS.

eBay? SUCKS.

Google? SUCKS

“You’re obviously right, Rob. But I can’t put my finger on why you’re so right! Please tell me!”

Okay, I’ll tell you. But you keep your filthy fingers away from me. You sound poor, and I don’t like it when poor people touch me.

 

Gradients

You can make gradients with it. HOT. SHIT. You might have THOUGHT you could make gradients before, but guess what? YOU DIDN’T. NOW you can finally make them.

I know, you really did think you were making gradients with Photoshop or Paint.NET or something, and using them on your site, but you didn’t. You just made colors. They were garish and horrible.

And you certainly didn’t make a single grayscale transparent gradient, then use CSS to set that as the background image with a background color so you could have multiple gradients from image. Nope, you’ve never done that.

See, all those would be way too much work and time investment. Why open up image software, and make a gradient in 3 or 4 clicks? Now you can open up Blend, and make a REAL GRADIENT WITH XAML in 3 or 4 clicks!

So, now you can finally create gradients. You’re on your way to being a real developer. Right now there is only one real developer in the world, me, because I’m the only one who’s used gradients in Silverlight.

But why are they good? Because the world is nothing but ONE BIG GRADIENT. Next time you’re in the bathroom, and you feel your insides explode and issue forth an eruption of epic proportions, take a look at what you see – it’s JUST like a gradient. It’s not one solid color. It’s a rainbow, softly blending from one color to the next. Just like Silverlight!

 

Animations

You can FINALLY animate stuff! I know, once again, you THOUGHT you animated stuff before.

BUT.

YOU.

DIDN’T.

“Pretty sure I use jQuery to easily animate some stuff.”

Listen, I don’t know what jQuery is, so it doesn’t exist. Got it? It was never possible to animate UI before.

“Well, pretty sure I used Flash to animate a site like a decade ago.”

Will you quit making shit up? I’ve never used Flash either, so I’m pretty sure it doesn’t exist either.

Finally, with Silverlight, using Blend, you can make animations! It uses this great thing that the Silverlight team invented, called a storyboard, which consists of timelines, that have keyframes! It’s pretty simple!

 

Hover effects

This is a bit of a combination of Gradients and Animations (capitalized because they’re so important)! Now, when a user hovers over important things, like menu items, you can give a subtle gradient to pop into place to let the user know they’re over something sensitive!

“That’s been possible with CSS for yea—“

SHUT UP!

“I could do it with javascript even longer than t—“

WHAT PART OF SHUT UP ARE YOU MISSING?! YOU COULD NEVER HAVE A HOVER EFFECT FOR A WEBSITE BEFORE SILVERLIGHT!!!! SHUT THE HELL UP!!!!!

 

It’s one huge ass object

Well, I know FOR A FACT this one is new! Silverlight is now one single element on the page! This is super great, and really where Silverlight shines.

This really makes it, finally, the perfect solution for public sites. I mean, who needs that “web” crap. Now, it’s one solid object. You no longer have to worry about your public site being pilfered by nasty search spiders. I don’t even know what those are, but they’re called spiders, so I assume they have eight legs and crawl all over you. I have enough night terrors about them as it is.

Plus, people now can’t link to somewhere deep in your site. I hate it when people link to direct portions of my site, and skip all my advertisements or cool pictures of myself or something. Now, they have to see all that EVERY TIME. Hell yes.

Also, you can’t copy & paste my text now, jerks! You’re just going to have to paraphrase it. I’ll get you started:

“Then Rob was awesome”.

Done.

“Uh, I made sites entirely in Flash a decade ago, and it was a horrible idea, and that’s why only marketing sites do it now, You see—“

I’M GOING TO SHOTGUN YOU IN THE FACE. I’M BUSY TALKING ABOUT HOW BRILLANT SILVERLIGHT IS, AND HOW EVERYTHING IT DOES IS COMPLETLEY NEW AND BETTER THAN ANYTHING THAT EXISTED FOR THE WEB BEFORE, EVEN THOUGH I’M PURPOSEFULLY IGNORANT OF ANY AND ALL WEB TECHNOLOGY!!!

 

The Bad Stuff

Well, the main bad stuff is that all these non-Silverlight websites haven’t been converted yet. Hopefully soon browsers will support xaml natively. At least the One True Browser should do it soon.

But there’s really one thing wrong with it:

Case Sensitivity. Seriously. WTF. How freaking retarded is that. I mean, in this day and Age, why is ANYThinG case Sensitive? i can’T think WHY it would Matter at alL. AnD whiLE I’M on tHiS subJecT, I shOUlnD’t hAve to spEll thINGs correCTly eiTheR. it shouLd havE a DictIOnaRy of poSSiBle ValUEs, aND pIck teH nErEst wRd B-Cuz e dUn hAf tIm oR sKil 2 lErn al tHeeS wurDz i meN u nO hw eT s wHN u gTz so Mny wRd tt its impsB fr tO sTp fern rEd F dgeR DqoeR gler!!!!!

Google hates IE

And therefore, I hate Chrome a little less.

google-hates-ie

Top is Firefox 3, bottom is Google. I was exploring some jQuery stuff and decided to see if the behavior was any different in IE (suspecting it wasn’t), and noticed a slight difference. I signed into my google account on both to see if that caused a difference

While in my perfect world, everyone would use FFX, I’d still rather them use Chrome then IE.

I then did a search on google to see if anyone else has noticed this (or so I could see how long it’s been around), and while I didn’t see anything relating to this, it’s nice to know that google has been pushing for an alternative browser for a while now. Too bad they switched their recommendations though.

And no, I don’t know why I blacked out my email address. Everyone who visits this blog knows it anyways.

A better way to organize your CSS/HTML

It’s tough being a genius. It really is. Here’s my new guidelines for CSS/HTML:

  1. All tags are divs
  2. Use standard html tags as class/id names whenever possible

So, with our new awesomeness, we might get something like this:

   1: <div class="body">
   2:  
   3:     <div class="h1">My Title</div>
   4:     <div id="p">This is my awesome <div id="span">stuff</div></div>
   5:  
   6: </div>

 

Pretty freaking slick, eh? I’m hoping it can be integrated into Kobe soon!

Installing Live Writer

So, I just got a new computer, and I had an amazing idea of outstanding excellence (forthcoming), but needed to install Live Writer first.

First, it tries to get you to install FIVE HUNDRED other programs first. Piece of crap.

But, then, in the installer, it has a picture of a girl smiling at her bewbz. I mean, I smile at bewbz too, but I don’t see why it’s in an installer.

Proof:

image

Designed by Posicionamiento Web | Bloggerized by GosuBlogger