Simple CSS Code Indentation
January 19th, 2012
admin Since I will be frequently supplying code examples, I initially devised a way to handle code indentation through css. My first attempt works, but was pretty sad, not to mention a space waster. And anyone who develops web pages knows the important of css as opposed to styling in an html document. First, it separates content from presentation, and it also reduces the download size to your end user. And smaller file sizes means your content will load in their browsers more quickly, taking away the variable of server load, of course.
So here is my first attempt:
Now if you copy the above examples into a stylesheet file name style.css and code.html you will see that the html is rendered with a perfectly formatted code example. However, looking at the example it certainly takes a lot of selectors to handle nesting of lines of code. In some complex code examples further indenting selectors would beed to be defined which is not sustainable.
As a software engineer, I wanted to simplify this greatly. Here is the simplified version. Note that the simplicity comes from the css itself, not necessarily from the markup. It completely alleviates the need to defined multiple nesting levels in your stylesheet.
At some point it’d be nice to write a quick C# program to generate the indented text. Even with the simpler example, it’s still quite the pain to get the formatting just right for display in a div area for copying the examples.
I hope this has helped anyone who needs to embed formatted code examples in their web pages
REST Easy
January 16th, 2012
admin I recently had the opportunity to work on a project in which we implemented REST as part of a solution. Since REST simply uses the HTTP protocol, how it is used is open to the desired architecture of the solution as long as it follows the HTTP protocol.
The REST vs SOAP debate is blogged about all over the place so I’m not going to go into that here. I believe, but have no proof, that anywhere SOAP is used, so too can REST with the added benefit that any stream can be returned with REST instead of just xml.
In this post, I am going to present the simplest code sample possible for implementing REST in .Net. In this example I will be returning a stream (formatted to xml, although JSON is certainly another option) and will be doing an HTTP GET to retrieve the information.
So here is the simplest example to get REST in .Net up and running.
This example assume the use of the .Net Framework 4.0.
First create a new WCF Service Application and ensure that you create a reference to the 4.0 .Net library System.ServiceModel.Activation.
The code base consists of three files.
It’s a fairly simple example. I’ve created a REST URI format of the type http://server/simplerest?getnumbers1to&lastnumber=12. If you compile the example and debug it and type that url into the web browser you will see something like this:
<?xml version=”1.0″ encoding=”utf-8″?><numbers><number>1</number><number>2</number>…<number>[lastnumber]</number></numbers>
What is happening in the code:
The web.config file is there for one reason only. The setting up of routes for REST requires the code base to require ASP.NET compatability. That’s it.
The Global.asax.cs file sets up the REST route table. Essentially you specify as many routes as you want. In this example we picked simplerest which then is required to be part of a query. The route table can also be version friendly, so we could set up a route such as simplerest/v1.0 and so on. You’ll notice the type of the service class is passed to add the new route.
Finally the SimpleRestService is the WCF service. In this example I don’t have an interface because my goal is to make the code base as small as possible. This is the meat of processing the REST GET request. It gets the action for the incoming request, ex. ?action=getnumbersfrom1to. Then it uses the current UriTemplateMatch object to get the lastnumber value. It checks if it’s the one action we support and creates a MemoryStream which contains an xml-friendly format to be displayed in the browser by using an XmlTextWriter.
Why I wrote this example:
To figure out how to do this I did a number of things. First, I bought the following book, which I highly recommend.
I love tech books and I wanted to add this to my library. Then, because I didn’t have the luxury of studying the book, I googled many sites until I figured out how to implement REST for my solution. The problem was, even after googling many sites there was still not a short cohesive example that I could find for setting up REST in the easiest manner possible. For this post, I condensed my knowledge to under 50 lines of code.
What is wrong with this example:
Well, everything, really. The one thing the code does right is it works. Let’s say I was your manager and I asked you to create me a RESTful .Net service and I gave you the REST interface I wanted supported and you delivered me the above piece of code. You’d be fired immediately. The code does not follow an object-oriented design paradigm, there is no error handling, no loose coupling, non-extensible, not data driven, and I can go on. The purpose of this code is so you can take the simplest possible example of what it takes to get a REST service up and running and deploy it in a sound architecture of your choosing.
I hope you enjoyed this REST example as much as I did writing it.
Unemployed, but still a Software Engineer
January 16th, 2012
admin So I’ve been thinking about what to do with this archive.
At my last two positions which lasted for a total of 11 years, I have been working as a senior software engineer working with mainly .Net technologies. I go beyond a simple coder in that I can do everything from the database, including design, to the middle-tier (or n-tier) to the UI layer (not design, mind-you, shudder), machine maintenance, builds, manage IIS, and all sorts of other various technical tasks. So I realize there are tons of software blogs out there covering any topic I can and will discuss. However, I plan to use this archive as a place for these things:
- Things that interest me relating to software.
- A log of answers to common problems that I find myself going to the Internet for again and again.
- Software topics that are beyond my main skills–I like to keep sharp and as current as possible with my discipline.
- Reviews of software, whether it be retail, or open source.
- Answers to anyone’s questions relating to software engineering. If it’s not a topic that I’m familiar with, I will do my best to find the answer.
I kind of want to make this a repository of sorts. There are so many things I can do within the realm of software development that I just inherently know how to do. However, time and time again I find myself quickly googling stuff I’ve done a million times before. How to I create a new anonymous type? How do I serialize an object to xml? I’ve done that millions of times, and yes it is only three lines of code. Why can’t I remember? And, as always, this forum is also for you to discuss, bring up more topics, and hopefully make this a place where developers can come to chat, solve problems, learn, and simply have fun.
Disclaimer: All code examples presented in my blog have been written and tested by me. Use them at your own risk. I will not be responsible for any use of the code in this blog for descriptive or example purposes. The code is not production nor enterprise ready. That being said, feel free to use, modify, and distribute the examples as you wish.


Posted in
Tags:
