Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Tuesday, January 12, 2010

Asp.Net Intrinsic properties

By default the System.Web.UI.Page exposes Session, Request, and Response properties.  However often you may want to access these properties outside of the Page context. The easiest way to do it is to use HttpContext.Current static property to obtain the current request’s HttpContext.  Thus, you can obtain a session variable like this:

HttpContext.Current.Session[“userid”].


Monday, May 25, 2009

Viewing temp table structure

For some reason I can never remember to properly retrieve connection specific temporary table’s structure, i.e. #tempCustomers. Due to the fact that the table resides in tempdb, this is the format required:

use tempdb
exec sp_help #tempCustomers
Highlight these two lines in Management studio and press F5!

Friday, May 8, 2009

Virtual Function Calls from Constructors/Finalizers … Bad Idea!

So I am skimming over this book, Framework Design Guidelines by Krzysztof Cwalina and Brad Abrams, when authors note to “avoid calling virtual members on an object inside its constructor”. At first a developer might not think about the danger but it does make sense with a quick example that authors provide.

Here is another one i cooked up:

public class Trans {
public abstract Log();
public Trans() {
Log(...);
}
}

public class SomeTrans:Trans {
private Logger logger;
public SomeTrans() {
logger = new Logger();
}
public override Log(...) {
if (logger == null)
Console.Write("Oops!");
else
logger.write(...);
}
}

Using the base class to execute common code that all derived classes will might seem like a good idea but here the Trans constructor calls the derived type’s Log method before SomeTrans constructor is called. Compiler is unable to detect this danger because binding happens at run-time so be very aware.

The rule of thumb is to never do that!

Also note, C++ behaves differently as it does not allow a virtual traversal because at base construction time the object type is Trans.

Tuesday, January 13, 2009

TypeMock Isolator 2.5 w/ VB.Net support

Programming Visual Basic applications?

Typemock have released a new version of their unit testing tool, Typemock Isolator 5.2.
This version includes a new friendly
VB.NET API which makes Isolator the best Isolation tool for unit testing A Visual Basic (VB) .NET application.

Isolator now allows unit testing in VB or C# for many ‘hard to test’ technologies such as SharePoint, ASP.NET MVC, partial support for Silverlight, WPF, LINQ, WF, Entity Framework, WCF unit testing and more.

Note that the first 25 bloggers who blog this text in their blog and tell us about it, will get a Free Full Isolator license (worth $139). If you post this in a VB.NET dedicated blog, you'll get a license automatically (even if more than 25 submit) during the first week of this announcement.

Go ahead, click the following link for more information on how to get your free license.

Thursday, December 18, 2008

C# Events and thread-safety


After reading “C# Programming Language”, which is more or less a dictionary of C# language features, I noticed that the spec claims thread-safety in event default accessors.

So code defined like this:

public event SomeEvent;
compiled into something roughly as this:
   1:  private EventHandler __SomeEvent;
   2:  public event SomeEvent {
   3:      add { lock(this) { __SomeEvent += value; }}
   4:      remove { lock(this) { __SomeEvent -= value; }}
   5:  }

Actually, add and remove keywords are translated as add_SomeEvent and remove_SomeEvent methods with [MethodImpl(MethodImplOptions.Synchronized)] attributes and thus equivalent to lock(this) shown above.

However, There is a problem here to achieve thread-safety. Using [MethodImpl(..)] is a bad practice b/c your code is trying to enter a monitor for this object instance. Thus, if another thread of execution obtains the monitor first, your subsequent call to “+=” or “-=” will block and will be hard to troubleshoot.

Instead consider this pattern where you implement custom accessors locking on a private object and give caller to invoke event safely through a method:

private object l_SomeEvent;
private EventHandler __SomeEvent;
public event SomeEvent {
add { lock(l_SomeEvent) { __SomeEvent += value; }}
remove { lock(l_SomeEvent) { __SomeEvent -= value; }}
}
public void OnSomeEvent(EventArgs e) {
EventHandler temp;
lock(l_SomeEvent) { temp = __SomeEvent; }
if (temp != null) { __SomeEvent(this,e); }
}

Friday, October 17, 2008

New Drop of Gallio 3.0.4 is out

New drop of Gallio is up and ready. It has a number of feature improvements, discussed at here. My main concern is R# 4.1 integration.
Download it now at here.