Category Archives: C#

The Migrations for .NET project is not dead yet!

I know, I know, since I got the chance to work full-time with Ruby I pretty much abandoned all of my .NET projects. Most of them died silently, in piece. They are now gone to a better world, with full coverage and never failing tests, clean and usable design, fast and beautiful code, funny and complete documentation, transcendent and logical structure… I hope, *tears*!

But one refuses to die!

MigratorI keep getting patches and questions for the Migrator. And hopefully for my fellow .NET readers, Nick Hemsley will be the future maintainer of that project. And by the refactorings he’ve already done in a branch, I can tell you that great things are still to come for that project! Please send all your love to Nick and thank him for rescuing another useful project from dying!

Thanks Nick!

2 Comments

Filed under C#

The Hacker’s Healthy Guide, a simple 3 steps algorithm

I love to eat! In fact I eat a lot! And I have the great fortune to be the partner of one marvelous chef
that cooks amazing meal almost every day!

But I’m a hacker, I like coding even more then I like to eat. So I spent most of my time sitting in front of a computer without any apparent movement, eating stuff and drinking a lot.

So what I eat everyday is important to me, but I’m not a grano-hippie carrying my bag of nuts and dried fruits. And I believe any diet or restriction on the aliments you can eat are the biggest bullshit ever. What I do is a quick check of the nutrition facts label of each product I buy at the grocery.

Fat

fat fat fatI don’t really check the fat quantity. Saturated fat should be low. But the important thing is Trans fat should be 0. Almost no product with trans fat ever enter my house. This type of fat is transformed in a way your body can not recognize it as fat, so it won’t end up in the right place and it’s not the type of fat you can loose by exercising. It’s there forever, like Microsoft hidden libraries when you install Office!

Sodium

salty!This one is important because it’s high on several products that are low fat or low calories. If it’s low in fat but has more then half your daily value in sodium (often the case for dried noodle soup) it’s a no-no, it would be like eating salt!

Protein

juiceEver wondered why drinking a glass full of fruit juice will keep you hungry and only one apple can sustain you for a couple of hours ? That’s because the juice doesn’t hold any proteins. Proteins expand in your stomach and make you feel satisfied. If a meal hold lots of proteins you won’t need to eat as much to get going!

Code samples

If you find this boring just read the code bellow, it’s a simplified version of the algorithm in the language of your choice:

Ruby:


  def eat?(product)
    product.trans_fat == 0 &&
      product.sodium < 1000 &&
        !product.proteins.empty?
  end

Python:


  def eatable(product):
    return product.transFat == 0 and product.sodium < 1000 \
                                 and len(product.proteins) > 0

C#:


  public bool IsEatable(Product product) {
    return product.TransFat == 0 && product.Sodium < 1000
                                 && product.Proteins.Length > 0;
  }

Lisp:


  (defun eat? (product)
    (and (= (trans-fat product) 0)
         (< (sodium product) 1000)
         (/= (array-total-size (protein product)) 0)))

Closing tips

asian pearEven if you have to eat frozen food for lunch there are some good choices out there, just check out those 3 things. Plus, fruits and vegetables are always a winner. But cutting, wrapping the thing can be pretty annoying. To bring back excitement, joy and extaze to your meal think about trying new fruits like: asian pear, dragon fruit, kaki, kiwi, mango… OK that’s enough I’m starting to dribble.

Now what are your tips on being an healthier hacker ? I’d really like to know! Or maybe you’d like to contribute to the translation of the (really complex!) algorithm (or correct my poor lisp coding skills). Please do, I’ll add it with due credit!

Bon appétit!

Warning if you’re gonna submit some code in the comments:
My friend wordpress.com seems to eat some part of the content when we use < or >. This is stupid, but if you’re going to submit some code in a comment, replace the < by &lt; and > by &gt; or just send me your code by email at macournoyer_AT_gmail.com. Sorry for the inconvenient.

12 Comments

Filed under C#, Misc, python, ruby, tutorial

