c# - 具有状态构造函数参数的 Akka .NET 中的依赖注入(inject)

标签 c# dependency-injection akka.net

在构造函数中使用类似字符串参数的东西会使依赖注入(inject)变得非常困惑。思考:

public class CurrencyActor
{
    public CurrencyActor(string currency, IRepository repository)
    {
...

还有其他问题(例如 this one)来解决这个依赖注入(inject)的特殊问题。这通常可以通过重新思考设计和重构来解决。

但是,如果拥有一个对象的多个版本,每个版本负责不同的数据(例如,每种货币都有一个 CurrencyActor),那会怎样呢?这在使用 Akka .NET 等参与者模型时非常正常,但即使在该域之外也是有意义的。

在传递所需的初始状态时,使用依赖注入(inject)创建这些多个实例的最佳方法是什么?

最佳答案

在构造函数中有依赖关系并不麻烦,这很常见。这没有错。

您可以在接受您的依赖项的 CurrencyActor 上创建默认 Prop 静态方法:

public static Props CreateProps(string currency, Irepository repo)
    {
        return Props.Create(() => new CurrrncyActor(currency, repo));
    }

然后创建任意数量的:

var usCurrency = system.ActorOf(CurrencyActor.CreateProps("US", someRepo), "US");
var swedishCurrency = system.ActorOf(CurrencyActor.CreateProps("SEK", someRepo), "SEK");

[更新]

关于在 Akka 中使用 DI 容器,这被列为否。人们在使用 akka.net 时犯的前 7 个错误中的 2 个

https://petabridge.com/blog/top-7-akkadotnet-stumbling-blocks/

Thus it’s considered to be a good practice for actors to manage their own dependencies, rather than delegate that work to a DI framework.

所以基本不做。如果必须,根据那篇文章,Autofac 是最佳选择

[更新 2]

如果你想动态创建同一个 Actor 的新实例但改变一些初始状态,那么你可以有一个主管来负责创建它们:

public class MatchesSupervisor : ReceiveActor
{
    List<IActorRef> _matches = new List<IActorRef>();
    public void MatchesSupervisor()
    {
        Receive<SomeCommandToStartANewMatch>(msg =>
        {
            // store the currently active matches somewhere, maybe on a FullTime message they would get removed?
            _matches.Add(Context.ActorOf(MatchActor.Create(msg.SomeMatchId)));
        }
    }
}

在上面的例子中,没有使用 DI 容器,如果每个 MatchActor 都需要其他东西,比如 IRepository,那么它会在创建时传递给 MatchesSupervisor,然后在创建时传递给每个 MatchActor创建。

这在某种程度上也取决于状态的来源,以及开始新 Match 的机制是什么——我只是假设其他 Actor 正在发送消息。

(我在 ipad 上打字,所以上面的代码可能无法真正编译,但希望你明白了,我也遗漏了 MatchActor 的实现,但它只是一个将一些值传递到其构造函数的 Actor )

希望这对您有所帮助!

关于c# - 具有状态构造函数参数的 Akka .NET 中的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295099/

相关文章:

c# - System.Refelection.Assembly.CreateInstance 在 .NET 4.0 中损坏

c# - 在 Visual Studio 中输出控制台应用程序而不是控制台

c# - BackgroundWorker 和异常

c# - 自定义控件,绑定(bind)到代码隐藏

.net - Ninject 运行时上下文绑定(bind)

delphi - MVP : other constructor's parameters than view and model?

android - Dagger 2 : Cannot resolve symbol for dagger component

c# - Akka.NET集群节点优雅关机

c# - 传递上下文信息

c# - 为什么这个 FSM 只在一次状态转换中成功?