Skip to content

Commit

Permalink
Merge pull request #856 from eventflow/cleanup-release-prepare
Browse files Browse the repository at this point in the history
Prepare for alpha release - part 2
  • Loading branch information
rasmus authored Jun 11, 2021
2 parents 40f5f30 + 3dc1a77 commit 1e4976e
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 32 deletions.
66 changes: 50 additions & 16 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ Here is the general motivation for introducing breaking changes to EventFlow.

- The initial version of EventFlow had its own IoC and logger implementation,
but with the introduction of standardized `Microsoft.Extensions` packages,
many of these custom made implementations can be removed.
- Focus on LTS verison of .NET (Core) and removing support .NET Framework.
- Missspelled API
- Missing async/await on critical methods
- Removal of non-async methods
many of these custom made implementations can be removed
- Focus on LTS versions of .NET (Core) and removing support .NET Framework.
- Fix misssssspelled API
- Add obviously missing async/await on critical methods
- Remove non-async methods wrapper methods

## Data in event stores

Upgrading EventFlow should **never** break existing data in event stores, not even
between major versions. All data currently in event stores will work with 1.x
releases. However, it might not be possible to do a rollback from 1.x to 0.x.
releases. However, it _might_ not be possible to do a rollback from 1.x to 0.x.

## Recommended strategy for migrating 0.x to 1.x

Expand All @@ -37,31 +37,65 @@ of EventFlow from 0.x to 1.x.

## NuGet packages removed

With the move toward the standardized Microsoft extensions packages and removal
of support for .NET Framework, there are a few NuGet packages that will no
longer be supported.

- `EventFlow.Autofac`

Since EventFlow uses the new `Microsoft.Extensions.DependencyInjection` for
handling IoC, its possible to install the Autofac adapter package instead,
thus rendering the package obsolete.

- `EventFlow.DependencyInjection`

Since the standard dependency injection is now a first class citizen in the
core package, this package is no longer needed.

- `EventFlow.Owin`

OWIN support has been removed as ASP.NET Core is introduced.

- `EventFlow.Autofac` use native Autofac integration packages for Microsoft
dependency injection
- `EventFlow.DependencyInjection` now integrated into the core package
`Microsoft.Extensions.DependencyInjection`
- `EventFlow.Owin` switch to ASP.NET Core

## Initializing EventFlow

There are a few ways you can initialize EventFlow.
Starting 1.0, there are a few ways you can initialize EventFlow.


### Fluent as `IServiceCollection` extension

```csharp
var eventFlowOptions = EventFlowOptions.New()
serviceCollection.AddEventFlow(o =>
// Set up EventFlow here
);
```

### Traditionally passing a `IServiceCollection` reference

```csharp
var eventFlowOptions = EventFlowOptions.New(serviceCollection)

// Set up EventFlow here
```


### Let EventFlow create the `IServiceCollection`

Useful in small tests, but should be used in real production setups.

```csharp
serviceCollection.AddEventFlow(o =>
// ...
)
var eventFlowOptions = EventFlowOptions.New()

// Set up EventFlow here
```

Its basically a shorthand for

```csharp
var eventFlowOptions = EventFlowOptions.New(new ServiceCollection())
```


## Aligning with Microsoft extension packages

Several types have been removed from EventFlow in order to align
Expand Down
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ The following list key characteristics of each version as well as its related br
* `0.x` (API stable)

The current stable version of EventFlow and has been the version of EventFlow
for almost siz years. 0.x versions have .NET Framework support and limited
for almost six years. 0.x versions have .NET Framework support and limited
support to the Microsoft extension packages through extra NuGet packages.

Feature and bug fix releases will still be done while there's interest in
Expand Down Expand Up @@ -248,29 +248,30 @@ public async Task Example()
// We wire up EventFlow with all of our classes. Instead of adding events,
// commands, etc. explicitly, we could have used the the simpler
// AddDefaults(Assembly) instead.
using (var resolver = EventFlowOptions.New
.AddEvents(typeof(ExampleEvent))
.AddCommands(typeof(ExampleCommand))
.AddCommandHandlers(typeof(ExampleCommandHandler))
.UseInMemoryReadStoreFor<ExampleReadModel>()
.CreateResolver())
var serviceCollection = new ServiceCollection()
.AddLogging()
.AddEventFlow(o => o
.AddEvents(typeof(ExampleEvent))
.AddCommands(typeof(ExampleCommand))
.AddCommandHandlers(typeof(ExampleCommandHandler))
.UseInMemoryReadStoreFor<ExampleReadModel>());

using (var serviceProvider = serviceCollection.BuildServiceProvider())
{
// Create a new identity for our aggregate root
var exampleId = ExampleId.New;

// Resolve the command bus and use it to publish a command
var commandBus = resolver.Resolve<ICommandBus>();
var commandBus = serviceProvider.GetRequiredService<ICommandBus>();
await commandBus.PublishAsync(
new ExampleCommand(exampleId, 42), CancellationToken.None)
.ConfigureAwait(false);
new ExampleCommand(exampleId, 42), CancellationToken.None);

// Resolve the query handler and use the built-in query for fetching
// read models by identity to get our read model representing the
// state of our aggregate root
var queryProcessor = resolver.Resolve<IQueryProcessor>();
var queryProcessor = serviceProvider.GetRequiredService<IQueryProcessor>();
var exampleReadModel = await queryProcessor.ProcessAsync(
new ReadModelByIdQuery<ExampleReadModel>(exampleId), CancellationToken.None)
.ConfigureAwait(false);
new ReadModelByIdQuery<ExampleReadModel>(exampleId), CancellationToken.None);

