Skip to content

Use TryAdd when registering services + add EventStreamId ctor #72

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public static EventStoreOptionsBuilder UseCQRS(

configure?.Invoke(cqrsBuilder);

builder.Services.AddSingleton(typeof(IStateProjector<>), typeof(StateProjector<>));
builder.Services.AddSingleton(typeof(IStateWriter<>), typeof(StateWriter<>));
builder.Services.AddTransient(typeof(ICommandProcessor<>), typeof(CommandProcessor<>));
builder.Services.AddTransient<ICommandProcessorFactory, CommandProcessorFactory>();
builder.Services.AddTransient<ICommandHandlerFactory, CommandHandlerFactory>();
builder.Services.AddSingleton<ICommandTelemetry, CommandTelemetry>();
builder.Services.TryAddSingleton(typeof(IStateProjector<>), typeof(StateProjector<>));
builder.Services.TryAddSingleton(typeof(IStateWriter<>), typeof(StateWriter<>));
builder.Services.TryAddTransient(typeof(ICommandProcessor<>), typeof(CommandProcessor<>));
builder.Services.TryAddTransient<ICommandProcessorFactory, CommandProcessorFactory>();
builder.Services.TryAddTransient<ICommandHandlerFactory, CommandHandlerFactory>();
builder.Services.TryAddSingleton<ICommandTelemetry, CommandTelemetry>();

builder.Services.AddSingleton<IProjectionOptionsFactory, ProjectionOptionsFactory>();
builder.Services.TryAddSingleton<IProjectionOptionsFactory, ProjectionOptionsFactory>();

builder.Services.AddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>));
builder.Services.AddTransient<IProjectionFactory, DefaultProjectionFactory>();
builder.Services.TryAddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>));
builder.Services.TryAddTransient<IProjectionFactory, DefaultProjectionFactory>();

builder.Services.TryAddSingleton<IProjectionTelemetry, ProjectionTelemetry>();

Expand Down
12 changes: 11 additions & 1 deletion src/Atc.Cosmos.EventStore.Cqrs/EventStreamId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ public EventStreamId(params string[] parts)
Value = string.Join(PartSeperator, parts);
}

/// <summary>
/// Initializes a new instance of the <see cref="EventStreamId"/> class from an existing <see cref="EventStreamId"/>.
/// </summary>
/// <param name="existing">The existing <see cref="EventStreamId"/>.</param>
public EventStreamId(EventStreamId existing)
{
Parts = existing.Parts;
Value = existing.Value;
}

public IReadOnlyList<string> Parts { get; }

public string Value { get; }

public static implicit operator EventStreamId(StreamId id)
=> FromStreamId(id.Value);
=> FromStreamId(id);

public static EventStreamId FromStreamId(StreamId id)
=> new(
Expand Down
45 changes: 45 additions & 0 deletions test/Atc.Cosmos.EventStore.Cqrs.Tests/EventStreamIdTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using FluentAssertions;
using Xunit;

namespace Atc.Cosmos.EventStore.Cqrs.Commands.Tests;

public class EventStreamIdTests
{
[Fact]
public void Ctor_must_throw_when_no_arguments_are_provided()
{
Assert.Throws<ArgumentException>(() => new EventStreamId());
}

[Fact]
public void Value_property_must_return_joined_parts()
{
var id = new EventStreamId("foo", "bar");
id.Value.Should().Be("foo.bar");
}

[Fact]
public void Parts_property_must_return_parts()
{
var id = new EventStreamId("foo", "bar");
id.Parts.Should().BeEquivalentTo("foo", "bar");
}

[Fact]
public void EventStreamId_can_be_cloned_using_ctor()
{
var id = new EventStreamId("foo", "bar");
var clone = new EventStreamId(id);
id.Value.Should().Be(clone.Value);
}

[Fact]
public void EventStreamId_can_be_created_from_StreamId()
{
var streamId = new StreamId("foo.bar");
EventStreamId eventStreamId = streamId;
EventStreamId eventStreamId2 = EventStreamId.FromStreamId(streamId);
eventStreamId.Value.Should().Be("foo.bar");
eventStreamId2.Value.Should().Be("foo.bar");
}
}
Loading