Sunday, 31 May 2009

Webforms vs MVC (London .NET User Group)

100 people listened to Sebastien Lambla and Phil Wistanley at the Microsoft customer centre in Victoria. They talked for 2 hours without a break. Two hours is a long time but the formula they adopted was entertaining and fun. Seb was supporting MVC, Phil Webforms. They kept rotating at the mike, going through humorous slides, throwing jokes at each other and interacting with the audience. Seb:
  • Webforms has a tendency to hide HTML and actually generates a lot of goo.
  • MVC relies on you knowing HTML but once you learn it, things get pretty easy.
  • Webform's page lifecycle is complex
  • Webforms is for morons.
Phil:
  • MVC is too complicated,
  • Webforms has a lot of ready-made controls, lots of 3rd party vendors
  • Uses a familiar event model
  • MVC is for hippies.
To sum it up, Webforms is for building apps quickly (better suited for the financial world), MVC is good if you need a high level of quality and testability.

Thursday, 14 May 2009

C++/CLI Cheat Sheet

My C++/CLI pocket reference...
  • Declare a string

System::String^ myString = "";

  • Declare a null reference

System::String^ myString = nullptr;

  • Pass a string by reference to a method (the reference to the string will be modified, not the string itself since strings are immutable). Use %:

void MyMethod(System::String^% myString)

{

}

  • Declare an array of strings
    cli::array<System::String^>^ stringArray = gcnew cli::array<System::String^>{"Skype","Blogger"};
  • Declare a managed member inside a native class
class IAmNative
{
    gcroot<IAmManaged^ > managedMember;
 
public:
    IAmNative():managedMember(gcnew IAmManaged())
    {
 
    }
};

  • Declare a managed member that will self destroy
#include <msclr\auto_gcroot.h> 
 
class IAmStillNative
{
    msclr::auto_gcroot<IAmStillManaged^> managedMember; // Will be disposed when IAmStillNative is destroyed
 
public:
    IAmStillNative():managedMember(gcnew IAmStillManaged())
    {
    }
};
  • Convert a native STL string to a managed string
System::String^ ToManaged(const std::string& nativeString)
{
    return gcnew System::String(nativeString.c_str());
}
  • The same backwards:

std::string ToNative(System::String^ managedString)
{
   char* str = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(managedString).ToPointer();
 
   std::string result(str);
 
   System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)str);
   return result;
}
Things you can do
  • Instantiate managed types, call managed methods from native code provided it compiles with /CLR.
  • Instantiate native types, call native methods from managed code
  • Link to the native types of a mixed static lib (/clr)
  • Declare managed methods that have native types in their signature
Things you can't do
  • Link to the managed types of a mixed static lib
  • Link to managed types in a DLL if those managed types have methods with native types in their signature.