February 11th, 2007 — 09:51 am
I had a view in my database (MS SQL 2005) with “Cast” statements.
At one point I tried modifying it, but this resulted in the following error:
“Cannot call methods on bigint“
After doing some research it turns out there was no problem with my query - the error came from the SQL 2005 management studio itself.
The fix was using T-SQL to modify the view instead of using the designer….
3 comments » | SQL Server
February 8th, 2007 — 11:46 am
Comment » | Useful .Net classes
February 8th, 2007 — 08:37 am
I have a class BaseFoo and several classes that inherite from it.
I needed to create instances of the descendants classes based on an integer value loaded from a database row.
The typical solution for this is a factory method pattern, but this pattern requires updating of the factory method every time I create a new descendant (’Foo’) class.
I wanted a way around this.
I needed a method which will extract all available classes from the assembly and will be able to locate the correct class according to the database row information and create a new instance.
At first I added a contant integer to each Foo class, but this meant using reflection to retrieve this constant, searching for the constant’s name and comparing it to a hard-coded string - meaning total meltdown the minute I changed the constant name or used obfuscation.
Instead I created a new Enum containing a range of integer values, this meaning I will search for a type, not a string. During run-time, first I retrieve all Foo classes to a memory dictionary containing also the unique Enum value of each class.
Then for each database row I lookup the integer value (from the database) in the dictionary and use reflection to call the class’s constructor.
private Dictionary FooClasses = new Dictionary();
//Create the memory table
private void BuildClassesMemoryTable()
{
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
for (int i=0; i < types.Length; i++)
{
if (types[i].BaseType == typeof(BaseFoo))
{
foreach (FieldInfo fi in types[i].GetFields())
{
if (fi.FieldType == typeof(FooEnum))
{
FooClasses.Add((int)fi.GetValue(null), types[i]);
break;
}
}
}
}
}
//Create a new instance of a Foo class
private BaseFoo CreateKnownClass()
{
//Create an array of parameter types according to the constructor’s signature to retrieve it
Type[] parameterTypes = new Type[2];
parameterTypes[0] = typeof(String);
parameterTypes[1] = typeof(int);
//Constructor’s parameters
Object[] parameters = new Object[parameterTypes.Length];
parameters[0] = “Hello”;
parameters[1] = 42;
return (BaseFoo)((Type)FooClasses[FooClass.ClassID]).GetConstructor(parameterTypes).Invoke(parameters);
}
private void LoadFromDatabase()
{
for (int i = 0; i < tables.fooDataSet.Rows.Count; i++)
{
//Some code to find the Foo class id from the database row…..
if (FooClasses.ContainsKey(fooClassID))
{
CreateKnownClass();
}
}
}
Comment » | Uncategorized
February 8th, 2007 — 06:30 am
One of the great thing about .Net is getting the callstack with minimum effort.
Unlike .Net, Delphi developers must rely on other methods to locate the location of an exception.
However, there is a way to get the call stack on Delphi exception.
You can download Jiri Hajek’s unit for this purpose, which also includes logging the exceptions to a file.
For this to work you also need to enable the “Stack frames” and “TD32 debug info” parameters in the project settings.
Here is something I wrote based on Jiri’s code, which directs the callstack output to another method, allowing actions other than file write:
unit ExceptionLog;
interface
uses Classes, MemCheck;
type
TOnException = procedure(CallStack : String) of object;
ExceptionHandler = class
private
FOnException: TOnException;
Function GetOnException : TOnException;
Procedure SetOnException(value: TOnException);
procedure Init;
Constructor Create;
public
Class Function GetInstance : ExceptionHandler;
Property OnException: TOnException read GetOnException write SetOnException;
end;
implementation
var
oldRTLUnwindProc : procedure; stdcall;
OInstance : ExceptionHandler;
{ ExceptionHandler }
constructor ExceptionHandler.Create;
begin
FOnException := nil;
end;
Class Function ExceptionHandler.GetInstance: ExceptionHandler;
begin
if OInstance = nil then OInstance := ExceptionHandler.Create;
result := OInstance;
end;
function ExceptionHandler.GetOnException: TOnException;
begin
Result := FOnException;
end;
procedure ExceptionHandler.SetOnException(value: TOnException);
begin
FOnException := value;
end;
procedure HandleOnException; stdcall;
var CS: TCallStack;
begin
FillCallStack(CS, 4);
Try
ExceptionHandler.GetInstance.OnException(CallStackTextualRepresentation(CS, ”));
Except
End;
asm
mov esp, ebp
pop ebp
jmp oldRTLUnwindProc
end; //asm
end; //HandleOnException
procedure ExceptionHandler.Init;
begin
oldRTLUnwindProc := RTLUnwindProc;
RTLUnwindProc := @HandleOnException;
end;
initialization
ExceptionHandler.GetInstance.Init;
end.
Usage:
- Add this unit to the “uses” section of your main unit
- Create a method with the “TOnException” (below) signature
- Connect the method to this class (Example: “ExceptionHandler.GetInstance.OnException := MyMethod“)
Comment » | Uncategorized
February 8th, 2007 — 06:25 am
Suppose you have a form containing a number of data adapters, each linked to few sql command classes.
You have a single sql connection class and you want to link all commands to this connection on runtime:
foreach (FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic & BindingFlags.Instance))
{
if (fi.FieldType == typeof(SqlCommand))
(fi.GetValue(this) as SqlCommand).Connection = sqlConnection1;
}
Comment » | Uncategorized
February 5th, 2007 — 05:40 am
I just read Oren Eini’s blog post about “Hack bombing”.
The basic notion is inserting time-dependant exceptions (set to trigger after a certain date) into the code instead of “To do” comments.
Oren is a great developer, constantly pushing himself to get better, but I think he took a step in the wrong direction in this case.
In a previous work place someone added an exception for his debugging, forgot about it, checked it into the source control and went home. Since he took the next day off, the rest of the development team had to put up with constantly getting a “bulbul” exception. (At one point a female developer declared “I got bulbul!” - this sounds very funny in Hebrew)
Whenever there is the slightest possible chance of your code leaving your personal computer, never insert UI meant for your personal use into it - in the worst possible scenario the customer may get a product containing this code.
If you may forget checking for “To do” comments, you may also forget running the section of the application you put the time bomb in before it reaches the QA.
And if you didn’t get to your “To do” comments because you were busy doing something more important, using a time bomb will just force you to break your important task, replacing the time bomb with a “To do” comment and going back to that task….
Update: Oren responded to my post, writing his objectives for creating hack bombs:
1) Putting the bomb there makes it much more likely that I will remember it in time.
2) Actually getting hit by the bomb means that I need to fix it now.
Making sure you remember something is easy. For example, in the agile methodology Scrum you add a backlog item. A simple alternative is to create a new task in your Outlook.
Regarding the second objective, the hack bombs method sounds to me like going into a diet by telling your dentist to seal your jaw shut instead of striving to change your eating habits.
It means you don’t trust yourself to make the right choice when the time comes.
Instead of forcing yourself to handle those task in advance, you should train yourself to record them and finding the time to address them voluntarily.
3 comments » | Programming
February 5th, 2007 — 05:37 am
The SRL team just released a new tool for using outlook to work with Team system.
Comment » | VSTS