Sunday, January 8, 2012

Beauty and the Beast

Days ago was navigating on web and met an old post from Ayende Rahien, the really cool challenge.
It was so cool, that decided to share it together with solution.

The basic idea is. Given the code provided in C#

public class Program
    private static void Main(string[] args)

    public static void CanThisHappen<T>() where T : class, new()
          var instnace = new T();
          Debug.Assert(instance!= null);

What we can do in order to break this code. ?
In practice what we can do in order to make Assert work ?
Or, what we can do in order to make:  instance==null ???

Wow, it seems impossible, but it is not.
And here is the name of the Beast: ProxyAttribute

In practice it's enough to

1. Define special Proxy
2. Override in that Proxy class CreateInstance method, where we will return a null.
3. Derive our class from ContextBoundObject
4. Bind our custom Proxy to that class

Better to see how it works.

Here is special proxy:

 public class MyProxyAttribute : ProxyAttribute
        public override MarshalByRefObject CreateInstance(Type serverType)
          //  return base.CreateInstance(serverType);
            return null;
 with an override of the CreateInstance object, where we return null.

Here we define MyFunnyType :

 public class CustomServer : ContextBoundObject
        public CustomServer()
            Console.WriteLine("CustomServer Base Class constructor called");
        public void HelloMethod(string str)
            Console.WriteLine("HelloMethod of Server is invoked with message : " + str);

which derived from ContextBoundObject.

And that is. ! In this way we can intercept new() call on our type (MyFunnyType) and not only ! We can change return type of the constructor ! So no more the constructed object will be return , but the null, as we defined.

Excellent explanation on ContextBoundObject is here.

Real Beauty! Truly Beast !

Things I wish I would known 
 The absolute awesome talk of Rob Johnson, one of creators of  Spring framework.

 Talk about business in IT.

 Enjoy :)


Sunday, December 4, 2011

Before or after ?

When declare variable in function: in the beginning of the function body, or  just before exact moment of use (if it's possible) ? 

The most natural choice we made is declare in the moment of use of the variable. So simple and seems trivial question turned out in a serious debate on StackOverflow, when I post that question after having a long discussion with colleague of mine: Scope of variables in C#.

Interesting thing that I found was, that from memory consumption perspective there is no any difference. As variables on function's local scope are pushed on stack by the way, will their be used or not.

The answer of Bruno Brant clearly shows what happens.

The sample code:
 static void Main(string[] args)
    var a = 3;
    if (a > 1){
        int b = 2;
        a += b;
    var c = 10;
    Console.WriteLine(a + c);

This is IL output of provided code:

.method private hidebysig static void  Main(string[] args) cil managed{
  // Code size       24 (0x18)
  .maxstack  2
  .locals init ([0] int32 a,     // Declare a
           [1] int32 b,          // Declare b
           [2] int32 c)          // Declare c
  IL_0000:  ldc.i4.3          
  IL_0001:  stloc.0            
  IL_0002:  ldloc.0            
  IL_0003:  ldc.i4.1          
  IL_0004:  ble.s      IL_000c
  IL_0006:  ldc.i4.2
  IL_0007:  stloc.1
  IL_0008:  ldloc.0
  IL_0009:  ldloc.1
  IL_000a:  add
  IL_000b:  stloc.0
  IL_000c:  ldc.i4.s   10
  IL_000e:  stloc.2
  IL_000f:  ldloc.0
  IL_0010:  ldloc.2
  IL_0011:  add
  IL_0012:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0017:  ret} // end of method Program::Main

It's clearly visible that "b" variable is pushed on stack even if it's in conditional "if" scope, so potentially could never be used during program run.

The architectural goodness of declaring the variable just in the moment of use  is clearly visualize the scope of use of that variable, and be sure that none outside of that scope is able to change it in any way.
Remark from Reed Copsey's post:
"In C#, it's better to place them at the scope where they are needed. This has a few benefits, including: 
1.   You reduce the risk of error from reusing a variable inappropriately, especially during long term maintenance
2.   You are keeping the variable constrained within that scope, which eases refactoring
The last point is, in my opinion, the most important. Good maintainability and testability rely on the abiltiy to keep your methods small - and keeping variables declared as they're used makes extracting a method much, much simpler. Not only do the tools become more effective, the resulting automatic refactorings tend to be simpler (as the variable isn't passed in by ref, etc)."
Thumbs up guys !

Saturday, December 3, 2011

SvnRadar, Subversion repository sniffer

There are times when working on distributed teams I need to be notified about someone's commit on Subversion repository. Sometime happens that I need to check if that commit is actually not a dangerous or is not risky to apply it to my working copy. 

 I've used different solutions, so far, but I found them not good enough for me. 

Svn Notifier slowed down the system and actually consumed too much processor power but also by integrating with TortoiseSVN offers power of basic operations. 

Commit Monitor, instead, is lightweight, offers easy preview of changes in repository but with chaotic, hard to use interface (in my opinion), and also I'm not able to execute an update from it.

So SvnRadar was actually born, with following ideas:

  • very simple and clean UI
  • low processor consumption
  • no 3rd party tools mandatory integration, except any Subversion client, which is mandatory. 
  • possibility to check actual changes on repository before downloading them
  • possibility to update repository directly from tool, by one click.
  • have a systray notification, so I can understand that something changed on repository
  • possibility to sniff more then one repository contemporary, and assign aliases to them. 

Simple and clean UI

   That is.

Low processor consumption
    Download and try it :)

No 3rd party tools mandatory integration except Subversion client

Like a subversion client I usually use Slik Svn, cause find it pretty reliable.
SvnRadar can be integrated with WinMerge to visualize a actual change made on repository. By the way, there is a built-in diff visualizer.

   Possibility to check actual changes on repository before downloading

Just hover the mouse over the change notified and popup will appear showing  who, revision number and comment. Want to see more? No problem. Expand the change and double click on the row.

Possibility to update repository directly from tool, by one click
Just use a button on bottom on every tab associated to any repository. 

Have a systray notification, so I can understand that something changed on repository

Possibility to sniff more then one repository contemporary, and assign aliases to them. 

Application is free for use and open source. Fill free to download a binaries from my Svn Radar  download section, or check out directly the code. 

App written in C# and WPF 4.0.