What's hot ? (and I mean really ...) - scroll down for more
1).  Code Templating - advanced usage of delegates & generics: my slides & demos are available for download! CodeProject article is also available.

2).  My series "TDD in the eyes of a simpleminded" is in progress(including code!): preface, part1, part2, Q&A 1, Manual Stub .vs. Mock Stub

3).  TDD Workshop: SeeCompass v0.1 and v0.2 are out.
# Friday, May 19, 2006

In the new asp.net 2.0 web application model, every web page (.aspx) and it's code behind (the class that this page "Inherits" from, actually it's partial with it, but that's the property name) compiles into a single assembly. At least as a default behavior.

aspnet2_Compile_flow.gif

 

That means that the following relationship is completely valid in this compilation model:

Default.aspx (CodeFile=Default.aspx.cs, Inherits=MyNamesapce.MyDefaultPage)
   Default.aspx.cs (class name MyNamespace.MyDefaultPage)

Default2.aspx (CodeFile=Default2.aspx.cs, Inherits=MyNamesapce.MyDefaultPage) // Inherits: same as above
   Default2.aspx.cs (class name MyNamespace.MyDefaultPage// class name: same as above


This can be quite confusing though. While you're developing everything will work as expected as every .aspx & aspx.cs will compile to a different assembly so no collision will occur; So you think you're good to go...

The problems start to show up when you're trying to compile everything to a single assembly (production env.). Now you'll get an "aspnet_merge.exe" error which tells you that you can't combine (1) Default.aspx with (2) Default2.aspx and (3) MyNamespace.MyDefaultPage class. It makes sense, after all, those three are partial classes.

When can this happen ?

if you Copy-Paste an existing page & forget to change the name of the class in the CodeBehind and in the Inherits property.

Solution - How to check if you're really good to go:

I've written a small utility with Mario, my teammate, which will find this duplication and give you a little report telling ya if you have more than 1 aspx file which inherit from the same class. Running it on the example above will produce the following report:

aspnet2_inspector_util.gif

Now all I have to do is change the Default.aspx.cs class name and change the Inherits property name in Default.aspx.

How to use the "Duplication Inspector" utility ?

(1) Extract the zip file (down below).
(2) Open app.config and change the "WebDirectoryToInspect" as needed.
(3) Run the InheritsDuplicationInspector.exe and voila !

You can find the source files here:

InheritsDuplicationInspector.zip - 47 KB

Have fun.

Posted by Oren Ellenbogen 
19/05/2006 09:55, Israel time UTC+03:00,     Comments [1]  | 
# Thursday, May 18, 2006

I had so much fun lecturing yesterday before the C++/C# User Group about abstracting your code via delegates & anonymous methods. The number of people that showed up was really astonishing, at least to my expectations, as about 70 people gave me the honor to talk about something I really like so thank you guys !

I got some really good feedbacks(update: Royo, Ken and Shani dedicated a post on the matter) from the audience, one of them even told me at the end that my lecture was much better than one of the lectures at TechEd 2006 about Generics so I'm really happy that my words and demos made an *impact*. Still, I feel that I can do a lot better so I hope I'll have the pleasure to give more lectures on the near future.

Finally, I got to say a BIG thanks to a bunch of folks: Justin Angel helped me to put my lecture on video so all of you (Hebrew speakers) can hear & see the live presentation(Justin - you rock!), Roy Osherove helped me think about the context of my lecture and connected me to the C++\C# User Group Leaders - Shai & Moshe. I owe a big thanks for Shai Bar Lev & Moshe Raab for giving me the opportunity and making it all possible. And finally, I want to say thanks to my teammates at SQLink that really pushed me through.

You can download the presentation & demos from this links:

Code Templating presentation (ppt) - 1.8 MB

Code Templating Demos (zip) - 500 KB

Code Templating Database (zip) - 139 KB

    * You need to restore this database on Sql Server 2005 Express which comes free 
       with VS.NET 2005. Without the database the ISPARefactoring demos wouldn't work.


The video will be available in a few days so stay tuned.

btw - Barca won the Champions League cup! What a great evening!

Barca.gif

Posted by Oren Ellenbogen 
18/05/2006 12:50, Israel time UTC+03:00,     Comments [3]  | 
# Monday, May 15, 2006

I'm presenting before the C# group on the following Wednesday (17/05/2006, 17:30) and I thought to give you a heads-up. You can click here for more details about the "Agenda", including how to get there & where to park your keyboard.

I'll elaborate on a new way (aka "Code Templating") to refactor your existing code for better re-usage and present some cool tricks via delegates and anonymous methods so get ready for some * cool * code. For the finale, I'm planning some live refactoring on existing code so you really don't want to miss this one !

btw - if you aren't familiar with the terms delegate, event, anonymous method or Generics - this presentation is for you as well; I'll try to cover as much as I can in this session.

It will be great to see you all there.

Posted by Oren Ellenbogen 
15/05/2006 05:07, Israel time UTC+03:00,     Comments [0]  | 
# Sunday, May 14, 2006

Preface:

Let's say that you have Button control named myBtn and you want to handle its Click event:

myBtn.Click += new EventHandler(myBtn_Click);

static void myBtn_Click(object sender, EventArgs e) {
   // do something
}


Now, raise your hand if you ever attached the event handler to the event manually, meaning you wrote "new EventHandler(myBtn_Click);" or even specified the method signature "void myBtn_Click(object sender, EventArgs e)".
I don't know about you, but the way I work is by simply writing "myBtn.Click +=" and this magic yellow thingi appears:

autoCompleteEvent.gif

TAB-ing again will generate the method stub and I'm good to go.

Doing this yellow magic on delegates parameters:

We all(?) worked with List<T>. This nice class has some nice methods like ForEach, FindAll, Find etc which get delegate(Action<T>\Predicate<T>) as parameter. for example: 

List<int> numbers = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });

