DCSIMG
April 2011 - Posts - IronShay

April 2011 - Posts

Forget 42, –1 is My New Answer to Life

I’ve just stumbled upon the next code statement – Thread.Sleep(-1). It left me wondering what was happening there since MSDN tells you nothing about a –1 value for the milliseconds parameter:

“The number of milliseconds for which the thread is blocked. Specify zero (0) to indicate that this thread should be suspended to allow other waiting threads to execute. Specify Infinite to block the thread indefinitely.“ – System.Threading.Thread.Sleep, MSDN

To check that out, I opened the IronRuby interactive console and filled in System::Threading::Thread.Sleep(-1) and hit Enter just to find out that this call blocks the thread indefinitely. Could it be? –1 is Infinite?

Reflector to our aid! oh wait, RedGate now charges money for it and had planted a time bomb inside the free version which made it stop working. grrrr
ILSpy to our aid! (I highly recommend ILSpy as a Reflector alternative… very similar, free, oss… great community effort!)

Anyway, ILSpy proved my concerns:

System.Threading.Timeout.Infinite Value

So… –1 actually represents Infinity (when it comes to threading in .NET). And Infinity is much cooler than 42, hence –1 is the new answer to life.
Q.E.D.

All the best,
Shay.

Posted by shayf | with no comments
תגים:,

Razor Tip #2: Output Text in Code Context

Razor, the new ASP.NET MVC view engine is incredible. I like it. A LOT. Great work Microsoft!

In this series of posts I’m sharing some handy tips and tricks that can enhance your experience with this new view engine. Enjoy!

The Problem

Razor is the place where HTML and C# live together in harmony. This is, in my opinion, one of the things that make razor the great view engine that it is. However, there’s a fly in the ointment. Assume you want to output “Good Morning!” if the hour is between 6AM and 9AM:

Razor Tip #2

But oh no! Razor thinks “Good Morning!” is code!

The Solution

Razor has a special solution for that problem. Three different solutions for the matter of fact:

  • Writing the text inside an HTML element – razor knows to differentiate between C# and HTML so if you wrap the text with an HTML element, everything will work as expected:
    Razor Tip #2
  • Adding the “@:” symbol at the beginning of the line – using this symbol will tell razor that this line is an output line and should not be treated as code:
    Razor Tip #2
  • Using the special <text></text> element – when you want to output several lines within code context, it becomes irritating to use the “@:” symbol… For that you’ve got the special <text></text> element which tell razor that its content is to be outputted as HTML. Notice that the <text> element will not be outputted to the final HTML output.
    Razor Tip #2

All the best,
Shay.

Posted by shayf | with no comments

Razor Tip #1: Explicitly Stating Statement Boundaries

Razor, the new ASP.NET MVC view engine is incredible. I like it. A LOT. Great work Microsoft!

In this series of posts I’m going to share some handy tips and tricks that can enhance your experience with this new view engine. Enjoy!

The Problem

You have to output a variable value and add some markup content immediately after it. For example, assume you have a variable holding a value representing a font size and you want to output it inside a style attribute, as follows:

image

But oh no! Razor can’t tell the difference between our fontSize variable and the “px” text that follows it so it will go look for a variable named “fontSizepx”!

The Solution

To solve the issue, we need to explicitly tell Razor where the variable name ends, or in more general terms – where the statement starts and ends. We do that by putting the variable name between parentheses:

image

You can also use the parentheses trick to output small code statements like these:

image 

All the best,
Shay.

Posted by shayf | with no comments
תגים:, , ,

LINQ Tip: Chain Ordering

Assuming you have the next code:

public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

public class Whatever
{
  public void Do()
  {
    var people = new List<Person>
                  {
                     new Person {Name = "Shay Friedman", Age = 27},
                     new Person {Name = "Shawn Doe", Age = 51},
                     new Person {Name = "Elvis Presley", Age = 76}
                  };            
  }
}

And now you want to order it first by name and then by age using LINQ. If you were to do that:

var orderedPeople = people.OrderBy(p => p.Name).OrderBy(p => p.Age);

You would get incorrect results:

LINQ Tip: Results when using OrderBy after an OrderBy call.

That’s because the second OrderBy call just overrides the first call results. To fix that, use the ThenBy LINQ method:

var orderedPeople = people.OrderBy(p => p.Name).ThenBy(p => p.Age);

And now everything works as expected:

LINQ Tip: Using ThenBy after an OrderBy call.

All the best,
Shay.

Posted by shayf | with no comments

Wrapping Up my “Swedish Tour”

