Skip to main content

Posts tagged with 'c'

Welcome to Cross Cutting Concerns. "Weekly Concerns" is a post-a-week series of interesting links, relevant to programming and programmers. You can check out previous Weekly Concerns posts in the archive.

I've been working a little bit here and there on a mobile app. One thing this app does a lot is use a RESTful API over HTTP, where the results are JSON.

I'm using RestSharp to handle these requests, and RestSharp can automatically serialize Json results into C# object(s), assuming I have C# objects that match.

Well, some of these JSON results can be a little complex. Not terribly complex, mind you, but enough to where it's annoying to create the classes myself. But I thought to myself, maybe there's a tool out there that already does this for me. I googled around and found json2csharp, which is a handy web app that does exactly that.

Well, thanks to Rob Gibbens, I now know that this feature is actually built-in to Visual Studio 2013, and available for 2012 as part of "ASP.NET and Web Tools".

Paste JSON As Classes in Visual Studio

You can see that there are some differences, but these tools get you most of the way there!

With my expanded focus, I decided to bring back my weekly link collection under the "Weekly Concerns" banner. Once again, this is just an excuse to avoid the work of having to write a real post.

Ack, WebForms! Burn it with fire!

WebForms is known to be a horrible ghetto full of HTML pitfalls, a Kafka-esque lifecycle, testability nightmares, and pages served up with inflated ViewState data.

Some of that is still true, but WebForms has improved, and despite all the horrors that we're familiar with, it's still got a lot going for it, including a control tree that can be very useful at times.

I have been working on a WebForms project in my independent consulting practice (it sounds so fancy when I say it like that). Even though it's a relatively small project, I was still thinking about how best to introduce dependency inversion [PDF] to the project (or if I even should).

I googled around, sifted through a lot of ancient blog posts, and I came up with a relatively simple solution with my favorite IoC tool, StructureMap. It's not really optimal, or terribly elegant, but it just might get the job done and still allow us to write tests. I'm certainly open to alternatives, but because this is a small project, I don't want to spend a ton of billable hours on a solution, especially when I'll be turning the code over to novice programmer(s) when my time is done.

First, I simply add standard StructureMap config to the project. Application_Start in Global.asax is as good a place as any. I also use the default convention (if you aren't familiar, it's basically naming the interface by prepending an "I" to the concrete class name).

Okay, so that's pretty standard. Now let's approach from the other end: an actual code-behind class of an ASPX page. I can't use constructor injection here, because I don't have much control over this object being instantiated. Instead, I'll just make public properties for my service(s).

So now I've got a StructureMap defintion, and I've got properties that want to be set by StructureMap. But how do I get them talking? My solution was to introduce a base class (bleck) to the WebForms page. In this constructor, I'll use StructureMap's BuildUp method to populate properties (I have to specify which ones, as noted later on).

That's the ugliest part, as far as I'm concerned, but I couldn't think of a better solution. Maybe an HttpModule or something? Maybe the newer WebForms releases contain something that I'm not familar with yet? Leave a comment with your suggestions.

Finally, StructureMap just won't go and set every property by itself. You need to specify which ones. There are a couple of ways to do this. One is the SetAllProperties, which allows you to conditionally examine the PropertyInfo, and the other is FillAllPropertiesOfType, which you can use to directly specify which properties to set based on what type they are. Even though it's a small project, I'd would prefer to use SetAllProperties, so I don't have to go back and edit my StructureMap config each time I create a new service or a new WebForms page.

When I open-sourced my C#-rewrite of Ledger recently, I realized that it was quite ugly. Since I look at it nearly every day, I figured I'd better make it look decent (previously I was using a CakePHP default theme, which isn't exactly a treat, but wasn't hard to look at either).

Two things I'm doing with Bootstrap that maybe not everyone agrees with or wants to do. But they make things easier for me:

  1. I'm using a CDN to host Bootstrap files instead of hosting them myself.
  2. I'm not using LESS, just plain Bootstrap CSS

For more customized or more critical projects, I could be convinced to do otherwise.

Getting started with Bootstrap is pretty easy, just add a JavaScript and a couple CSS references and away you go:

From there, you can start by looking at the basic Bootstrap template, which is far from intimidating.

Next, there are a bunch of examples, one of which you might want to use a starting point. For Ledger, I simply used the Starter template, and built from there.

The next step would be to check out the CSS classes that are available: what they do, and how they work. Then, you can drill down even farther and look at some of the very useful components that are available.

You can customize styles, and for more elaborate projects than Ledger or this blog, you will certainly need to. But I found nearly everything that I needed within the CSS classes and components. As a nice bonus, your pages will become responsive! If you are reading this post on an Android phone, for instance, note that it takes up the full width of the screen. On my desktop screen, it only takes up maybe 70% of the screen, with the sidebar column with "About Me" and such takes up the remaining 30%. Also, the navbar automatically shrinks down into a "hamburger" menu.

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter