diff --git a/src/Atc.Cosmos.EventStore.Cqrs/DependencyInjection/EventStoreOptionsBuilderExtensions.cs b/src/Atc.Cosmos.EventStore.Cqrs/DependencyInjection/EventStoreOptionsBuilderExtensions.cs index a55fc06..01153f8 100644 --- a/src/Atc.Cosmos.EventStore.Cqrs/DependencyInjection/EventStoreOptionsBuilderExtensions.cs +++ b/src/Atc.Cosmos.EventStore.Cqrs/DependencyInjection/EventStoreOptionsBuilderExtensions.cs @@ -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(); - builder.Services.AddTransient(); - builder.Services.AddSingleton(); + builder.Services.TryAddSingleton(typeof(IStateProjector<>), typeof(StateProjector<>)); + builder.Services.TryAddSingleton(typeof(IStateWriter<>), typeof(StateWriter<>)); + builder.Services.TryAddTransient(typeof(ICommandProcessor<>), typeof(CommandProcessor<>)); + builder.Services.TryAddTransient(); + builder.Services.TryAddTransient(); + builder.Services.TryAddSingleton(); - builder.Services.AddSingleton(); + builder.Services.TryAddSingleton(); - builder.Services.AddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>)); - builder.Services.AddTransient(); + builder.Services.TryAddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>)); + builder.Services.TryAddTransient(); builder.Services.TryAddSingleton(); diff --git a/src/Atc.Cosmos.EventStore.Cqrs/EventStreamId.cs b/src/Atc.Cosmos.EventStore.Cqrs/EventStreamId.cs index 827f1d9..45a0a44 100644 --- a/src/Atc.Cosmos.EventStore.Cqrs/EventStreamId.cs +++ b/src/Atc.Cosmos.EventStore.Cqrs/EventStreamId.cs @@ -16,12 +16,22 @@ public EventStreamId(params string[] parts) Value = string.Join(PartSeperator, parts); } + /// + /// Initializes a new instance of the class from an existing . + /// + /// The existing . + public EventStreamId(EventStreamId existing) + { + Parts = existing.Parts; + Value = existing.Value; + } + public IReadOnlyList 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( diff --git a/test/Atc.Cosmos.EventStore.Cqrs.Tests/EventStreamIdTests.cs b/test/Atc.Cosmos.EventStore.Cqrs.Tests/EventStreamIdTests.cs new file mode 100644 index 0000000..94cb010 --- /dev/null +++ b/test/Atc.Cosmos.EventStore.Cqrs.Tests/EventStreamIdTests.cs @@ -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(() => 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"); + } +} \ No newline at end of file