无状态与有状态
我对包含有关编程中无状态和有状态设计的一些具体信息的文章感兴趣。我很感兴趣,因为我想了解更多有关它的信息,但我真的找不到任何关于它的好文章。我在网上读过几十篇文章,这些文章模糊地讨论了这个主题,或者他们正在谈论 Web 服务器和会话 - 这也是关于有状态与无状态的,但我对编码中属性的无状态与有状态设计感兴趣。示例:我听说 BL 类在设计上是无状态的,实体类(或者至少我是这么称呼它们的 - 比如 Person(id, name, ..))是有状态的,等等。
我认为了解这一点很重要,因为我相信如果我能理解它,我就能写出更好的代码(例如考虑到粒度)。
无论如何,非常简短,以下是我对有状态与无状态的了解:
有状态(如 WinForms):存储数据以供进一步使用,但限制了应用程序的可扩展性,因为它受到 CPU 或内存限制
无状态(如 ASP.NET) - 尽管 ASP 试图通过 ViewStates 保持状态): 操作完成后,数据将被传输,并且实例将被移交回线程池(非晶态)。
正如您所看到的,它的信息非常模糊且有限(并且非常关注服务器交互),所以如果您能为我提供一些更有趣的信息,我将非常感激:)
I'm interested in articles which have some concrete information about stateless and stateful design in programming. I'm interested because I want to learn more about it, but I really can't find any good articles about it. I've read dozens of articles on the web which vaguely discuss the subject, or they're talking about web servers and sessions - which are also 'bout stateful vs stateless, but I'm interested in stateless vs stateful design of attributes in coding. Example: I've heard that BL-classes are stateless by design, entity classes (or at least that's what I call them - like Person(id, name, ..)) are stateful, etc.
I think it's important to know because I believe if I can understand it, I can write better code (e.g. granularity in mind).
Anyways, really short, here's what I know 'bout stateful vs stateless:
Stateful (like WinForms): Stores the data for further use, but limits the scalability of an application, because it's limited by CPU or memory limits
Stateless (Like ASP.NET - although ASP tries to be stateful with ViewStates):
After actions are completed, the data gets transferred, and the instance gets handed back to the thread pool (Amorphous).
As you can see, it's pretty vague and limited information (and quite focussed on server interaction), so I'd be really grateful if you could provide me with some more tasty bits of information :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
无状态意味着没有过去的记忆。每笔交易的执行就像第一次完成一样。
有状态意味着有过去的记忆。以前的交易会被记住,并且可能会影响当前的交易。
无状态:
有状态:
参考: https://softwareengineering.stackexchange.com/questions/101337/whats-the-difference- Between-stateful-and-stateless
Stateless means there is no memory of the past. Every transaction is performed as if it were being done for the very first time.
Stateful means that there is memory of the past. Previous transactions are remembered and may affect the current transaction.
Stateless:
Stateful:
Refer from: https://softwareengineering.stackexchange.com/questions/101337/whats-the-difference-between-stateful-and-stateless
有状态应用程序存储自开始运行以来所发生或发生的变化的信息。任何有关它处于什么“模式”,或者已经处理了多少记录,或者其他什么的公共信息,都会使其成为有状态的。
无状态应用程序不会公开任何此类信息。它们每次对相同的请求、函数或方法调用给出相同的响应。 HTTP 的原始形式是无状态的 - 如果您对特定 URL 执行 GET,您每次都会得到(理论上)相同的响应。当然,例外情况是当我们开始在顶部添加状态性时,例如使用 ASP.NET Web 应用程序:) 但如果您想到仅包含 HTML 文件和图像的静态网站,您就会明白我的意思。
A stateful app is one that stores information about what has happened or changed since it started running. Any public info about what "mode" it is in, or how many records is has processed, or whatever, makes it stateful.
Stateless apps don't expose any of that information. They give the same response to the same request, function or method call, every time. HTTP is stateless in its raw form - if you do a GET to a particular URL, you get (theoretically) the same response every time. The exception of course is when we start adding statefulness on top, e.g. with ASP.NET web apps :) But if you think of a static website with only HTML files and images, you'll know what I mean.
我建议您从 StackOverflow 中讨论无状态编程优点的问题开始。这更多是在函数式编程的背景下,但您将阅读的内容也适用于其他编程范例。
无状态编程与函数的数学概念相关,当使用相同的参数调用时,总是返回相同的结果。这是函数式编程范式的一个关键概念,我希望您能够找到该领域的许多相关文章。
您可以研究以获得更多了解的另一个领域是 RESTful Web 服务。与其他试图以某种方式保持状态的网络技术相比,这些技术在设计上是“无状态的”。 (事实上,您说 ASP.NET 是无状态的说法是不正确的 - ASP.NET 努力使用 ViewState 来保持状态,并且肯定会被描述为有状态。另一方面,ASP.NET MVC 是一种无状态技术)。有很多地方讨论 RESTful Web 服务的“无状态”(例如 this 博客点),但您可以再次从 SO 问题。
I suggest that you start from a question in StackOverflow that discusses the advantages of stateless programming. This is more in the context of functional programming, but what you will read also applies in other programming paradigms.
Stateless programming is related to the mathematical notion of a function, which when called with the same arguments, always return the same results. This is a key concept of the functional programming paradigm and I expect that you will be able to find many relevant articles in that area.
Another area that you could research in order to gain more understanding is RESTful web services. These are by design "stateless", in contrast to other web technologies that try to somehow keep state. (In fact what you say that ASP.NET is stateless isn't correct - ASP.NET tries hard to keep state using ViewState and are definitely to be characterized as stateful. ASP.NET MVC on the other hand is a stateless technology). There are many places that discuss "statelessness" of RESTful web services (like this blog spot), but you could again start from an SO question.
形容词“有状态”或“无状态”仅指会话的状态,它与为相同输入提供相同输出的功能概念无关。如果是这样,任何动态 Web 应用程序(背后有数据库)都将是有状态服务,这显然是错误的。
考虑到这一点,如果我委托任务在底层技术(例如 coockie 或 http 会话)中保持会话状态,我将实现有状态服务,但如果所有必要的信息(上下文)都作为参数传递,我' m 实现无状态服务。
应该注意的是,即使传递的参数是会话状态的“标识符”(例如票证或会话 ID),我们仍然在无状态服务下操作,因为会话是无状态的(票证在客户端和会话之间不断传递)。服务器),并且这两个端点可以说是“有状态的”。
The adjective Stateful or Stateless refers only to the state of the conversation, it is not in connection with the concept of function which provides the same output for the same input. If so any dynamic web application (with a database behind it) would be a stateful service, which is obviously false.
With this in mind if I entrust the task to keep conversational state in the underlying technology (such as a coockie or http session) I'm implementing a stateful service, but if all the necessary information (the context) are passed as parameters I'm implementing a stateless service.
It should be noted that even if the passed parameter is an "identifier" of the conversational state (e.g. a ticket or a sessionId) we are still operating under a stateless service, because the conversation is stateless (the ticket is continually passed between client and server), and are the two endpoints to be, so to speak, "stateful".
从一个帐户在线转移到另一个帐户的资金是有状态的,因为接收帐户具有有关发送者的信息。
将现金从一个人交给另一个人,这种交易是无状态的,因为收到现金后,赠送者的身份并不与现金一起存在。
Money transfered online form one account to another account is stateful, because the receving account has information about the sender.
Handing over cash from a person to another person, this transaction is statless, because after cash is recived the identity of the giver is not there with the cash.
只是添加其他人的贡献...另一种方法是从 Web 服务器和并发的角度来看它...
HTTP 本质上是无状态的,这是有原因的...在 Web 服务器的情况下,< strong>有状态意味着它必须记住用户上次连接的“状态”,和/或保持与请求者的开放连接。在具有数千个并发连接的应用程序中,这将非常昂贵且“压力很大”...
在这种情况下,无状态可以明显有效地利用资源...即在单个实例中支持连接请求和响应...没有保持连接打开和/或记住上次请求中的任何内容的开销...
Just to add on others' contributions....Another way is look at it from a web server and concurrency's point of view...
HTTP is stateless in nature for a reason...In the case of a web server, being stateful means that it would have to remember a user's 'state' for their last connection, and /or keep an open connection to a requester. That would be very expensive and 'stressful' in an application with thousands of concurrent connections...
Being stateless in this case has obvious efficient usage of resources...i.e support a connection in in a single instance of request and response...No overhead of keeping connections open and/or remember anything from the last request...
我们通过使用会话对象覆盖 HTTP 无状态行为来使 Web 应用程序有状态。当我们使用会话对象时,状态会被携带,但我们仍然只使用 HTTP。
We make Webapps statefull by overriding HTTP stateless behaviour by using session objects.When we use session objets state is carried but we still use HTTP only.
我对有状态和无状态类设计也有同样的疑问,并做了一些研究。刚刚完成,我的发现已发布在我的博客中
I had the same doubt about stateful v/s stateless class design and did some research. Just completed and my findings has been posted in my blog