File:Flag of Sweden.svgI’ve just got back home after a week in Sweden. During the week I had the opportunity to travel the country a bit, to learn a few Swedish words and to speak in a few occasions about ASP.NET MVC, IronRuby and some other technologies. But above all of that, I got to meet and talk with some incredible people! it was such an amazing experience!!! thanks to all of you (you guys know who you are), you made this week one of the best I’ve ever had.

Well, yes. I know I titled this post a “wrap up” and you want the details. So…

 

 

 

Part 1 – Stockholm

The first part of my “tour” was Stockholm. I was invited to Stockholm by Tibi to participate in the unconference he had been planning. It had been snowing in Sweden before I got there so I was welcomed by a lot of snow. And I LOOOOOOOOVE snow! I was sad, though, that it wasn’t snowing while I was there…

Tibi hosted me in his place during my stay in Stockholm so huge thanks to him, to his wife Nicolleta and to his sweet daughters for having me!

On the day after I came we had the unconference in a very cool office in Stockholm. I was impressed that even university students showed up for the unconference! good for you! During the unconference we had very interesting and inspiring discussions about various different and unrelated subjects (in the .NET world). I got to talk about IronRuby and it was very interesting for me to hear the questions and the feedback.

Some photos from the event:

.NET Unconference in Stockholm.NET Unconference in Stockholm

Part 2 – SDC2011, Gothenburg

The day after the unconference, me and Tibi took the train to Gothenburg to participate in the Scandinavian Developer Conference, AKA SDC or ScanDev. We got to an amazing hotel called Gothia Towers in Gothenburg where all the speakers were hosted. The conference itself took place in a convention center that is attached to the hotel. This is how the hotel looks like from outside and the inside:

My room in Gothia Towers HotelGothia Towers Hotel from Outside

On the first evening we had a speakers dinner organized by the conference organizers. They took us to a very nice restaurant and I got to meet and talk with other speakers about technologies, languages (real ones! not just programming languages!) and other stuff. These meetings are the best thing in conferences… the opportunity to chat with people from all over the world (face-to-face) is not something that you run into every day.

Anyway, the next day I had my session, The Big Comparison of ASP.NET MVC View Engines, and it went pretty well. I had something like 100 people attending and had lots of fun. Razor won the poll again, by the way. I wrote another post about the session, in case you want to see/download the slides or code samples.

On the evening we had some food at a nice little restaurant, had interesting discussions and lots of alcohol. Everything is a bit blurry for me from this night… I love conferences! :)

On the second day of the conference I didn’t have any sessions so I got to relax, go to some sessions myself and in general, have fun! In the evening we went to a traditional Swedish restaurant and had some traditional Swedish food. It was a blast!

Part 3 – Swenug (Swedish .NET User Group), Gothenburg

The last part of my trip was the Swedish .NET user group meeting in Gothenburg. I had two ~1 hour sessions, one was about tips and tricks in ASP.NET MVC 3 (or the session’s official name – “ASP.NET MVC Rulezzzzzz”) and the second one was about IronRuby and its possible usages for .NET devs (or in its official name, “IronRuby FTW!!!!!!!”). I must say, these were one of the best presentations I’ve ever had. It felt good, I had an awesome awesome AWESOME time, got very good responses afterwards and even all the demos worked!

Thank you Anders for having me and pulling this off.

Summary

It was an amazing week. I had a chance to speak in front of the great Swedish crowd. I met so many interesting people. I ate a reindeer (sorry Santa). And I got to see snow!

Sweden, I’ll be back!

All the best,
Shay.

Posted by shayf | with no comments

MVP for Another Year!

Two weeks ago I received the email from Microsoft, notifying me that I’m a Microsoft C# MVP for another year!

MVP!
I’m very excited that I get the opportunity to continue and affect future Microsoft releases. Thanks CodeValue, Microsoft Israel and everyone else for the support!

I’d also like to congratulate the other renewed MVPs and the new addition to the list of Israeli MVPs – Shlomo Glodberg. Mazal Tov Shlomo!

All the best,
Shay.

Posted by shayf | with no comments
תגים:,

Slides + Code Samples from my Session at SDC2011

Today I had the honor to talk before the Scandinavian crowd in Gothenburg at SDC2011. My session was “The Big Comparison of ASP.NET MVC View Engines” where I compared ASPX, Razor, Spark, NHaml and the StringTemplate view engines.

Thanks to everyone who attended! I hope you had as much fun as I had!

 

The code samples from the session can be downloaded from my github account: https://github.com/shayfriedman/AspNetMvcViewEnginesSamples

And here are the slides:

A recording of the session should be available on the conference site in a few days.

All the best,
Shay.