简体   繁体   English

在服务器 memory 中更新 object 与从数据库中提取更新后的 object

[英]Updating an object in server memory vs pulling the updated object from the database

I have somewhat of an architectural/best practices question我有一些架构/最佳实践问题

I have a small object, like so我有一个小object,像这样

userObjectFactory(data) {
  const {
    id,
    lastActive,
  } = data;

  return Object.freeze({
    id,
    lastActive,
  });
}

Now I have to ways to update it in memory.现在我必须在 memory 中更新它。 I can either recreate the object with updated value, like so我可以使用更新的值重新创建 object,就像这样

if(saveToDb().passed === true) {
  const newUser = userObjectFactory(newData)
}

or, I can save to the database, and then pull the updated object.或者,我可以保存到数据库中,然后拉取更新后的 object。

It seems that both of these approaches will use the same amount of code, but the second one will make one more pull from the database, which can add up.似乎这两种方法都将使用相同数量的代码,但第二种方法会从数据库中多提取一个,这可以加起来。

What's the best solution here?这里最好的解决方案是什么?

And the second question is, if I stick with the first option will it be better to recreate the object using the factory?第二个问题是,如果我坚持第一个选项,使用工厂重新创建 object 会更好吗? Or to add a method to change a property?或者添加一个方法来改变一个属性? I'm a bit confused about the immutability principle我对不变性原则有点困惑

First off, if you can avoid writing to the database, absolutely take that choice.首先,如果您可以避免写入数据库,请绝对选择该选项。

Then, we could answer better if we had some context about what you're trying to protect against - whether it's just accidental changes in your own code or whether you're giving this object to other, outside code that you don't control?然后,如果我们有一些关于您要保护的内容的上下文,我们可以更好地回答 - 无论是您自己的代码中的意外更改,还是您将此 object 提供给您无法控制的其他外部代码?

If it's just your code, then you're probably overthinking things by trying to make the entire object immutable when you actually don't want everything in the object to be immutable.如果它只是您的代码,那么当您实际上不希望 object 中的所有内容都是不可变的时,您可能会通过尝试使整个 object 不可变来过度思考。 Some of the object is active state that can change.一些 object 是活动的 state 可以改变。

For that case, I would use Object.defineProperty() and set up the actual immutable properties as {writable: false, configurable: false} and leave the mutable properties as something you can directly write to and change.对于这种情况,我会使用Object.defineProperty()并将实际的不可变属性设置为{writable: false, configurable: false}并将可变属性保留为您可以直接写入和更改的内容。 You've described no reason to Object.freeze() the entire object when you actually intend to change some of the properties.当您实际打算更改某些属性时,您没有描述Object.freeze()整个 object 的理由。

If you're passing an object to outside code and you don't want them to be able to change your actual core object, then just pass them a copy of the object, not the original.如果您将 object 传递给外部代码,并且您不希望他们能够更改您的实际核心 object,那么只需将 object 的副本传递给他们,而不是原始代码。 Then, no matter what they do to it, it won't affect your code in any way.然后,无论他们对它做什么,它都不会以任何方式影响您的代码。 Then, you're not trying to manage some sort of hybrid object that is immutable to some code, but mutable to some code which is just a bit of a mess.然后,您不会尝试管理某种混合 object 对某些代码是不可变的,但对某些代码是可变的,这只是有点混乱。

And the second question is, if I stick with the first option will it be better to recreate the object using the factory?第二个问题是,如果我坚持第一个选项,使用工厂重新创建 object 会更好吗? Or to add a method to change a property?或者添加一个方法来改变一个属性? I'm a bit confused about the immutability principle.我对不变性原则有点困惑。

It's not clear exactly what you you're trying to achieve with the immutability principle here.目前尚不清楚您要通过此处的不变性原则来实现什么。 It sounds like perhaps you're trying to follow some sort of design principle about making things immutable whenever you can.听起来您可能正在尝试遵循某种设计原则,即尽可能使事物不可变。 There are certainly places for that, but like any design principle they should be applied when they make sense and not when they don't.当然有这样的地方,但就像任何设计原则一样,它们应该在有意义的时候应用,而不是在没有意义的时候应用。 If you have actual mutable state in that object, you probably don't want to put that state in an object that is immutable.如果您在 object 中有实际的可变 state,那么您可能不想将 state 放入不可变的 ZA8CFDE6331BD4B666ACZ 中。 So, either store the mutable state elsewhere or don't pretend the mutable object is immutable (when it's not).因此,要么将可变的 state 存储在其他地方,要么不要假装可变的 object 是不可变的(当它不是时)。 Just like if you have an array of 10,000 items and you wish to update one item in the array, you don't make the array immutable and then have to make an entirely new copy of the array just so you can modify one item in the array.就像如果您有一个包含 10,000 项的数组并且您希望更新数组中的一项,您不必使数组不可变,然后必须制作一个全新的数组副本,以便您可以修改数组中的一项大批。 No, you mutate the existing array and change the one element directly.不,您改变现有数组并直接更改一个元素。 It's a ton more efficient.它的效率要高得多。

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

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