Wednesday, July 9, 2008

PEX

Pex is Automated EXploration Testing. It is an intelligent assistant to the programmer and will help us to generate a prarameterized unit test.

very cool...

Thursday, June 12, 2008

Trie Algorithm for Autocomplete and Suggest word

Currently I try using Trie for Suggest and Autocomplete algorithm.

public List<string> Suggest2(string str)
{
List<String> result = new List<String>();
TrieNode curr = root;
foreach (char c in str.Trim().ToLower().ToCharArray())
{
curr = curr.GetChild(c);
if (curr == null)
return result;
}

Suggest2(curr, new StringBuilder(str), result);
return result;

}

public void Suggest2(TrieNode curr,StringBuilder sbTemp, List<string> result)
{
if (curr == null) return;
if (curr.IsEnd) result.Add(sbTemp.ToString());

foreach (char c in curr.Childs.Keys)
{
sbTemp.Append(c);
Suggest2(curr.Childs[c], sbTemp, result);
sbTemp.Length--;
}
}



Any Suggestion to improve this algorithm except changing String to StringBuilder ?

Friday, June 6, 2008

IDisposable Pattern in .NET

In some cases we also need to implement IDisposable pattern in our code.

For example, If you have unmanaged objects such as (File, Image, TCP Connection, etc) or you want to make sure that you have closed your connection / web service.

Here is the code

#region IDisposable Pattern
/// <summary>
///
Desctructor in VB.NET - Finalize
/// It will be called by Garbage Collector
/// or you may force GC by calling GC.Collect()
/// </summary>
~MyClass()
{
//only dispose unmanaged obj, for managed obj,
//it probably already been clear by GC,
//so we cannot called it again.
Dispose(false);
}
/// <summary>
///
Will be called when you use 'using'
/// keyword or implicitly called by you obj.Dispose()
/// </summary>
public void Dispose()
{
Dispose(true);
//tell GC not to run Desctrutor,
//because it has been called implicitly
GC.SuppressFinalize(this);
}

/// <summary>
///
Dispose Unmanaged and manage
/// </summary>
/// <param name="bDisposeManaged">
///
true - dispose unmanaged and managed code as well
/// fase - dispose only and unmanaged
/// </param>
protected virtual void Dispose(bool bDisposeManaged)
{
try
{
if (bDisposeManaged)
{
//TODO: dispose managed code
//Such as(Service, connection, etc)
if (_ServiceUser != null)
{
try
{
_ServiceUser.Dispose();
}
catch { }
}
}
//TODO: dispose unmanaged code
//such as (File, Bitmap / Image, TCP Connection, etc)
}
catch{}
}
#endregion

Friday, May 30, 2008

New Beta Sites

Check this out...
www.evernote.com
This web site has OCR Build in which can search on handwriting inside your image 
     image image
http://www.tripit.com/ - If you like traveling 

Thursday, May 29, 2008

Dynamic Data on ASP.NET 3.5 Extensions CTP

I recently try Dynamic Data on ASP.NET 3.5 Extensions CTP.

The most thing that impress me is an idea of Dynamic Control /Field.

<asp:DynamicControl runat="server" DataField="EmailAddress"/>



So you can just simple define Dynamic Field in your Grid View and it will rendered into particular template (Include Edit Template) based on data type of your value.




You can define your template to have validation control or even can you 3rd party control such as telerik.



Here is How you can tell dynamic control to render based on particular template or even BLOB/Image Type as well.




//this is a partial class to override the code which has been generated by DBML
[MetadataType(typeof(MyClassMetadata))]
public partial class MyClass
{
//TODO: You may put Validation when your data change or whatever...
}

//Here is your metadata which you will define rendering using UIHint
public class MyClassMetadata
{
//Date Type
[DisplayFormat(DataFormatString="{0:dd MMM yyyy}")]
public object StartDate {get; set;}

//Integer Type
[Range(0,10, ErrorMessage="Please enter valid quantity between 1-10")]
public object Quantity {get; set;}

//Text type using 3rd Party WSYWIG control
[UIHint("TextEditorTelerik")]
public object Description {get; set;}

//Blob - image type
[UIHint("DbImage")]
[ImageFormat(200,200)]
public object Picture {get; set;}
}



UIHint is where you define your template.


You can create whatever for your template name and just put "_Edit" for edit mode.


Just see the sample in under FieldTemplates of your Dynamic Data Project



Singleton Confusion

I heard some developers asked about this question "If you can create a static class, Why do we need to create a Singleton Class ?"

I think the answer is straight forward.

Singleton Pattern is a pattern that will make sure there is only 1 instance. on the other hand It will make sure that developers will not be able to another instance.

Static Class will share your instance through your application. But this static class will not make sure that it will only be 1 instance of your class. Other developer which don't know where you put your static instance will able to create the new instance... and It will be any duplication and waste of resources.

Here is very simple Singleton pattern.

public class MySingleton
{
//Your exclusive instance
private static MySingleton _Instance;

//this will make sure no one can create an instance of this class.
private SampleSingleton(){}

//this static constructor will be the only who can create an instance of this class
static MySingleton()
{
//TODO: you may preinit / validation code before create an instance
_Instance = new MySingleton();
}

public static MySingleton Instance
{
get{ return _Instance;}
}
}