zarusz / SlimMessageBus

Lightweight message bus interface for .NET (pub/sub and request-response) with transport plugins for popular message brokers.
Apache License 2.0
467 stars 78 forks source link

[Host.HybridMessageSerialzier] Resolve HybridMessageSerializer dependencies from IServiceProvider #239

Closed EtherZa closed 5 months ago

EtherZa commented 5 months ago

238 PR to allow for serializers to be added and then resolved by the hybrid message serializer.

By resolving the specialized serializers from the DI container, registration of those serializers can be performed via their included builders. This will then allow for the expected dependency resolution path to be followed for each of the specialized serializers ie. JsonMessageSerializer can use a supplied JsonSerializerOptions instance or resolve one from the DI container directly

Caveat: The HybridMessageSerializer must be registered last so that it is able to remove previous IMessageSerializer registrations in order to make itself the designated serializer.

    services
        .AddSlimMessageBus(mbb =>
        {
            mbb
                // add required serializers to the DI container first
                .AddAvroSerializer()
                .AddGoogleProtobufSerializer()
                .AddJsonSerializer()

                // Add hybrid serializer last so that it can update DI container and set itself as the default
                .AddHybridSerializer<JsonMessageSerializer>(
                    o => {

                        // Message1, Message2 => AvroSerializer
                        // Message3 => GoogleProtobufMessageSerializer
                        // all other messages by JsonMessageSerializer

                        o.Add<AvroSerializer>(typeof(Message1), typeof(Message2));
                        o.Add<GoogleProtobufMessageSerializer>(typeof(Message3));
                    })
                ...
        } 
EtherZa commented 5 months ago

Removed some syntactic sugar that SonarCloud wasn't a fan of.

EtherZa commented 5 months ago

@EtherZa have a look at a few minor things.

I am also wondering if there is a way we could not require hybrid to be configured as the last (while ensuring backward compatibility with the overall serializer configuration experience).

Looks good overall!

I have added a potential option that you may prefer in the issue.

sonarcloud[bot] commented 5 months ago

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

zarusz commented 5 months ago

@EtherZa thanks for your contribution looks good!

If you have a moment would be good to tweak these 4 sonar warnings (in another PR). Else I can look at them in a few days. Cheers.

EtherZa commented 5 months ago

@EtherZa thanks for your contribution looks good!

Fantastic!

If you have a moment would be good to tweak these 4 sonar warnings (in another PR). Else I can look at them in a few days. Cheers.

243 fixes the issues.

Thanks for the support/ideas in getting it across the line.