// Print the numbers via ForEach method
numbers.ForEach(
delegate(int i) {
   
Console.Write(i);
});

Would it be great if you can write something like:

numbers.ForEach(

And the yellow thingi will popup and by clicking TAB this code will be generated:

numbers.ForEach(delegate(int i) {
   //TODO
});

This means that this IDE feature should check the type of the delegate - the number and type (even generic type!) of parameters and the return type. This will prevent us from looking at MSDN or "Go To Definition" while working with delegates which are sent as parameters.

Time for you to make a difference:

what say you ? can this save you some time ? If so - leave a comment, I'll do my best to implement it or to forward this request to MS folks (as ladybug).

Posted by Oren Ellenbogen 
14/05/2006 12:27, Israel time UTC+03:00,     Comments [5]  | 
# Saturday, May 13, 2006

We got up at 07:05. It was hard as hell, but we had to put the effort for the team. After all, we came to hear some great lectures. No way in hell I'm gonna miss it... Oh, and there are many pictures to see, come on, I know you want to see this one !
Posted by Oren Ellenbogen 
13/05/2006 07:37, Israel time UTC+03:00,     Comments [3]  | 
# Friday, May 12, 2006

So it all began on Tuesday, Moty (CEO), Roee(CTO), Ken (Team Leader and a major guffy) and myself drove about 5 hours to Eilat...
Posted by Oren Ellenbogen 
12/05/2006 09:30, Israel time UTC+03:00,     Comments [0]  | 

Man, that was a good 3 days of great lectures, great people and one great great party ! I had so much fun, really, and I'll try to share with my enthusiasm during the following 2-3 posts. Prepare for plenty of reviews about the lectures I've went to and a big set of pictures.

I got a lot of inspiration from some of the lecturers at TechEd and I can't tell you how much I'm excited about my lecture this week, I've invested a tremendous amount of time in it so I hope it will turn out the way I'm want it to be. It will be a dream come true to lecture on one of the next TechEd, so I hope I'll have some chances to share with you my motivation and my passion about our world(programming rules!).

All in time, I guess.

OK - on to the next post !

Posted by Oren Ellenbogen 
12/05/2006 08:56, Israel time UTC+03:00,     Comments [0]  | 
# Monday, May 08, 2006

In the previous version of asp.net, v1.1, the CustomValidator did NOT run when the control(e.g TextBox) was empty. Possible solutions were to create your own control (something like RequiredCustomValidator : CustomValidator) or the change WebUIValidation.js file so it will actually validate the control even if it's empty. Now, in the new version, v2.0, you can simply set the ValidateEmptyText to true and you're done. Nice (and useful) !

Posted by Oren Ellenbogen 
08/05/2006 04:02, Israel time UTC+03:00,     Comments [2]  | 
# Friday, May 05, 2006

I know, this is kind of a childish post but heck, It's hilarious to hear someone tries to pronounce my name - kudos Chuck, you did it !!

Click here to download the podcast (btw - my "thing" starts at -5:38).

Thanks Chuck, I'll update my progress on this one so keep reading ;)

Posted by Oren Ellenbogen 
05/05/2006 07:19, Israel time UTC+03:00,     Comments [3]  | 

The using keyword works on objects that implement IDisposable interface (an interface with one member, the Dispose() method).
So a code like this:

using (SqlConnection conn = new SqlConnection("connection_string"))
{
    // use conn...
}

Is actually transformed into:

SqlConnection conn = null;
try
{
    conn = new SqlConnection("connection_string");
}
finally
{
    if (conn!=null)
        conn.Dispose();
}

The using keyword allows more elegant writing, nothing more actually.

The additional feature I would like to see is that in case of exception, a method in the implementor of IDisposable will be called, meaning the code should(?) be transformed into something like this:

SqlConnection conn = null;
try
{
    conn = new SqlConnection("connection_string");
}
catch(Exception err) // *
{
    HandleException(err);
}
finally
{
    if (conn!=null)
        conn.Dispose();
}


Of course, I would have to implement HandleException(Exception e) method as part of implementing IDisposable interface.

Why is this good for ??

Well, think about creating a transaction scope interface:

interface ITransactionScope : IDisposable
{
    void Commit();
    void Rollback();
    // ....
}

In this scenario, it would be great that if no exceptions were thrown, the transaction should Commit() by default,
but if any exception was thrown and not handled - do Rollback().

using (ITransactionScope trans = DbServices.StartTransactionScope())
{

    // delete 1

} // should do Commit()


using (ITransactionScope trans = DbServices.StartTransactionScope())
{
    
    // delete 1
    
    throw new ArgumentException("just to make the point clear");

} // should do Rollback()


HandleException(Exception e) will allow me to set a flag for Rollback on Dispose or to Rollback immediately; This can be quite handy.

Today I Rollback at the Dispose(if the transaction state is "uncommitted") and I force my programmers to call Commit() method directly.

* I know that catching Exception is a bad practice by default, but in those scenarios I would rather ask the "is\as" question(s) and handle my exception than wrapping using with try-catch blocks. After all, using should allow elegant code...

What do you think ?

update:
I've posted a ladybug at Microsoft Product Feedback, you can check it out (and vote!!!) here:

http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=f3c7e216-8790-4937-acc0-502848232a9d

Posted by Oren Ellenbogen 
05/05/2006 10:37, Israel time UTC+03:00,     Comments [2]  |