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

Update versions and add event handler alias the whole way #76

Merged
merged 3 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Samples/Tutorials/GettingStarted/DishHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class DishHandler
{
public void Handle(DishPrepared @event, EventContext eventContext)
{
// throw new Exception();
Console.WriteLine($"{@event.Chef} has prepared {@event.Dish}. Yummm!");
}
}
Expand Down
17 changes: 13 additions & 4 deletions Source/Events.Handling/Builder/ConventionEventHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void BuildAndRegister(
CancellationToken cancellation)
{
logger.LogDebug("Building event handler from type {EventHandler}", EventHandlerType);
if (!TryGetEventHandlerInformation(out var eventHandlerId, out var partitioned, out var scopeId))
if (!TryGetEventHandlerInformation(out var eventHandlerId, out var partitioned, out var scopeId, out var alias, out var hasAlias))
{
logger.LogWarning("The event handler class {EventHandlerType} needs to be decorated with an [{EventHandlerAttribute}]", EventHandlerType, typeof(EventHandlerAttribute).Name);
}
Expand All @@ -89,7 +89,9 @@ protected void BuildAndRegister(
return;
}

var eventHandler = new EventHandler(eventHandlerId, scopeId, partitioned, eventTypesToMethods);
var eventHandler = hasAlias
? new EventHandler(eventHandlerId, alias, scopeId, partitioned, eventTypesToMethods)
: new EventHandler(eventHandlerId, nameof(EventHandlerType), scopeId, partitioned, eventTypesToMethods);
var eventHandlerProcessor = new EventHandlerProcessor(
eventHandler,
processingConverter,
Expand Down Expand Up @@ -316,17 +318,24 @@ bool ParametersAreOkay(MethodInfo method, ILogger logger)
return okay;
}

bool TryGetEventHandlerInformation(out EventHandlerId eventHandlerId, out bool partitioned, out ScopeId scopeId)
bool TryGetEventHandlerInformation(out EventHandlerId eventHandlerId, out bool partitioned, out ScopeId scopeId, out EventHandlerAlias alias, out bool hasAlias)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method TryGetEventHandlerInformation has 5 arguments (exceeds 4 allowed). Consider refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will consider this later, thanks :)

{
eventHandlerId = default;
partitioned = default;
scopeId = default;
alias = default;
hasAlias = default;
var eventHandler = EventHandlerType.GetCustomAttributes(typeof(EventHandlerAttribute), true).FirstOrDefault() as EventHandlerAttribute;
if (eventHandler == default) return false;
if (eventHandler == default)
{
return false;
}

eventHandlerId = eventHandler.Identifier;
partitioned = eventHandler.Partitioned;
scopeId = eventHandler.Scope;
alias = eventHandler.Alias;
hasAlias = eventHandler.HasAlias;
return true;
}

Expand Down
18 changes: 17 additions & 1 deletion Source/Events.Handling/Builder/EventHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class EventHandlerBuilder : ICanBuildAndRegisterAnEventHandler

ScopeId _scopeId = ScopeId.Default;

EventHandlerAlias _alias;
bool _hasAlias;
bool _partitioned = true;

/// <summary>
Expand Down Expand Up @@ -63,6 +65,18 @@ public EventHandlerBuilder InScope(ScopeId scopeId)
return this;
}

/// <summary>
/// Defines the event handler to have a specific <see cref="EventHandlerAlias" />.
/// </summary>
/// <param name="alias">The <see cref="EventHandlerAlias" />.</param>
/// <returns>The builder for continuation.</returns>
public EventHandlerBuilder WithAlias(EventHandlerAlias alias)
{
_alias = alias;
_hasAlias = true;
return this;
}

