简体   繁体   English

申请状态在哪里?

[英]Where to put application state?

Where in the code do I best put object creation (stateful objects) and where not? 在代码中我最好把对象创建(有状态对象)和不在哪里? In what layers? 在什么层?

For example, I once put an object reference inside a Hibernate DAO class and I was told that this was incorrect because DAO classes are not supposed to have state. 例如,我曾经在Hibernate DAO类中放置了一个对象引用,并且我被告知这是不正确的,因为DAO类不应该具有状态。 State should be inside the 'service layer'. 州应该在'服务层'内。

I have been told that I should not create new objects on recurring method calls such as UpdateCart(). 有人告诉我,我不应该在重复的方法调用(如UpdateCart())上创建新对象。 Creation of objects is costly and should not be sitting in your code everywhere. 创建对象代价高昂,不应该随处可见代码。 It should be sitting in initialization type methods only. 它应该只在初始化类型方法中。 For example, if an e-commerce application needs a cart, put it in the session. 例如,如果电子商务应用程序需要购物车,请将其放入会话中。 If it needs some general main object, put it in the initialization code. 如果需要一些通用主对象,请将其放入初始化代码中。 Create it once there and let the rest of the application access its instance later. 在那里创建它,让应用程序的其余部分稍后访问它的实例。 Do not create this instance upon every call. 每次调用时都不要创建此实例。

I'm confused about this whole design principle. 我对这整个设计原则感到困惑。 The strangest thing I heard is 'an app is not supposed to have state. 我听到的最奇怪的事情是“应用程序不应该有状态。 State should be kept in the data layer where the database is'. 状态应保存在数据库所在的数据层中。 Really? 真? I'm quite new to these design concepts and I don't know where to look in order to get more educated on it. 我对这些设计概念很陌生,我不知道在哪里可以看到它以获得更多的教育。 GoF? GoF的? Design Patterns books? 设计模式书籍? The goal is to create qualitative code (ie to be used in the business). 目标是创建定性代码(即在业务中使用)。

Thanks 谢谢

What is good practice can vary base don the type of project it is. 什么是良好的做法可以根据项目的类型而有所不同。

For most projects, creating objects is not that expensive for the CPU. 对于大多数项目来说,创建对象对CPU来说并不昂贵。 A cost which is not always articulated well is the cost to the design. 不总是很好地表达的成本是设计的成本。 It appears your application has a design methodology where all the state and objects need to be managed in a controlled and centralised way. 您的应用程序似乎有一种设计方法,其中所有状态和对象都需要以受控和集中的方式进行管理。 This is often done to improve maintainability and simplify the design. 通常这样做是为了提高可维护性并简化设计。 I wouldn't assume you should just know what the design is unless it has been very clearly spelt out to you. 我不认为你应该只知道设计是什么,除非它已经非常清楚地告诉你。

I suspect they rest of the team are used to working a particular way and don't think they should have to document or teach you this methodology, just tell you when you got it "wrong". 我怀疑团队的其他人习惯于以某种特定方式工作,并且认为他们不应该记录或教你这种方法,只要告诉你什么时候“错误”。 This is not productive IMHO, but you have to deal with the situation you have and ask them questions when it comes to the placement of state or data structures. 这不是高效的恕我直言,但你必须处理你所拥有的情况,并在涉及状态或数据结构的位置时问他们问题。

'an app is not supposed to have state. '应用程序不应该有状态。 State should be kept in the data layer where the database is' 状态应保存在数据库所在的数据层中

There are designs where this is the norm, aptly called 'stateless architectures'. 有些设计是常态,恰当地称为“无状态架构”。 Whether every architecture should be stateless is of course doubtful and the very term is perhaps opens to debate as well. 每个建筑是否应该是无国籍的当然是值得怀疑的,而且这个术语也许也会引发争论。

