【发布时间】:2015-08-06 19:30:15
【问题描述】:
如何创建一个本身完全惰性的 opbjet?我有一个块,我想(作为依赖)传递块的“当前值”(在调用时)而不是依赖注入时的值。
我实际上不能传递 lambda,因为所有服务都需要一个实际的对象,所以它们不会向它们发送 :call,只是访问它们。
这个(过于简单化的)例子可以说明情况:
class Timer
def initialize(current_time)
@current_time = current_time
end
def print_current_time
print @current_time
end
end
class Injector
def current_time
# a lazy object that when accessed actually calls the lambda below
# every single time.
end
def current_time_lazy
-> { Time.now }
end
def instantiate(class_name)
# search for the class, look at the constructor and
# create an instance with the dependencies injected by
# name
# but to be simple
if class_name == "Timer"
Timer.new(current_time)
end
end
end
timer = Injector.new.instantiate("Timer")
timer.print_current_time # => some time
sleep 2
timer.print_current_time # => some *different* time
实际情况意味着传递current_user,但取决于当前用户在注入这些值后可能会更改的情况。
我非常感谢任何建议(即使现在我会仔细排序依赖注入代码,以免发生这种情况,但我认为它非常脆弱)
【问题讨论】:
-
我不明白你第二段中的“实际对象”,考虑到 lambdas 是对象。
-
@CarySwoveland 当然 lambda 是对象。我需要的是能够传递类似于承诺的东西。因此,获得该承诺/lambda/whatever 的服务将无法区分,但我不需要在注入时手头有对象。
标签: ruby lazy-evaluation