Showing posts from November, 2012

Roslyn shopping cart DSL - Part 2

In this post I will be reviewing some considerations that I usually have in mind while designing a DSL.
The audience When you are working on a DSL, the first and most important thing to have in mind is the audience you are aiming for. It’s gonna be technical people, business executives, sales people, etc…, etc….
The syntax If the audience is not a bunch of techies, here is set of design rules that you should follow (at least, they've worked out for me): The syntax should be as much as closest to the domain terms that the user uses on a daily basis.Use sentences like “when something_happen:” instead of “if (somethingHappen){..}”.Use logical operators like “and”, “or” instead of “&&”, “||”.Don't make your syntax case sensitive.snake_case works better than PascalCase.When syntax error happen (and it will happen) provide useful messages that lead the user to the right path.Try to avoid the use of parentheses as much as you can. (our users are not Lisp programmers ;))You are no…

Roslyn shopping cart DSL - Part 1

Lately, a lot of people where asking on mailing lists and users groups, how to dynamically load and execute C# code. This code maybe be stored as expressions in the database or will be entered by the user in some sort editor or whatever they want to use, but the important thing is that the app should compile and execute the code at runtime. Unlike Ruby or Python, C# does not support this kind of stuff right out of the box. Fortunately, the Redmond guys had implemented a nice library called Roslyn, which allows us to do this and a lot of other crazy stuff.
Whereas this was possible in the past by using reflection or the CodeDomProvider, it were a lot of work (complex and error prone work), so, very few people did it.
In this post I’m going to show a little app that uses Roslyn to compile and execute C# code at runtime.
Instead of work with expressions like 2+2 or a>b (a.k.a. the classic “hello world!” example), I’ll work with something more close to a DSL. This DSL will evaluate expre…

Migrating an ASP.NET MVC 4 app from Azure websites to WinHost

About a week ago I've to migrate an ASP.NET MVC 4/EF5 application from Azure websites to WinHost. While the process was really smooth, there were some caveats related to database connections that I want to share with you.
Create and setup the ftp profile on VS and configure the connection string was really easy, WinHost provide you those values and there is nothing special here. But once you deploy your website and try to see it online, you may get the “yellow screen of dead” with the message:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
Assuming you wrote the connection string properly, this happens because you cannot use the default connection name in your web.config file (this …

How to create MS Word documents from Office templates using C#

The OpenXML SDK allows you to do pretty much anything you want with office files such as Excel, Word, etc… While many people like this library, I found it complex, unintuitive and poorly documented, not to mention the awful xml format that uses under the hood to represent the documents, styles, etc. So I decided not to use it and build my own solution. If you, like me, don’t like that library, you will find in this post an alternative approach to build word documents from templates using c#.
A neat trick to work with Office is to use the macro recorder to understand how things work. The macro recorder allows you to start a macro, do something by hand, stop it, and then take a look at the generated VBA code. Once you do this, you are pretty much set.
This is how it looks the template I’am going to use.

Note: save the file as a Word template (.dotx)
This is the code to create Word documents from C#:

By running the code, you should get a document that looks like this.

Note that the font, forma…

How to authenticate a console app to an ASP.NET MVC 4 web site

In this post I will show you how to authenticate an application to an ASP.NET MVC web site that uses forms authentication. This sounds a bit weird, but as I found out on the web, this is a fairly common thing to do these days.
The most common scenario where people want to do this is, is when they want to consume an ASP.NET MVC web site as if it was a Web API, to get resources that are already in place and are accessible thru controller’s actions.
There is couple of ways of doing this, and it is pretty easy until you need to authenticate the request in order to get those resources.
The web site we are working on uses Forms Authentication, this means that when a request arrives, the ASP.NET infrastructure is going to see if this request is authenticated or not, if is not, it will redirect the request to the login page. While this works fine for web pages, is not so good for APIs. Down below I’ll show how to authenticate a client to a login page using the HttpClient class. 
* note: I'm a…

Hacking the Kindle Fire's keyboard layout

If you bought a Kindle Fire and want to use it only for reading, don’t worry you will be fine, but if you want to use it as a tablet, as soon as you try to write something in a language other than English, you will realize that you gave to yourself a pretty expensive brick ;).  The Kindle Fire comes with an autocomplete feature (that cannot be disabled) that inserts matching words when you press the space bar, while this is arguable a nice thing, if you are writing in a language such as Spanish it’s a bit of a pain in the ass. You have to erase and retype all of the words inserted by this feature.
Surfing the web you will find a lots of comments saying that is not possible to change the Kindle Fire's keyboard layout, while this is true based upon the Fire’s defaults options, there is a guy who hacked the system and give us a neat way to change it.
This guy is Gero Zahn and this is how he did it
There is another way…