DevTeach : The .NET place to be

Just came back from DevTeach day 2. Wow! So much great speakers. A year ago, I would never had tough that Montreal was a place for such a great event. I’m pretty proud to’ve been a part of it.

Now! Enough for the flattering.

First I attended Claudio Lassada’s Tips & Tricks to boost up productivity session. He presented a couple of tools to speed up things (slickrun, jot, sysinternals). But I still think the key is only to go mousless, everything else will follow (And I think launchy is a lot better then slickrun). Among other things he promoted dual screens, weekly brown bag meeting, in which you share tricks you’ve learned, and CodeRush (I prefer ReSharper). Still it was really entertaining and a great start for the day.

Then I jumped to the Agile room where I stayed all day. Scott Bellware kicked it off with a talk on Behaviour-Driven-Design. The whole session was about refactoring a real world example using a TDD approach. He then presented BDD as a way to make test code more english-like and self documenting. It was a great presentation and a real convincing one. He talked about RSpec and NUnit.Spec but not as much as I would have expected. He sure looks like a bright guy (read: he’s bald). During that same presentation Jeremy Miller had some noticeable comments:

If its hard to test change it!

Yeah testability is one important thing when you don’t have the flexibility of a dynamic language.

After that was Oren Eini on Rhino Mocks. Even tough it was hard to understand what he was saying at first (strong Israel accent I guess) it was worth the effort. He made pretty obvious all the powers of Rhino Mocks. One other amazing things was how fast he was coding. I first tough it was a recorded video played with boosted speed. Not! He’s a true ReSharper jedi! He ended on a thoughtful note:

watch out for test paralysis

In other words: keep your tests flexible so they don’t break all the time.

Lunch was filet de porc in some brown sauce. Vegetables? Yes! Great I was hungry! Dessert was strange tough: looked like ice cream but was really a cake with strawberry toping inside. Still it was okay plus I had some great talks. Finished my coffee and I was up and running.

During lunch, in another room (yeah, really while lunch was served!!!) there was a talk about pair programming by two girls from Oxygen Media. Great talk, too bad I came too late (I was hungry!) I missed the rabbit story, what was that about ?

Then was Udi Dahan on What to Test and When. Udi is such a great speaker, so much energy when he talks, he sounded like Anthony Robbins on software testing. He focused on the importance of testing every aspect of the software on each iteration, specially the risky parts: performance, security, complex business logic and external interfaces.

Finally, Oren Eini finished it up with a MonoRail tribute. I don’t know if the crowd cough the brilliance of MonoRail, only 2 people, including I, had used Ruby on Rails! I tough Oren approach for a first dive into MonoRail was brilliant. He started with a browser and pointed to http://localhost/home/index.devteach then followed the exception messages for adding a controller, an action and a template. Talk about descriptive error messages!
The funniest quote of the day is definitely when he pointed out some recent comment on brail’s template, made by developers on an earlier talk:

This is Perl, I’m not doing Perl!

funny!

1 Comment

Filed under C#, Castle, conference, montreal

Great Castle Windsor articles

Alex of Bitter Coder got some great articles about Castle Windsor container project.

Here’s the last one about Startable Facility or if you’re new to this great tool start with Part I – Simple configuration.

Nice work Alex, thanks for sharing your knowledge.

1 Comment

Filed under C#, Castle

Finally some good moves from MS

Some Microsoft insiders recently announced the upcoming release of a Dynamic Language Runtime (DLR) built on top of CLR.

I’m a little confused by the Silverlight project web site, on the front page you can read:

Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications.

But browsing the quickstart section I bumped into some XAML… oho! I hope this is not what I’m thinking about, Webforms, XML, AJAX-Atlas-style.

Anyways, DLR is supposed to be released as an open-source project, plus being built on top of the CLR, it should run on Mono. That’s a great plus for all the dynamic languages to have a common base, so all annoying problems (performance, GC, Unicode) are solved once and we can focus on the real thing: the syntax.

