Wednesday, 5 March 2008

Null object pattern

- Avoid using a null value to represent an error. An error is an error and it must be treated as such by throwing an exception.

- When returning a collection of objects, return an empty list if no results are obtained. The consumer method will not need to test if the returned collection is null or not. It will only need to verify that atleast one element is present in the collection to know that results were obtained.

- When returning results of a binary nature, initialize your binary variable to a default value. If you want to maintain 3 states (true, false, not set), use a class like the Nullable class of the .Net framework.

Remark: The use of any pattern depends on your problem context.

Thursday, 31 January 2008

Excluding data contracts from a WCF service proxy class

It may happen that you do not want to generate your data contract classes in the proxy class of your service. For example, you might wish to deploy these data contracts in a separate assembly so as to share them between your different services.

To generate a WCF service proxy by excluding data contracts use the svcutil.exe command. You can execute this command in a visual studio command window.

Syntax: svcutil /et:<Fully qualified name of your data contract class> <Service URL>

Note that et: is the short form for excludeType. The above command is the same as:

svcutil /excludeType:<Fully qualified name of your data contract class> <Service URL>

The successful execution of the above command generates, in your current directory:
1. A proxy class
2. A configuration file for the proxy.

Tuesday, 29 January 2008

Foreground versus background threads in .Net

At the end of a .Net application, all background threads are automatically aborted by the Common Language Runtime (CLR).

To set whether a thread is a background thread, use the IsBackground property of the Thread class.

While background threads end with your application, foreground threads continue to execute if not explicitly aborted/stopped. So it is up to you to ensure that all foreground threads of your application terminate/abort correctly.