// Verify that the read model has the expected magic number
exampleReadModel.MagicNumber.Should().Be(42);
Expand Down Expand Up @@ -354,7 +355,7 @@ public class ExampleCommandHandler
CancellationToken cancellationToken)
{
aggregate.SetMagicNumber(command.MagicNumber);
return Task.FromResult(0);
return Task.CompletedTask;;
}
}
```
Expand All @@ -366,11 +367,13 @@ public class ExampleReadModel : IReadModel,
{
public int MagicNumber { get; private set; }

public void Apply(
public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ExampleAggregate, ExampleId, ExampleEvent> domainEvent)
IDomainEvent<ExampleAggregate, ExampleId, ExampleEvent> domainEvent,
CancellationToken _cancellationToken
{
MagicNumber = domainEvent.AggregateEvent.MagicNumber;
return Task.CompletedTask;
}
}
```
Expand Down
164 changes: 164 additions & 0 deletions Source/EventFlow.Tests/ReadMeExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// The MIT License (MIT)
//
// Copyright (c) 2015-2021 Rasmus Mikkelsen
// Copyright (c) 2015-2021 eBay Software Foundation
// https://github.com/eventflow/EventFlow
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.Threading;
using System.Threading.Tasks;
using EventFlow.Aggregates;
using EventFlow.Commands;
using EventFlow.Core;
using EventFlow.Exceptions;
using EventFlow.Extensions;
using EventFlow.Queries;
using EventFlow.ReadStores;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace EventFlow.Tests
{
public class ReadMeExamples
{
[Test]
public async Task Example()
{
// We wire up EventFlow with all of our classes. Instead of adding events,
// commands, etc. explicitly, we could have used the the simpler
// AddDefaults(Assembly) instead.
var serviceCollection = new ServiceCollection()
.AddLogging()
.AddEventFlow(o => o
.AddEvents(typeof(ExampleEvent))
.AddCommands(typeof(ExampleCommand))
.AddCommandHandlers(typeof(ExampleCommandHandler))
.UseInMemoryReadStoreFor<ExampleReadModel>());

using (var serviceProvider = serviceCollection.BuildServiceProvider())
{
// Create a new identity for our aggregate root
var exampleId = ExampleId.New;

// Resolve the command bus and use it to publish a command
var commandBus = serviceProvider.GetRequiredService<ICommandBus>();
await commandBus.PublishAsync(
new ExampleCommand(exampleId, 42), CancellationToken.None);

// Resolve the query handler and use the built-in query for fetching
// read models by identity to get our read model representing the
// state of our aggregate root
var queryProcessor = serviceProvider.GetRequiredService<IQueryProcessor>();
var exampleReadModel = await queryProcessor.ProcessAsync(
new ReadModelByIdQuery<ExampleReadModel>(exampleId), CancellationToken.None);

// Verify that the read model has the expected magic number
exampleReadModel.MagicNumber.Should().Be(42);
}
}

// The aggregate root
public class ExampleAggregate : AggregateRoot<ExampleAggregate, ExampleId>,
IEmit<ExampleEvent>
{
private int? _magicNumber;

public ExampleAggregate(ExampleId id) : base(id) { }

// Method invoked by our command
public void SetMagicNumber(int magicNumber)
{
if (_magicNumber.HasValue)
throw DomainError.With("Magic number already set");

Emit(new ExampleEvent(magicNumber));
}

// We apply the event as part of the event sourcing system. EventFlow
// provides several different methods for doing this, e.g. state objects,
// the Apply method is merely the simplest
public void Apply(ExampleEvent aggregateEvent)
{
_magicNumber = aggregateEvent.MagicNumber;
}
}

// Represents the aggregate identity (ID)
public class ExampleId : Identity<ExampleId>
{
public ExampleId(string value) : base(value) { }
}

// A basic event containing some information
public class ExampleEvent : AggregateEvent<ExampleAggregate, ExampleId>
{
public ExampleEvent(int magicNumber)
{
MagicNumber = magicNumber;
}

public int MagicNumber { get; }
}

// Command for update magic number
public class ExampleCommand : Command<ExampleAggregate, ExampleId>
{
public ExampleCommand(
ExampleId aggregateId,
int magicNumber)
: base(aggregateId)
{
MagicNumber = magicNumber;
}

public int MagicNumber { get; }
}

// Command handler for our command
public class ExampleCommandHandler
: CommandHandler<ExampleAggregate, ExampleId, ExampleCommand>
{
public override Task ExecuteAsync(
ExampleAggregate aggregate,
ExampleCommand command,
CancellationToken cancellationToken)
{
aggregate.SetMagicNumber(command.MagicNumber);
return Task.CompletedTask;
}
}

// Read model for our aggregate
public class ExampleReadModel : IReadModel,
IAmReadModelFor<ExampleAggregate, ExampleId, ExampleEvent>
{
public int MagicNumber { get; private set; }

public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ExampleAggregate, ExampleId, ExampleEvent> domainEvent,
CancellationToken cancellationToken)
{
MagicNumber = domainEvent.AggregateEvent.MagicNumber;
return Task.CompletedTask;
}
}
}
}

0 comments on commit 1e4976e

Please sign in to comment.