Twelve Days of .NET: Day 1: Web API

This post is part of my 12 Days of .NET series. This is a (not terribly ambitious) series of posts on .NET topics that came up while I was working on a recent C# Web API project.

For the .NET project that I’ve been working on recently, I’ve had to play a bit of “catch-up,” since I haven’t been doing too much .NET/C# development recently. My part of the project was to create a middleware API (for lack of a better term) that would provide a simplified internal interface to a more complicated external REST API. I decided to make my API a REST API also, since that’s pretty much the standard at this point. I’d done a lot of work with SOAP APIs in the past. And I’d created a REST API in .NET at least once before, using WCF. Back then, I probably started out by reading something like this article from 2008.

The way to do this stuff now is Web API. The most up-to-date way would be to do this in .NET Core. I couldn’t do that, for reasons that are kind of complicated and not worth going into. (Actually, at some point, I figured out that I probably could do it with .NET Core, but by that point I was too far along to go back.)

I used a few resources to get started. I watched a couple of video courses on Pluralsight: Jon Flanders’ Intro to ASP.NET Web API and Shawn Wildermuth’s Implementing an API in ASP.NET Web API. I also read the book ASP.NET MVC 4 In Action. This was somewhat out of date (last revised in 2012), but was still useful.

So let’s call that my day one post: I started a Web API project, using VS 2017, and the standard .NET Framework.

Twelve Days of .NET

Back around Thanksgiving, I got interested in stuff like the F# Advent Calendar, the C# Advent Calendar, and Advent of Code. I’ve been working on a pretty cool .NET project at work recently, and I got the idea that maybe I’d do a series of blog posts about various tools and techniques that I’d come across while working on this project. I haven’t written a lot of programming-related blog posts lately, so I though this would be a fun way to get some programming content back on the blog.

I originally thought I’d try to follow the Advent Calendar model, and do one post a day for 25 days, starting on 12/1. Then, when I didn’t get around to starting that, I thought maybe I’d do a Twelve Days of Christmas thing, and do twelve posts from 12/13-24. Now that it’s past Christmas, I’ve decided to just do twelve posts and skip the whole Advent/Christmas connection. I’ll just call it “12 Days of .NET”.

I’m not going to be too ambitious about this. Most of the posts will probably be short, and I’m not going to post a lot of code. Mostly just links to stuff that I found useful. I’ve got most of the posts written already (at least in rough form), so I’m going to set them up in advance to auto-post every day for the next twelve days.

Black Friday

My Black Friday spending, so far, has been limited to renewing my Pluralsight subscription for their usual Black Friday rate of $200. When I paid for my first year, I wasn’t sure I was going to renew it for a second, but I’m getting enough out of it, so I guess it’s worth paying for another year. So I’m good through 2018 now.

I’m not seeing too many other Black Friday sales that look interesting, but I’ll probably spend some money at Comixology before the weekend is over. They have a lot of good stuff on sale.

Trying to catch up with .NET

I’ve really fallen behind with recent developments in the .NET ecosystem. At work, I spend most of my time in Dynamics AX, so I don’t get to work on a lot of pure .NET stuff. I’ve been trying to get current, but it’s really an uphill battle. Stuff changes faster than I can keep up!

I just finished a book on ASP.NET Core, ASP.Net Core Application Development: Building an Application in Four Sprints. (Even just reading the title on that book is exhausting!) I posted a review on Goodreads, so I won’t repeat myself here.

I have a little extra respect for the book, because it includes a quote from Lord Baden-Powell, the founder of the Boy Scouts:

Try and leave this world a little better than you found it and when your turn comes to die, you can die happy in feeling that at any rate you have not wasted your time but have done your best. ‘Be Prepared’ in this way, to live happy and to die happy — stick to your Scout Promise always — even after you have ceased to be a boy — and God help you to do it.

I think this was in the chapter on refactoring code. So with respect to programming, I guess it means I can die happy if I’ve done my best to refactor poorly-written legacy code, renaming obscurely-named variables, reducing cyclomatic complexity, and all that good stuff.

Anyway, while I got a lot out of that book, I didn’t really come out with what I’d call an actual working understanding of ASP.NET MVC. I mean, I understand the basics, but I’ve got a long way to go. And there’s so much related stuff to learn too. One thing I’ll say is that this book had the first explanation of dependency injection that actually made sense to me. (I’d heard it described in podcasts before, and had probably read a few blog posts about it. But I don’t think I really got it until the explanation in this book.)

I’m also trying to read ASP.NET MVC 4 in Action right now. This one dates back to 2012, so it’s a little frustrating trying to reconcile stuff in this book vs. the way ASP.NET Core 2 works now. But it seems like a good book so far.

ASP.NET Core 2 is pretty recent of course. Here’s a What’s New in ASP.NET Core 2.0 post from July and an Announcing ASP.NET Core 2 post from August. (The new Razor Pages feature is pretty interesting, by the way. I listened to a podcast about it last week.)