Meanwhile, I’ll be checking the latest source of IronPython which is supposed to be built on top of this new thing.

4 Comments

Filed under C#, python, ruby

C# Generic headach

I recently converted some code to use Generic types in C# 2.0 and got the following error:

Cannot create an instance of ... because Type.ContainsGenericParameters is true

Turns out, you can’t dynamically call methods on a generic class. Well you can, but you kind of have to convert the generic type to enable late bound reflection.


type = instance.GetType().MakeGenericType(instance.GetType().GetGenericArguments());
return type.GetProperty(property).GetValue(instance, null);

via (GA)

Leave a comment

Filed under C#

Agile Database fun with the Migrator

Since I released the Database Migrator for .NET, a lot of people have asked me if I’ve used it on real/large/live projects and how was my experience. Like any tools, it can be good and … it can be bad. Here’s a couple of guidelines or what some would call “Good Practices” (note my voluntary abstinence over the incorrect “Best Practices” term).

Coming from hand-written SQL script to beautifully manage database migrations is a big step towards programming nirvana. But like the use of any revision control tools you ought to follow some guidelines.

Tip #1 : Test your migrations

Like any code in your application you must test your migrations. Ups and downs code. Do it part of your continuous build process and test it on as many different databases and environment as you can.

Tip #2 : Never edit a released migration

Once your application is released you should never alter it. Doing so would permit that two different database schema have the same version number, which is purely evil and can lead to programming darkness.
A good way to enforce this practice is to allow modifications to the migrations as long as they are on a feature branch or on the trunk and not on a release branch.
I guess one could code a Subversion pre-commit hook for this.

Tip #3 : Keep them short and sweet

Don’t create one migration for version 1.0 of your app. Split it in as many migrations as possible (be logical here). Adding a table ? 001_CreateUserTable, Removing a column ? 002_RemoveSomeColumn. This is simple SoC.
It also ease-up debugging when a migration fails and you only have 3 lines of code in there.

Tip #4 : Be migration-driven

Stop using any GUI to alter the database. Code it!
As a coder you should find that writing:


generate migration AddPostNoteColumn
edit db/migrations/004_AddPostNoteColumn.cs
...
Database.AddColumn("Post", "Note", typeof(string));
CTRL+S
nant migrate

is a lot faster then:

  1. Fire your database management app
  2. Connect to the database server
  3. Browse to the Post table
  4. Click in the menu to edit table schema
  5. Type : Note
  6. Select : varchar
  7. CTRL+S

.
Plus you have to write the migration code after anyway.

If you tough of some other tips let us know!

22 Comments

Filed under C#, Migrator

Ajax Scaffolding with Castle Generator

Dan Bunea created an generator for Ajax Scaffolding, check out the step by step instructions on his excellent blog, Becoming Agile.

Wow!

3 Comments

Filed under C#, Castle, Generator

Code generator : the languages comparison

I’ve been hacking a code generator in various languages just to get the fell of each languages, none is the worst, but some are so much cooler. Here’s the results of my experimentation.

C#

Goods

  • Fast
  • Should run on any win32 machine with .NET

Bads

  • Too much of LOC
  • Had to use NVelocity has a template engine
  • Code to inject template bindings was ugly

Python

Goods

  • Easy to read class and attributes metadata
  • Scripting language so the developement cycle was faster, no compile step
  • Easely multiplateforme

Bads

  • Had to use Cheetah has the template engine

Boo

Goods

  • Fast
  • Implemented the generator code has a script in Boo

Bads

  • Playing with the compiler pipeline and stuff is complexe
  • lack of docs (but yeah Boo has not hit 1.0 yet)
  • No template engine usable for this with Boo’s syntax

Ruby

Goods

  • OMG!
  • Implemented in 2 LOC

    require 'erb'
    print ERB::new('mytemplate').run(binding)
    

Bads

  • Too easy 😉

Leave a comment

Filed under C#, Generator, python, ruby