Skip to content

Commit

Permalink
Fix the tests and wrapper lines; broken as command processor and disp…
Browse files Browse the repository at this point in the history
…atcher don't use yet.
  • Loading branch information
iancooper committed Dec 1, 2023
1 parent 8eadfe9 commit 70675a2
Show file tree
Hide file tree
Showing 137 changed files with 2,347 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,14 @@ public static MessageMapperRegistry MessageMapperRegistry(IServiceProvider provi
{
var serviceCollectionMessageMapperRegistry = provider.GetService<ServiceCollectionMessageMapperRegistry>();

var messageMapperRegistry = new MessageMapperRegistry(new ServiceProviderMapperFactory(provider));
var messageMapperRegistry = new MessageMapperRegistry(
new ServiceProviderMapperFactory(provider),
null
);

foreach (var messageMapper in serviceCollectionMessageMapperRegistry)
{
messageMapperRegistry.Add(messageMapper.Key, messageMapper.Value);
messageMapperRegistry.Register(messageMapper.Key, messageMapper.Value);
}

return messageMapperRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public ServiceProviderTransformerFactory(IServiceProvider serviceProvider)
/// </summary>
/// <param name="transformerType">The type of transformer to create</param>
/// <returns></returns>
public IAmAMessageTransformAsync Create(Type transformerType)
public IAmAMessageTransform Create(Type transformerType)
{
return (IAmAMessageTransformAsync) _serviceProvider.GetService(transformerType);
return (IAmAMessageTransform) _serviceProvider.GetService(transformerType);
}

/// <summary>
/// If the transform was scoped as transient, we release it when the pipeline is finished
/// </summary>
/// <param name="transformer"></param>
public void Release(IAmAMessageTransformAsync transformer)
public void Release(IAmAMessageTransform transformer)
{
if (!_isTransient) return;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2022 Ian Cooper <[email protected]>
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. */

#endregion

using System;
using Microsoft.Extensions.DependencyInjection;

namespace Paramore.Brighter.Extensions.DependencyInjection
{
/// <summary>
/// A factory for creating transformers, backed by the .NET Service Collection
/// </summary>
public class ServiceProviderTransformerFactoryAsync : IAmAMessageTransformerFactoryAsync
{
private readonly IServiceProvider _serviceProvider;
private readonly bool _isTransient;

/// <summary>
/// Constructs a transformer factory
/// </summary>
/// <param name="serviceProvider">The IoC container we use to satisfy requests for transforms</param>
public ServiceProviderTransformerFactoryAsync(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
var options = serviceProvider.GetRequiredService<IBrighterOptions>();
if (options == null) _isTransient = false; else _isTransient = options.HandlerLifetime == ServiceLifetime.Transient;
}

/// <summary>
/// Creates a specific transformer on demand
/// </summary>
/// <param name="transformerType">The type of transformer to create</param>
/// <returns></returns>
public IAmAMessageTransformAsync Create(Type transformerType)
{
return (IAmAMessageTransformAsync) _serviceProvider.GetService(transformerType);
}

/// <summary>
/// If the transform was scoped as transient, we release it when the pipeline is finished
/// </summary>
/// <param name="transformer"></param>
public void Release(IAmAMessageTransformAsync transformer)
{
if (!_isTransient) return;

var disposal = transformer as IDisposable;
disposal?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ public Dispatcher Build(string hostName)
subscriberRegistry.Register<ConfigurationCommand, ConfigurationCommandHandler>();
subscriberRegistry.Register<HeartbeatRequest, HeartbeatRequestCommandHandler>();

var incomingMessageMapperRegistry = new MessageMapperRegistry(new ControlBusMessageMapperFactory());
var incomingMessageMapperRegistry = new MessageMapperRegistry(
new ControlBusMessageMapperFactory(), null
);
incomingMessageMapperRegistry.Register<ConfigurationCommand, ConfigurationCommandMessageMapper>();
incomingMessageMapperRegistry.Register<HeartbeatRequest, HeartbeatRequestCommandMessageMapper>();

var outgoingMessageMapperRegistry = new MessageMapperRegistry(new ControlBusMessageMapperFactory());
var outgoingMessageMapperRegistry = new MessageMapperRegistry(
new ControlBusMessageMapperFactory(), null
);
outgoingMessageMapperRegistry.Register<HeartbeatReply, HeartbeatReplyCommandMessageMapper>();

var producerRegistry = _producerRegistryFactory.Create();
Expand Down
2 changes: 1 addition & 1 deletion src/Paramore.Brighter.ServiceActivator/MessagePump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private TRequest TranslateMessage(Message message)

try
{
request = _unwrapPipeline.UnwrapAsync(message).GetAwaiter().GetResult();
request = _unwrapPipeline.Unwrap(message);
}
catch (ConfigurationException)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Paramore.Brighter/CommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,9 @@ public TResponse Call<T, TResponse>(T request, int timeOutInMilliseconds)
{
s_logger.LogDebug("Reply received from {ChannelName}", channelName);
//map to request is map to a response, but it is a request from consumer point of view. Confusing, but...
//TODO: this only handles a synchronous unwrap pipeline, we need to handle async too
var inUnwrapPipeline = _transformPipelineBuilder.BuildUnwrapPipeline<TResponse>();
response = inUnwrapPipeline.UnwrapAsync(responseMessage).GetAwaiter().GetResult();
response = inUnwrapPipeline.Unwrap(responseMessage);
Send(response);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Paramore.Brighter/ControlBusSenderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public class ControlBusSenderFactory : IAmAControlBusSenderFactory
public IAmAControlBusSender Create<T, TTransaction>(IAmAnOutbox outbox, IAmAProducerRegistry producerRegistry)
where T : Message
{
var mapper = new MessageMapperRegistry(new SimpleMessageMapperFactory((_) => new MonitorEventMessageMapper()));
var mapper = new MessageMapperRegistry(
new SimpleMessageMapperFactory((_) => new MonitorEventMessageMapper()),
null);
mapper.Register<MonitorEvent, MonitorEventMessageMapper>();

var busConfiguration = new ExternalBusConfiguration();
Expand Down
57 changes: 57 additions & 0 deletions src/Paramore.Brighter/IAmAMessageMapperAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>
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. */

#endregion

using System.Threading.Tasks;

namespace Paramore.Brighter
{
/// <summary>
/// Interface IAmAMessageMapperAsync
/// Map between a <see cref="Command"/> or an <see cref="Event"/> and a <see cref="Message"/>. You must implement this for each Command or Message you intend to send over
/// a <a href="http://parlab.eecs.berkeley.edu/wiki/_media/patterns/taskqueue.pdf">Task Queue</a>
/// </summary>
public interface IAmAMessageMapperAsync { }

/// <summary>
/// Interface IAmAMessageMapperAsync
/// Map between a <see cref="Command"/> or an <see cref="Event"/> and a <see cref="Message"/>. You must implement this for each Command or Message you intend to send over
/// a <a href="http://parlab.eecs.berkeley.edu/wiki/_media/patterns/taskqueue.pdf">Task Queue</a>
/// </summary>
/// <typeparam name="TRequest">The type of the t request.</typeparam>
public interface IAmAMessageMapperAsync<TRequest> : IAmAMessageMapperAsync where TRequest : class, IRequest
{
/// <summary>
/// Maps to message.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Message.</returns>
Task<Message> MapToMessage(TRequest request);
/// <summary>
/// Maps to request.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>TRequest.</returns>
Task<TRequest> MapToRequest(Message message);
}
}
46 changes: 46 additions & 0 deletions src/Paramore.Brighter/IAmAMessageMapperFactoryAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>
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. */

#endregion

using System;

namespace Paramore.Brighter
{
/// <summary>
/// Interface IAmAMessageMapperFactoryAsync
/// In order to use a <a href="http://parlab.eecs.berkeley.edu/wiki/_media/patterns/taskqueue.pdf">Task Queue</a> approach we require you to provide
/// a <see cref="IAmAMessageMapper"/> to map between <see cref="Command"/> or <see cref="Event"/> and a <see cref="Message"/> registered via <see cref="IAmAMessageMapperRegistry"/>
/// We then call the instance of the factory which the client provides to create instances of that <see cref="IAmAMessageMapper"/>. You will need to implement the
/// <see cref="IAmAMessageMapperFactory"/> to use the Task Queue approach, and provide the instance of your mapper on request. Typically you might use an IoC container
/// to implement this.
/// </summary>
public interface IAmAMessageMapperFactoryAsync
{
/// <summary>
/// Creates the specified message mapper type.
/// </summary>
/// <param name="messageMapperType">Type of the message mapper.</param>
/// <returns>IAmAMessageMapper.</returns>
IAmAMessageMapperAsync Create(Type messageMapperType);
}
}
10 changes: 10 additions & 0 deletions src/Paramore.Brighter/IAmAMessageMapperRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ THE SOFTWARE. */

#endregion

using System;

namespace Paramore.Brighter
{
/// <summary>
Expand All @@ -45,5 +47,13 @@ public interface IAmAMessageMapperRegistry
/// <typeparam name="TRequest">The type of the t request.</typeparam>
/// <typeparam name="TMessageMapper">The type of the t message mapper.</typeparam>
void Register<TRequest, TMessageMapper>() where TRequest : class, IRequest where TMessageMapper : class, IAmAMessageMapper<TRequest>;

/// <summary>
/// Registers this instance.
/// </summary>
/// <param name="request">The type of the request to map</param>
/// <param name="mapper">The type of the mapper for this request</param>
/// <exception cref="System.ArgumentException"></exception>
void Register(Type request, Type mapper);
}
}
59 changes: 59 additions & 0 deletions src/Paramore.Brighter/IAmAMessageMapperRegistryAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>
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. */

#endregion

using System;

namespace Paramore.Brighter
{
/// <summary>
/// Interface IAmAMessageMapperRegistryAsync
/// In order to use a <a href="http://parlab.eecs.berkeley.edu/wiki/_media/patterns/taskqueue.pdf">Task Queue</a> approach we require you to provide
/// a <see cref="IAmAMessageMapper"/> to map between <see cref="Command"/> or <see cref="Event"/> and a <see cref="Message"/>
/// registered via <see cref="IAmAMessageMapperRegistry"/>
/// The default implementation<see cref="MessageMapperRegistry"/> is suitable for most purposes and the interface is provided for testing
/// </summary>
public interface IAmAMessageMapperRegistryAsync
{
/// <summary>
/// Gets this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>IAmAMessageMapperAsync&lt;T&gt;.</returns>
IAmAMessageMapperAsync<T> GetAsync<T>() where T : class, IRequest;
/// <summary>
/// Registers this instance.
/// </summary>
/// <typeparam name="TRequest">The type of the t request.</typeparam>
/// <typeparam name="TMessageMapper">The type of the t message mapper.</typeparam>
void RegisterAsync<TRequest, TMessageMapper>() where TRequest : class, IRequest where TMessageMapper : class, IAmAMessageMapperAsync<TRequest>;

/// <summary>
/// Registers this instance.
/// </summary>
/// <param name="request">The type of the request to map</param>
/// <param name="mapper">The type of the mapper for this request</param>
/// <exception cref="System.ArgumentException"></exception>
void RegisterAsync(Type request, Type mapper);
}
}
Loading

0 comments on commit 70675a2

Please sign in to comment.