WebService:我应使用内部静态类派生结果 [英] WebService: Should I use Static Class Within to Derive Results

查看:110
本文介绍了WebService:我应使用内部静态类派生结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在编写WebService来预测多层次营销公司的资格.

在了解到静态类比创建实例的类要快之后,我编写了代码,该代码用于导出资格数据并将其返回给客户端使用的非静态类的WebService方法以及静态方法.

最近,一位程序员朋友告诉我,使用静态类可能会导致性能问题,因为此Web服务会受到很多打击.

感谢您提供任何反馈意见.
谢谢!
:) Anne

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

推荐答案

关于主"服务类:尽管可以向该类添加static方法,但这些方法不能实现)的网络方法.该Web服务由其Interface定义,并且C#/VB不允许您使用静态方法来实现Interface方法,因此会出现编译错误.出于这个原因,主类本身不能是静态的,但是即使您能够做到这一点,作为服务类来赞美它也没有多大意义.但是,服务类可以调用静态类.根据您的方案和所使用的技术,您可能会考虑使用不同的实例化策略(例如,WCF具有单个实例,可能会有所帮助).

关于性能,除非您要进行大量的计算,否则执行服务调用所花费的时间很可能会淹没方法执行本身.无论如何,静态方法和实例之间并没有太大的区别,最重要的是确定成员是否为静态是否具有对等意义.如果您遇到性能问题,则切换到静态不太可能有很大帮助.
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.


除了Keith提到的以外,您还需要考虑一些事项.

针对特定目的进行正确的设计取决于您对数据模型的了解以及将对数据执行的操作.

没有什么可以阻止多个线程并行执行相同的静态方法,并且您几乎可以将非静态方法视为具有隐藏参数"this"的静态方法.例如,这就是在c ++中的工作方式–虚拟方法要复杂一些,因为它们需要通过特定于类的方法表进行一定程度的间接访问,但是类比仍然有用.

通常,一些关键数据应预先加载到应用程序中,而大多数数据则不然.访问和操作预加载的数据需要锁定-因为您的Web服务通常是多线程的-为并行请求提供服务的单独线程.通常使用单个 [ ReaderWriterLockSlim [<非递归中的href ="http://msdn.microsoft.com/zh-cn/library/system.threading.readerwriterlockslim.aspx" target ="_ blank" title ="New Window"> ^ ]模式.读取器/写入器锁允许多个并发线程同时读取数据,同时将所有其他线程锁定以进行写操作.

组织数据,选择锁定机制等的方式最终决定了您从设计中获得的性能水平.
如果将所有数据保存在数据库中,则使用与数据库的单独连接为每个请求重新加载数据,您可以模仿常规Web应用程序的行为.这通常会更容易设计,但是如果有人创建一个比您的应用程序好1000到1的类似应用程序,您就不会感到惊讶.通过了解您的数据模型,并利用设计中的知识来使用以下方法针对数据库创建查询将信息保存在内存中以简化查询,您有望能够做很多事情来提高应用程序的性能.

问候
Espen Harlinn
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


这篇关于WebService:我应使用内部静态类派生结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