Click here to Skip to main content
15,884,948 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I am writing a WebService to forecast qualifications for a multi-level marketing company.

After having read that static classes are faster than those that create instances, I have written the code that derives the qualification data and gives it back to the not static class's WebService method that clients use, with static methods.

Recently a programmer friend has told me that using static classes may cause performance problems since there will be a lot of hits on this webservice.

Any feedback is appreciated.
Thanks!
:) Anne
Posted
Updated 16-May-11 8:01am
v2

With respect to the "main" service class: Although can add static methods to the class but these can't be implementation(s) of web methods. The web service is defined by its Interface and C#/VB won't allow you to implement Interface methods with statics, so you'll get a compile error. For this reason the main class itself can't be static, but even if you could it wouldn't make much sense as a service class likr this. The service class can however call static classes. Depending on your scenario and the technology you are using you might consider a different instancing strategy (WCF has a singleton that might help for example).

With respect to performance, unless you are doing something computationally heavy, in all likelihood the time taken to make the service call is going to swamp the method execution itself. There isn't that much difference between a static method and an instance one anyway, the most important this is to determine whether having the member as static makes symantic sense. If you are having performance problems, switching to static is unlikely to help a great deal.
 
Share this answer
 
Comments
Espen Harlinn 16-May-11 15:24pm    
Good points, my 5
Manfred Rudolf Bihy 16-May-11 15:42pm    
Agreed! 5+
Apart from what Keith mentions, there are a few things you can take into consideration.

Proper design for a particular purpose depends on leveraging your knowledge about the data model, and the operations that will be performed on the data.

There is nothing that prevents multiple threads executing the same static methods in parallel, and you can almost think of non-static methods as static methods with a hidden parameter: “this”. That’s how it works in c++ for instance – virtual methods are a bit more complicated as they require a level of indirection through a class specific table of methods, but the analogy is still useful.

Usually some key data merits preloading into the application, while most of the data does not. Access to, and manipulation of, preloaded data requires locking – as your web service will be usually be multithreaded – separate threads servicing parallel requests. Management those data items are often done using the singleton[^] pattern, where a single static property or method provides access to an instance of the management class ensuring one, and only one instance, of the management class. The instantiation process of the instance of the management class must be protected in a manner that prevents multiple threads from instantiating multiple instances of the management class.

When designing such a system I prefer to investigate the possibility of leveraging ReaderWriterLockSlim[^] in non-recursive mode. A reader/writer lock allows multiple concurrent threads to read data at the same time, while locking out all others for write operations.

The way you organize your data, choose locking mechanisms, and so on – is eventually what is going to determine the level of performance you will get out of your design.
If you keep all your data in a database, reloading it for each request – using a separate connection to the database – you have something that mimics the behavior of a regular web application. This will usually be easier to design, but don’t be surprised if somebody creates a similar application that outperforms your application by a 1000 to 1. By understanding your data model, and leveraging that knowledge in your design to create queries against the database using information kept in memory to simplify the queries, you will hopefully be able to do much to improve the performance of your application.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 16-May-11 15:43pm    
Nicely elaborated! 5+
Espen Harlinn 16-May-11 15:48pm    
Thank you, Manfred!
AnneThorne 16-May-11 18:55pm    
Thank you so much for your help!!!
:) Anne

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900