Most "stateless" applications in fact do have state, but as the rule above states (no pun) this state is kept in one global place; 事实上,大多数“无国籍”的申请都有国家,但正如上面的规则所述(没有双关语),这种状态被保存在一个全球的地方; the database. 数据库。 As Peter mentions, the reasons for this might be maintainability and simplification, but it's also often heard that this is for scalability . 正如彼得所提到的,其原因可能是可维护性和简化性,但也经常听说这是为了scalability Without state appearing anywhere but in the database, it's thought to be easy to add additional front-end servers, processing servers, and what have you. 如果没有状态出现在数据库中的任何地方,那么可以很容易地添加额外的前端服务器,处理服务器以及你有什么。

While this has some merit indeed, I think we do have to make a distinction between temporary state and authoritative state. 虽然这确实有一些优点,但我认为我们必须区分临时状态和权威状态。

Temporary state can be something like the place you are in an ordering process and the details you have already entered. 临时状态可以是您在订购过程中所处的位置以及您已输入的详细信息。 In Java EE, you could keep this around in eg @ConversationScoped beans, or @Stateful beans. 在Java EE中,您可以将其保留在例如@ConversationScoped bean或@Stateful beans中。 This is thus state that you keep inside the web layer resp. 因此,这表示您保留在Web层内。 business layer. 业务层。

The advantages of this are ease of use, performance and unloading your single central database. 这样做的好处是易于使用,性能和卸载单个中央数据库。 Sure, you can store temporary state in your central database as well, but you probably want to keep this away from the regular, non-temporary data, which means some additional programming complexity is needed. 当然,您也可以将临时状态存储在中央数据库中,但是您可能希望将其与常规的非临时数据保持一致,这意味着需要一些额外的编程复杂性。 It's also typically much faster to retrieve data from the web layer, and it removes some load from the database. 从Web层检索数据通常要快得多,并且它会从数据库中删除一些负载。

In many systems there's only a single master database (a database accepting writes), so this single database might become a huge bottleneck in that setup. 在许多系统中,只有一个主数据库(一个接受写入的数据库),因此这个单一数据库可能成为该设置中的一个巨大瓶颈。

Depending on your actual architecture and setup, not keeping temporary state in the database -may- actually improve your ability to scale. 根据您的实际架构和设置, 不在数据库中保留临时状态 - 可能 - 实际上提高了您的扩展能力。

The disadvantages are that you do need your client to stick to the single server on which the temporary state is currently kept. 缺点是您确实需要您的客户端坚持当前保留临时状态的单个服务器。 This is typical called 'sticky sessions'. 这通常称为“粘性会话”。 If the one server where this client is interacting with fails or needs to be restarted or whatever, the client will loose this temporary data. 如果此客户端与之交互的一台服务器出现故障或需要重新启动或其他任何服务器,则客户端将丢失此临时数据。 There are some schemes like replicating state to all nodes in a cluster or to nearby nodes (buddy replication), but this is making things complicated again and may overload the network (if all nodes are constantly replicating to each other). 有一些方案,比如将状态复制到集群中的所有节点或附近的节点(伙伴复制),但这会使事情再次变得复杂并且可能使网络过载(如果所有节点不断地相互复制)。

Authoritative state means it represents shared data that is the sole source of information. 权威状态意味着它代表作为唯一信息来源的共享数据。 This kind of state is something we almost always like to have at a central location, but sometimes you'd see it being stored in eg a web node. 这种状态是我们几乎总是喜欢在中心位置的状态,但有时您会看到它存储在例如Web节点中。

For example, suppose we have a list of recent prices, and instead of persisting this to a central location we keep it on the web node where it was entered. 例如,假设我们有一个最近价格列表,而不是将其持久保存到中心位置,我们将其保存在输入它的Web节点上。 Now there's a concept of the "one and only" web node that has this information and other servers may start assuming there's only this "one and only" web node. 现在有一个“唯一的”Web节点的概念,它具有此信息,其他服务器可能开始假设只有这个“唯一的”Web节点。 Adding additional nodes is now impossible, since it breaks this assumption. 现在添加额外的节点是不可能的,因为它打破了这个假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM