Wednesday, August 24, 2011

WCF, Data Contracts and Enums

With Windows Communication Foundation it is easy to create any kind of services, of course my opinion could be biased because I am a .net (mainly c#) developer, but I really find hard to believe that someone could disagree with such statement.

Of course there is a lot of know-how and trouble for beginners, specially trying to understand that WCF integrates all communication models we know (web, tcp, pipes, message queues) under one uniform paradigm.

But there are some moments where you hit a rock in the path; years ago I stumbled upon the following scenario:

I had a library “common.dll” that I used from both the client and the service. You might think this is not something you would do too often, and many people would say “that a client must not know anything about the service internals”. Well… that’s true, but this is not that case. It is just a situation where a client shares some business logic with the service.

And the problem?

Well, there is no problem with that configuration unless you have an enum in common.dll AND you tag it with the “DataContract” attribute. WCF allows you to do so, but I don’t recommend it. Usually when you have a common set of libraries referenced from both the service and the client, you mark the “reuse types in referenced assemblies” on Visual Studio, right? Look below:

Now, when you do that, and you have tagged an enum as a data contract you will get an error like this:

The solution? Well, do not attribute enums as Data Contracts if you reuse the assemblies. Here you can download a sample project with full source code so you can test this behavior. In order to check both situations, just add/remove the “DataContract” attribute from the MyEnum enum.

Thursday, August 11, 2011

Disable ENTER key on web pages with multiple forms

As an developer, you might find pretty common to have more than one form on a web page, sometimes you decided so, sometimes you just inherited a design and have to make it work. Let's just put the case where we have a form on the master page area, like a “search” or a “subscribe” button, and our inner page is a simple form with some fields to collect user information and submit it back to our site, like the image below.

Both forms will cause well-formed postbacks to the server if you click either on the search or the submit button, however if you are typing on a textbox and you hit ENTER, anything could happen. You might have inadvertently submitted form 1 instead of form 2 or vice versa.

Changing the default button of the page is simple and will help you if you know which button will be the one to respond to the ENTER key all the time. If you are using ASP.NET Just one line of code and you are done.

However, this might not work smoothly across different browsers and in some cases it turns out you might decide to completely disable the ENTER KEY on the page, how to do so? Well you can use some javascript and add it to your regular page or master page. See the fragment below:

And this should be enough, well, users will have to click on buttons, but at least they won’t submit the wrong data by mistake.

Monday, August 08, 2011

Before blogging about code....

The first thing that we need before blogging in a tech blog, is a way to display well-formatted code, and that means with all the syntax highlighting that our more beloved editors do. You can’t deny, after all, that reading code without colors, is not the same. So I checked a bit online and I managed to show code like this:

var total = (from u in ctx.Blogs
                     where u.Name.Contains("Blogger")
                     select u).Count();
        if (total > 1) return "I though blogger was the only one";

The best thing about this tool is that supports “brushes” to render a lot of languages, so we can have pieces of code in html, c#, c++, python or whatever we want, the setup is quite easy. I found many entries on internet but finally when to they integration page to really see it working.

So it seems we are ready to start blogging with source code!