Pierre Greborio.NET

Talking about .NET world

July 2003 - Posts

Going to vacation

Here we go. Today I'll leave for vacation until 5th of august. The Calabria's beach (south Italy) is waiting for me :-)

See you soon....

IsNumeric

To test if the string content is a valid integer we can use the VB function IsNumeric. C# doesn't provides such a function then we have to write our own helper function to do so. One of the most common approach I've seen on the newsgroups is the following code:

public static bool IsNumeric(string inputData)
{
 try
 {
  int.Parse(inputData);
  return true;
 }
 catch
 {
  return false;
 }
}

If the string isn't a valid integer I'll get a FormatException or OverflowException if it is valid but out of the integer domain. Another approach is to use Convert.ToInt32() instead of Parse. From a performance point of view the two solutions are pretty the same.

A very competitive way is to use Regular Expressions checking if the string data contains really only digits:

private static Regex _isNumber = new Regex(@"^\d+$");

public static bool IsNumeric(string inputData)
{
 Match m = _isNumber.Match(inputData);
 return m.Success;
}

In this case, the regular expression is compiled once and then the solution is, more or less, 4 times faster than the previous ones.

DIME with VBScript demo code
I recently posted a sample of consuming a DIME message from VBScript code. Here I posted the (very) simple source code - web service and VBScript.
Strongly Typed Collection Generator for Visual Studio .NET 2003 upgrate

I upgrated to Visual Studio .NET 2003 my Add-In for generating stronly typed collections both in C# and VB.NET (download).

Comparing to the past version (for Visual Studio .NET) I fixed a bug (now it works fine for VB.NET too) and changed the way of using it. Now you have a new "Tools" menu item called STC Generator which will invoke the configuration dialog box. Selecting the project and then the class you can generate the strongly typed collection for that class in the project's language (C# and VB.NET actually)



This add-in lack in documentation and I hope to provide it ASAP.

DISCLAIMER:
The sample is provided as is. Be sure that it is actually only a sample and does not in any sense conform to any coding guidelines and has not been proven to be a stable product! The code has not been reviewed by third parties or even been refactored for optimization - be sure that it is still much improvable.
The author cannot be made responsible for any damage or inconveniencies but is willed to accept any questions and comments to this sample.

DIME message with VBScript

To implement a client and a Web Services with .NET that uses attachements is pretty simple. WSE does a lot of stuff for us. But, if we want consume a web service with attachement from a VB or VBScript client we have to write more code.

First of all we have to download Microsoft SOAP Toolkit 3.0 which provides an object model for DIME also. Then we should have a WebMethod with attachements, such as the following simple sample:

[WebMethod]
public string GetLastImage()
{
  DimeAttachment attachment = new DimeAttachment("image/jpeg",
     TypeFormatEnum.MediaType, Server.MapPath(@"\images\Test.jpg"));
  HttpSoapContext.ResponseContext.Attachments.Add(attachment);
  return "Test.jpg";
}

On the client side we can then create a VBS file as following:

WScript.Echo ("-----Start------")
' Connect to the web service
EndPointURL = "
http://webstudy/WebServices/DimeSample.asmx";
Set connector = CreateObject("MSOSOAP.HttpConnector30")
connector.Property("EndPointURL") = EndPointURL
connector.Connect

' Define SOAP message
connector.Property("SoapAction") = "urn:pierregreborio-it/GetLastImage"
connector.BeginMessage

Set Serializer = CreateObject("MSOSOAP.SoapSerializer30")
Serializer.Init connector.InputStream

Serializer.StartEnvelope
Serializer.StartBody
Serializer.StartElement "GetLastImage", "urn:pierregreborio-it"
Serializer.EndElement
Serializer.EndBody
Serializer.EndEnvelope

connector.EndMessage

' Post the message and get the response with the DIME parser (this is a DIME message)
Set Reader = CreateObject("MSOSOAP.SoapReader30")
Set Parser = CreateObject("MSOSOAP.DimeParser30")
Reader.LoadWithParser Connector.OutputStream, Parser

If Not Reader.Fault Is Nothing Then
  WScript.Echo(Reader.FaultString.Text)
Else
  ' Get the image from the attachments 
  Set ImageAttach = Reader.Attachments.Item(0)
  FileName = "ReceivedImage.jpg"
  Set fso = CreateObject("Scripting.FileSystemObject")
  If (fso.FileExists(FileName)) Then
     fso.DeleteFile FileName
  End If
  ImageAttach.SaveToFile FileName
End If

WScript.Echo("-----Done------")

This sample doesn't contains all error controls but gives an idea how to get an attachment with SOAP Toolkit 3.0...hopefully.

Posted: Jul 09 2003, 05:36 PM by PierreG | with 5 comment(s)
Filed under:
Testing a Web Service

We have several ways to test a Web Service:

  • Using Internet Explorer (by default from Visual Studio .NET)
  • Create a client (CUI or GUI)
  • Create a Microsoft InfoPath document
  • Create a WS testing generic application

For the last point we could use the Christian's DynWSLib which resolves dynamically a Web Service and then build a rich client over it.

Posted: Jul 07 2003, 02:37 PM by PierreG | with 1 comment(s)
Filed under:
Lazy Initialization

One of the most critics about Object Orientation for real world projects I get during the speaks is: OOP isn't performant. Considering a very simple example where a class Customer contains a list of classes Order. For the best OO philosophy we should have three types: Customer representing a single customer entity, Order which is an order entity and an Orders class wich is a list of Order entities (strongly typed collection of Order types).

Normally, all objects are, in some way, mapped over a relational database and when the consumer wants some information about a customer, they are loaded from the database.

The standard .NET way (many devs adopt this one) is to use a DataSet with two DataTable with a relation. The OOP way should be to load all informations from the database (maybe using a ORM tool) and expose them through it's object model.

This approach can provide some performances drawback when we load all informations (for all entities) at the same time (object creation time), especially if we need only some info from the customer and not it's orders.
Another approach consits in loading the deep information (in this sample the Orders) only on demand, as the Lazy Initialization pattern suggests.

Considering the following pseudocode:

public class Customer {
  // all code here
  public Orders CustomerOrders;

  public void Load(int customerID) {
    // Load the customer from DB and also it's orders (ie. Orders.Load(customerID))
  }
}

Can be refactored to:

public class Customer {
  // all code here
  private Orders _customerOrders = null;

  public Orders CustomerOrders {
     get
    {
         if(_customerOrders == null)
           _customerOrders = Orders.Load(customerID);
         return _customerOrders;
    }
  }

  public void Load(int customerID) {
    // Load only the customer from DB 
  }
}

As we can see the orders are loaded from the database only if really needed. This pattern is applicable to all objects where the creation of some instance variables cost either in terms of time or memory and they may never be used. This pattern is used as basis of a large numer of aother patterns, such as Singleton.

The sample above isn't thread safe but I will talk about it another time. Stay tuned :-)

More Posts