The two ASP.NET books mentioned above are both available via my ACM Safari subscription, so that’s how I’ve been reading them. There’s a lot of good stuff there. I’m also getting a little bit of use out of my Pluralsight subscription, but probably not enough to justify the cost. It was really useful for the SharePoint stuff I watched on it a while back, but for general .NET stuff, there’s plenty of free video training out there, through Channel 9 and other sites.

My bookshelves are a mess

Scott Hanselman posted a tweet yesterday with a photo of a bookshelf, asking people to reply with photos of their bookshelves. I was bored last night, so I took a few photos. This morning, I posted them to Twitter. A lot of other people replied to him too, with some cool photos.

I thought I’d post my photos here too. Of course, a large portion of my reading these days gets done via Kindle, iPad, and laptop, so my physical bookshelves aren’t necessarily reflective of what I’m reading these days.

It would be nice to have organized bookshelves, but what I have is really just piles of stuff, sometimes on bookshelves and sometimes on the floor or on various horizontal surfaces. It seems like the piles just grow, until they become structurally unsound, at which point I need to toss some books and/or reorganize. That’s one of the many reasons why ebooks are so great. I can buy as many as I want, and they never become a fire hazard.

I feel a little like I need to justify and explain these photos a bit, but maybe it’s better if I just let them speak for themselves. Or maybe not. The first one is a pile of random computer books, most of which I’ve read, but a few of which I never really got around to. The second is a pile of paperbacks, mostly Vonnegut, that I got from one of my brothers. (I can’t remember which brother.) I haven’t actually read most of them. (Oh, and there’s a couple of Ghost in the Shell DVDs on top of the paperbacks.) The third is some random SF paperbacks, all of which I have read. Mostly Gibson and Zelazny. The fourth is one of several “to be read” piles of graphic novels. There’s some stuff in there that I’m really looking forward to, including Grant Morrison’s X-Men run, a few volumes of Bill Winningham’s Fables, and some Usagi Yojimbo. (I’m a little embarrassed by the Vampirella book in that pile, but I’ll own up to liking Vampirella. It’s goofy cheesecake, but it’s fun.)

 

 

more TypeScript and SharePoint

I got a chance to work on another TypeScript / SharePoint project at work recently. I’m finding these projects to be a nice change of pace from my usual Dynamics AX work.

I’m not doing anything that’s much more complicated than I did on the last one, but I got a little more ambitious on this one. I’m still using Q for promises and Papa Parse for parsing an input CSV file. I think I’m getting a little better with promises, but I still occasionally have problems with them.

On my last project, I put all my TypeScript code in one file. For this one, I’ve broken things up into several classes and files. I’ve even gone as far as having an abstract base class with two child classes inheriting from it.

And, for the last project, I just deployed the generated JavaScript as-is. For this one, I’m using Web Essentials to combine and minify the JavaScript. That’s working better than I thought it would.

I’ve also given up on the idea of deploying this thing as a regular SharePoint sandbox project, though I’ve set it up that way in Visual Studio. Instead, I’m simply copying the HTML and JS files to the SharePoint server with SharePoint Designer. Maybe that’s not the best way to do it, but it’s the easiest, and the least disruptive to the server. (I should look into whether or not it’s possible for me to do that with PowerShell.)

more on subscriptions

This post is a follow-up to this morning’s post about subscription software and services. First, it turns out that I do have a 7-day trial subscription to Comixology Unlimited. I got an email today telling me that it would expire soon and that I could convert it to a full 30-day trial, if I wanted to. But it looks like it won’t auto-convert into a paid account, so that’s nice. (It would have been even nicer if Comixology was more straightforward about it to begin with, but close enough, I guess.)

On the O’Reilly item, there was a discussion about this on Hacker News today. (And one on Slashdot, though I don’t pay much attention to Slashdot these days. And another on Reddit.)

In the Hacker News discussion, a few people mentioned the Safari subscription that’s included with ACM membership. This has always been a limited subscription, with only a small subset of the full Safari library, and a limit of ten books on your “bookshelf” at a time. Well, it turns out that ACM now offers full access to Safari, starting yesterday. (See this FAQ for details.)

While I’m pretty happy to have found out about this, I’m a little confused. A full Safari subscription costs $400 a year. An ACM membership costs about $100 a year. I’m not sure why O’Reilly would give away full access to Safari like this. ACM is a professional society, but (as far as I know) they don’t enforce any real membership criteria. Most members probably have at least a BS in Comp Sci, but I don’t think it’s a requirement. So I don’t know, now, why anyone would pay $400 for Safari when they could just give $100 to ACM and get Safari, plus the other ACM benefits. Maybe I’m missing something. Either way, I guess I don’t need to worry about buying any computer books any time soon. (I’ve been an ACM member for many years.)

This all gets me thinking about Pluralsight again. I paid for a one-year subscription back at the end of last year. I got some good use out of it for a while, but I honestly haven’t done much with it over the last few months. Part of the reason for that is that I was using it to get up to speed on SharePoint programming; I’m not currently working on any SharePoint stuff, so I really don’t need to finish watching all the SharePoint content I had queued up. Another reason is that they’re cracking down on streaming video at work, due to concerns about bandwidth usage. While Pluralsight is probably OK, I don’t want to risk showing up on any management reports, so I just haven’t watched any videos at all at work recently.