/// <inheritdoc/>
public void BuildAndRegister(
IEventProcessors eventProcessors,
Expand Down Expand Up @@ -93,7 +107,9 @@ public void BuildAndRegister(
return;
}

var eventHandler = new EventHandler(_eventHandlerId, _scopeId, _partitioned, eventTypesToMethods);
var eventHandler = _hasAlias
? new EventHandler(_eventHandlerId, _alias, _scopeId, _partitioned, eventTypesToMethods)
: new EventHandler(_eventHandlerId, _scopeId, _partitioned, eventTypesToMethods);
var eventHandlerProcessor = new EventHandlerProcessor(eventHandler, processingConverter, loggerFactory.CreateLogger<EventHandlerProcessor>());
eventProcessors.Register(
eventHandlerProcessor,
Expand Down
28 changes: 27 additions & 1 deletion Source/Events.Handling/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class EventHandler : IEventHandler
/// </summary>
/// <param name="identifier">The <see cref="EventHandlerId" />.</param>
/// <param name="scopeId">The <see cref="ScopeId" />.</param>
/// <param name="partitioned">The value indcating whether the <see cref="EventHandler" /> is partitioned.</param>
/// <param name="partitioned">The value indicating whether the <see cref="EventHandler" /> is partitioned.</param>
/// <param name="eventHandlerMethods">The event handler methods by <see cref="EventType" />.</param>
public EventHandler(
EventHandlerId identifier,
Expand All @@ -35,6 +35,26 @@ public EventHandler(
_eventHandlerMethods = eventHandlerMethods;
}

/// <summary>
/// Initializes a new instance of the <see cref="EventHandler"/> class.
/// </summary>
/// <param name="identifier">The <see cref="EventHandlerId" />.</param>
/// <param name="alias">The <see cref="EventHandlerAlias"/>.</param>
/// <param name="scopeId">The <see cref="ScopeId" />.</param>
/// <param name="partitioned">The value indicating whether the <see cref="EventHandler" /> is partitioned.</param>
/// <param name="eventHandlerMethods">The event handler methods by <see cref="EventType" />.</param>
public EventHandler(
EventHandlerId identifier,
EventHandlerAlias alias,
ScopeId scopeId,
bool partitioned,
IDictionary<EventType, IEventHandlerMethod> eventHandlerMethods)
: this(identifier, scopeId, partitioned, eventHandlerMethods)
{
Alias = alias;
HasAlias = true;
}

/// <inheritdoc/>
public EventHandlerId Identifier { get; }

Expand All @@ -47,6 +67,12 @@ public EventHandler(
/// <inheritdoc/>
public IEnumerable<EventType> HandledEvents => _eventHandlerMethods.Keys;

/// <inheritdoc />
public EventHandlerAlias Alias { get; }

/// <inheritdoc />
public bool HasAlias { get; }

/// <inheritdoc/>
public async Task Handle(object @event, EventType eventType, EventContext context, CancellationToken cancellation)
{
Expand Down
20 changes: 20 additions & 0 deletions Source/Events.Handling/EventHandlerAlias.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Dolittle.SDK.Concepts;

namespace Dolittle.SDK.Events.Handling
{
/// <summary>
/// Represents the concept of the alias for an Event Handler.
/// </summary>
public class EventHandlerAlias : ConceptAs<string>
{
/// <summary>
/// Implicitly converts from a <see cref="string"/> to an <see cref="EventHandlerAlias"/>.
/// </summary>
/// <param name="alias">The <see cref="string"/> representation.</param>
/// <returns>The converted <see cref="EventHandlerAlias"/>.</returns>
public static implicit operator EventHandlerAlias(string alias) => new EventHandlerAlias { Value = alias };
}
}
22 changes: 19 additions & 3 deletions Source/Events.Handling/EventHandlerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ public class EventHandlerAttribute : Attribute
/// <param name="eventHandlerId">The unique identifier of the event handler.</param>
/// <param name="partitioned">Whether the event handler is partitioned.</param>
/// <param name="inScope">The scope that the event handler handles events in.</param>
public EventHandlerAttribute(string eventHandlerId, bool partitioned = true, string inScope = default)
/// <param name="alias">The alias for the event handler.</param>
public EventHandlerAttribute(string eventHandlerId, bool partitioned = true, string inScope = default, string alias = default)
{
Identifier = Guid.Parse(eventHandlerId);
Partitioned = partitioned;
Scope = inScope == default ? ScopeId.Default : inScope;
Scope = inScope ?? ScopeId.Default;
if (alias != default)
{
Alias = alias;
HasAlias = true;
}
}

/// <summary>
Expand All @@ -32,11 +38,21 @@ public EventHandlerAttribute(string eventHandlerId, bool partitioned = true, str
/// <summary>
/// Gets a value indicating whether this event handler is partitioned.
/// </summary>
public bool Partitioned { get; }
public bool Partitioned { get; }

/// <summary>
/// Gets the <see cref="ScopeId" />.
/// </summary>
public ScopeId Scope { get; }

/// <summary>
/// Gets the <see cref="EventHandlerAlias"/>.
/// </summary>
public EventHandlerAlias Alias { get; }

/// <summary>
/// Gets a value indicating whether this event handler has an alias.
/// </summary>
public bool HasAlias { get; }
}
}
12 changes: 11 additions & 1 deletion Source/Events.Handling/IEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ public interface IEventHandler
/// </summary>
IEnumerable<EventType> HandledEvents { get; }

/// <summary>
/// Gets the alias of the event handler.
/// </summary>
EventHandlerAlias Alias { get; }

/// <summary>
/// Gets a value indicating whether the event handler has an alias or not.
/// </summary>
bool HasAlias { get; }

/// <summary>
/// Handle an event.
/// </summary>
/// <param name="event">The event to handle.</param>
/// <param name="eventType">The artifact representign the event type.</param>
/// <param name="eventType">The artifact representing the event type.</param>
/// <param name="context">The context in which the event is in.</param>
/// <param name="cancellation">The <see cref="CancellationToken" /> used to cancel the processing of the request.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous action.</returns>
Expand Down
5 changes: 5 additions & 0 deletions Source/Events.Handling/Internal/EventHandlerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public override EventHandlerRegistrationRequest RegistrationRequest
ScopeId = _eventHandler.ScopeId.ToProtobuf(),
Partitioned = _eventHandler.Partitioned
};
if (_eventHandler.HasAlias)
{
registrationRequest.Alias = _eventHandler.Alias.Value;
}

registrationRequest.EventTypes.AddRange(_eventHandler.HandledEvents.Select(_ => _.ToProtobuf()).ToArray());
return registrationRequest;
}
Expand Down
6 changes: 3 additions & 3 deletions versions.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project>
<PropertyGroup>
<ContractsVersion>6.0.0</ContractsVersion>
<ContractsVersion>6.1.0</ContractsVersion>
<MicrosoftExtensionsVersion>3.1.2</MicrosoftExtensionsVersion>
<RxVersion>4.4.1</RxVersion>
<ProtobufVersion>3.14.0</ProtobufVersion>
<GrpcVersion>2.35.0</GrpcVersion>
<ProtobufVersion>3.18.1</ProtobufVersion>
<GrpcVersion>2.41.0</GrpcVersion>
<NewtonsoftVersion>12.0.3</NewtonsoftVersion>
<SystemImmutableVersion>1.7.1</SystemImmutableVersion>
</PropertyGroup>
Expand Down