Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dapr Component Specification Validation (+ Source Generators) #456

Open
FullStackChef opened this issue Feb 7, 2025 · 1 comment
Open
Labels

Comments

@FullStackChef
Copy link
Contributor

Related to an existing integration?

Yes

Existing integration

Hosting.Dapr

Overview

By leveraging source generators, we can create a strongly-typed, fluent builder interface that encapsulates the component metadata.

For example, rather than working with untyped dictionaries, the source generator would produce metadata classes (like RedisStateMetadata) with defined properties:

public class RedisStateMetadata
{
    public string RedisHost { get; set; }
    public int RedisPort { get; set; }
    public SecretRef RedisPassword { get; set; }
}
  • Catch Errors Early: Identify misconfigurations before the application even runs.
  • Enforce Consistency: Ensure that every component adheres to its expected schema.
  • Improve Developer Experience: Provide immediate feedback through compile-time warnings or errors, reducing runtime surprises.
  • The goal is to offer a robust, type-safe mechanism that validates the Dapr component specifications, thereby increasing reliability and reducing debugging time.

Usage example

1. Fluent Chaining

This approach uses individual fluent methods to configure each property:

var builder = new DaprComponentBuilder();

builder.AddDaprRedisStateStore("myDaprRedisStateStore")
    .WithHost("127.0.0.1")
    .WithPort(6379)
    .WithPassword(passwordParam);

Here, AddDaprRedisStateStore("myDaprRedisStateStore") initializes the configuration for a Redis state store with the provided name. The subsequent fluent methods (WithHost, WithPort, and WithPassword) configure the strongly typed RedisStateMetadata.


2. Lambda Configuration in the Method

This variant offers an overload that accepts an Action<RedisStateMetadata> for inline configuration:

var builder = new DaprComponentBuilder();

builder.AddDaprRedisStateStore("myDaprRedisStateStore", metadata =>
{
    metadata.RedisHost = "127.0.0.1";
    metadata.RedisPort = 6379;
    metadata.RedisPassword = passwordParam;
});

This API variant bundles the configuration into a single call, offering a compact way to set up the metadata.


3. WithMetadata Extension

The third approach separates the initial resource creation from metadata configuration using a dedicated WithMetadata method:

var builder = new DaprComponentBuilder();

builder.AddDaprRedisStateStore("myDaprRedisStateStore")
    .WithMetadata(metadata =>
    {
        metadata.RedisHost = "127.0.0.1";
        metadata.RedisPort = 6379;
        metadata.RedisPassword = passwordParam;
    });

In this scenario, AddDaprRedisStateStore first creates the state store resource, and then WithMetadata allows you to configure the associated metadata using a lambda expression. This approach maintains the separation of concerns while still providing a fluent and strongly typed configuration experience.

Breaking change?

Yes

Alternatives

Each of the apis would support the future generation of a WithReference API that can be used to provide a rich experience for configuring dapr components based on aspire managed resources

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

Copy link

github-actions bot commented Mar 1, 2025

We have noticed this issue has not been updated within 21 days. If there is no action on this issue in the next 14 days, we will automatically close it. You can use /stale-extend to extend the window.

@github-actions github-actions bot added the Stale label Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant