使用持久函数,如何从活动函数返回多个值 [英] Using Durable Functions, how to return multiple values from an Activity Functions

查看:75
本文介绍了使用持久函数,如何从活动函数返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是领域驱动设计的忠实拥护者和开发人员,并且一直试图将技术/体系结构映射到DDD领域。将微服务映射到有限的上下文提供了自然的结合。归根结底,我只想专注于业务逻辑并将其托管在正确的环境中。持久功能对我来说很有希望,因为如果需要的话,它需要解决跨微服务,Sagas或Process Manager的分布式事务问题。我已经使用NServicebus多年了,并且喜欢他们的Saga技术,但是想利用Durable Function业务流程来处理Saga逻辑。

我当前遇到的体系结构问题如下。我已将DDD应用程序服务映射到活动功能。因此,只需通过协调器点击正确的一组活动功能来运行其交易即可创建传奇。从概念上讲,建立回滚也是非常简单的,但是我遇到了实现问题。顺便说一句,每个活动功能都将委派给一个处理所有业务逻辑详细信息的库,然后将获取业务逻辑返回的事件列表,并将其返回给Orchestrator。据我研究,还没有办法从活动函数返回多态返回值。因此,如果我的业务逻辑生成了UpdateSucceeded或UpdateFailed事件,我该如何简单地将其从我的活动函数返回给业务协调器,以便业务协调器可以通过调用不同的回滚活动函数来采取纠正措施?

I’m a big Domain Driven Design fan and developer, and am always attempting to map technology/architecture into the world of DDD. Mapping microservices to bounded contexts provides for a natural marriage. At the end of the day, I just want to focus on my business logic and host that in the right environment. Durable Functions looked promising to me, in needing to solve the distributed transaction problem across microservices, Sagas or Process Manager if you will. I have been using NServicebus for years, and love their Saga technology, but would like to leverage Durable Function orchestrations for handling saga logic.   The architecture problem that I am currently having is the following. I have mapped the DDD Application Service to an Activity Function. A Saga would therefore be created simply with an Orchestrator tapping the right set of Activity Functions to run their transactions. Building out a rollback is pretty straightforward as well conceptually, but I am having an implementation problem. As an aside, each Activity Function will delegate to a library that handles all the business logic details, and will then take the list of events returned by the business logic and return that back to the Orchestrator. As far as I have researched, there is no way to return a polymorphic return value from an Activity Function. So if my business logic generates either a UpdateSucceeded or a UpdateFailed event, how would I simply return that from my Activity Function back to the Orchestrator so that the Orchestrator can take corrective action by calling distinct rollback Activity Functions?

推荐答案

您可以在持久功能中使用新的自定义序列化程序支持 2.1.0 即可。仍然需要对其进行记录,但是使用Azure Functions 依赖注入

You can use the new custom serializer support in Durable Functions 2.1.0 to do this. It still needs to be documented but basically it works like this, using Azure Functions dependency injection:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddSingleton<IMessageSerializerSettingsFactory, CustomMessageSerializer>();
    }
}

public class CustomMessageSerializer : IMessageSerializerSettingsFactory
{
    public JsonSerializerSettings CreateJsonSerializerSettings()
    {
        return new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
    }
}

现在,如果您有以下课程:

Now if you have classes like these:

public abstract class Thing
{
    public abstract string GetMessage();
}

public class Foo : Thing
{
    public override string GetMessage()
    {
        return "I'm a Foo";
    }
}

public class Bar : Thing
{
    public override string GetMessage()
    {
        return "I'm a Bar";
    }
}

您具有以下编排和活动功能:

And you have orchestration and activity functions like this:

[FunctionName(nameof(Orchestrator))]
public static async Task<List<string>> Orchestrator(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var outputs = new List<string>();

    outputs.Add((await context.CallActivityAsync<Thing>(nameof(GetThing), true)).GetMessage());
    outputs.Add((await context.CallActivityAsync<Thing>(nameof(GetThing), false)).GetMessage());

    // [ "I'm a Foo", "I'm a Bar" ]
    return outputs;
}

[FunctionName(nameof(GetThing))]
public static Thing GetThing([ActivityTrigger] bool isFoo, ILogger log)
    => isFoo ? (Thing)new Foo() : (Thing)new Bar();

以上自定义序列化程序适用于所有通过Durable Functions扩展序列化的内容。如果需要更多控制,可以创建更复杂的序列化器。

The above custom serializer applies to everything serialized by the Durable Functions extension. You can create a more complicated serializer if you want more control.

这篇关于使用持久函数,如何从活动函数返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