I don’t have any particular area of technology that I need to learn for work right now. I should probably pick something new to work on in my spare time. I could get back to F#, but I’ve read several books on that, and I don’t see much value in getting back to it without having a real project to work on. Or I could get back to Ruby on Rails; I really didn’t get very far with that, the last time I started working on it. I picked up some TypeScript skills on my last SharePoint project, and I can see where there’s a lot I could work on with TypeScript and modern front-end development in general. (There’s a lot I don’t know.) Or I could try to learn more about .NET Core, which would be more in keeping with the career track I was trying to keep myself on before I got side-tracked into my current Dynamics AX position. Oh, and hey, Swift looks interesting!

So I should really pick a topic, then watch some Pluralsight videos and/or read a book or two on Safari, and see where that takes me.

Shrug

At work today, I decided that I needed to be able to more easily get the shrug symbol into my emails and messages. This probably says something bad about my attitude towards my job, but: (shrug).

I specifically wanted to use the multi-character text symbol, and the not single-character emoji. That symbol is a combination of simple ASCII characters, plus a couple of fancier UNICODE characters. This article has some interesting background on it:

Taking ¯\_(ツ)_/¯ the worldview to its logical conclusion, Foster makes the fatalistic argument that everything is predetermined and space-time is a false construction of the human mind.

Well, OK, maybe I’m not ready for that worldview, but it seemed like a good response to the guy who asked me if I could kill an out-of-control SQL process this morning. (Final determination: I could not.)

So I went to AutoHotKey and tried to define a little macro. My first attempt, which should probably have worked, didn’t. (Probably because my AHK script file is UNICODE, but the wrong kind of UNICODE?) Anyway, changing it so that the UNICODE characters are specified with their hex codes fixes it. So now I can type “backslash backslash shrug” anytime I need a shrug!

; doesn't work right.
; ::\\shrug::¯\_(ツ)_/¯
; works great!
::\\shrug::{U+00AF}\_({U+30C4})_/{U+00AF}

By the way, WordPress is kind of weird about displaying the shruggie too. It sometimes eats the backslash and sometimes doesn’t. It looks OK in the blockquote above, but when I tried using it directly in this post, not in a blockquote and not in a ‘pre’ block, it kept eating the backslash. Even when I entered the escape sequence instead of directly entering the backslash character. I think that’s a mystery for another day though. (Shrug!)

more JavaScript and TypeScript shenanigans

I’m still working on my fairly ridiculous JavaScript/TypeScript/SharePoint project, which I’ve mentioned previously. I’ve got the Q (promises) stuff all worked out now, I think.

I wanted to be able to read in a standard Microsoft Excel file as part of this project. To handle that, I looked at SheetJS. This looked like it would be pretty straightforward. But I hit a few issues in trying to integrate it into my project. There was a TypeScript definition file for it available via NuGet in Visual Studio. But the way the file was written led me down a rabbit hole of trying to figure out JavaScript modularization standards. This probably isn’t that tough a subject to figure out. I got as far as figuring that I should be using AMD and RequireJS. But, after about 20 minutes of reading RequireJS documentation and some related articles about how to use it in TypeScript, I decided that maybe I was better off skipping the whole thing and sticking with a CSV input instead of Excel.

I assumed I’d be able to read and parse a CSV file pretty easily, with the native JavaScript FileReader and some simple string operations to break up the lines into fields. But figuring out how to parse a CSV file sent me down a rabbit hole too. But it wasn’t a terribly deep rabbit hole. The end result is that I’m using a little library called Papa Parse to deal with the CSV parsing. (It turns out that the kind of CSV files I have will be a little too complex for “string.split(‘,’)”.)

Despite all the little issues, I’m learning a lot on this project. Maybe, most importantly, I’m learning about how far JavaScript development has come since the last time I did any major JS work. I need to do some catch-up work. Time for some more PluralSight videos.

Promised You a Miracle

I’m still working on the JavaScript project that I mentioned last week. I think I’ve nearly got promises figured out now. I’ve switched from using jQuery’s promises to using Q. I’m definitely doing enough weird stuff that the jQuery promise support was never going to work for me.

The documentation for Q isn’t bad. There’s a page on moving to Q from jQuery, and that was helpful, as is the API reference page.

This blog post on promise anti-patterns helped me figure out how to handle collections of promises. Q.all() and Q.allSettled() are very useful, once you figure them out. I get the feeling that, while my code works, I’ve probably still got a lot of anti-patterns in there, or at least some fairly sub-optimal constructions.

While working on this stuff, I found that the old Simple Minds song, Promised You A Miracle, started running through my head. I was really hooked on Simple Minds for a while there, back in the early eighties. I hadn’t listened to them or even really thought about them in years, so I’m enjoying a little nostalgia now, via YouTube. (Eighties music videos do look pretty cheesy now, though, huh?) Here’s a new version of the song, an acoustic version with KT Tunstall. It’s not bad.