Scala 函数具有以下链接方法:
fn1.andThen(fn2)
fn1.compose(fn2)
但是这个例子怎么写:
我有函数cleanUp()
,必须始终在最后一步调用它。
我还有很多其他功能,例如:
class Helper {
private[this] val umsHelper = new UmsHelper()
private[this] val user = umsHelper.createUser()
def cleanUp = ... // delete user/ and other entities
def prepareModel(model: TestModel) = {
// create model on behalf of the user
}
def commitModel() = {
// commit model on behalf of the user
}
}
一些外部代码可以使用如下代码:
val help = new Helper()
help.prepareModel()
help.commitModel()
// last step should be called implicitly cleanUp
如何以函数式方式编写此内容,链接将始终
最后一步隐式调用 cleanUp
函数?
注意:我将其视为 C++ 中析构函数的类似物。一些链接(不管这个链是如何完成的)fn1 andLater fn2 andLater fn3
必须作为最后一步调用cleanUp
(fn1 andLater fn2 andLater fn3 andLater cleanUp
)。直接编写 cleanUp
方法的错误是,很有可能有人会错过这一步,并且用户将被泄露(将保留在数据库中)
最佳答案
这是一个更高级的替代方案:
当您听到“上下文”和“步骤”时,您会直接想到一种功能模式:Monad。汇总您自己的 monad 实例可以简化用户端将有效步骤放在一起的过程,同时保证上下文将在这些步骤之后被清理。
在这里,我们将开发一个遵循该模式的“CleanableContext”构造。
我们的构造基于最简单的 monad,其唯一功能是保存一个值。我们将其称为Context
trait Context[A] { self =>
def flatMap[B](f:A => Context[B]): Context[B] = f(value)
def map[B](f:A => B): Context[B] = flatMap(f andThen ((b:B) => Context(b)))
def value: A
}
object Context {
def apply[T](x:T): Context[T] = new Context[T] { val value = x }
}
然后我们有一个CleanableContext
,它能够“自行清理”并提供一些“清理”功能:
trait CleanableContext[A] extends Context[A] {
override def flatMap[B](f:A => Context[B]): Context[B] = {
val res = super.flatMap(f)
cleanup
res
}
def cleanup: Unit
}
现在,我们有了一个能够生成可清理的 UserContext
的对象,该对象将负责管理用户的创建和销毁。
object UserContext {
def apply(x:UserManager): CleanableContext[User] = new CleanableContext[User] {
val value = x.createUser
def cleanup = x.deleteUser(value)
}
}
假设我们已经定义了模型和业务功能:
trait Model
trait TestModel extends Model
trait ValidatedModel extends Model
trait OpResult
object Ops {
def prepareModel(user: User, model: TestModel): Model = new Model {}
def validateModel(model: Model): ValidatedModel = new ValidatedModel {}
def commitModel(user: User, vmodel: ValidatedModel): OpResult = new OpResult {}
}
用法
有了可重复使用的机制,我们的用户就可以以简洁的方式表达我们的流程:
import Ops._
val ctxResult = for {
user <- UserContext(new UserManager{})
validatedModel <- Context(Ops.prepareModel(user, testModel)).map(Ops.validateModel)
commitResult <- Context(commitModel(user, validatedModel))
} yield commitResult
该过程的结果仍然被封装,并且可以使用 value
方法从 Context
中“取出”:
val result = ctxResult.value
请注意,我们需要将业务操作封装到 Context
中,以便在此单子(monad)组合中使用。另请注意,我们不需要手动创建或清理用于操作的用户。这已经为我们照顾好了。
此外,如果我们需要不止一种托管资源,则可以使用此方法通过将不同的上下文组合在一起来管理其他资源。
说到这里,我只是想提供另一个角度来解决这个问题。管道更加复杂,但它为用户通过组合创建安全流程奠定了坚实的基础。
关于scala - 以不同的方式链式运作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358462/