Created
May 25, 2011 22:25
-
-
Save trbngr/992134 to your computer and use it in GitHub Desktop.
Lokad.Cqrs.SimpleSynchronousMessageSender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Lokad.Cqrs.Build.Client; | |
namespace Lokad.Cqrs.Extensions.Build | |
{ | |
public static class ExtendCqrsClientBuilder | |
{ | |
public static CqrsClientBuilder AddSynchronousMessageSender(this CqrsClientBuilder builder) | |
{ | |
return AddSynchronousMessageSender(builder, config => { }); | |
} | |
public static CqrsClientBuilder AddSynchronousMessageSender(this CqrsClientBuilder builder, | |
Action<SynchronousSendMessageModule> config) | |
{ | |
var module = new SynchronousSendMessageModule(); | |
config(module); | |
builder.EnlistModule(module); | |
return builder; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Lokad.Cqrs.Core.Envelope; | |
namespace Lokad.Cqrs.Extensions | |
{ | |
public interface ISynchronousMessageSender | |
{ | |
TEntity Send<TKey, TEntity>(TKey entityKey, object content); | |
TEntity Send<TKey, TEntity>(TKey entityKey, object content, Action<EnvelopeBuilder> configure); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using Autofac; | |
using Lokad.Cqrs.Core.Envelope; | |
using Lokad.Cqrs.Feature.AtomicStorage; | |
namespace Lokad.Cqrs.Extensions | |
{ | |
internal class SimpleSynchronousMessageSender : ISynchronousMessageSender | |
{ | |
private readonly TimeSpan checkInterval; | |
private readonly ILifetimeScope scope; | |
private readonly IMessageSender sender; | |
public SimpleSynchronousMessageSender(IMessageSender sender, ILifetimeScope scope, TimeSpan checkInterval) | |
{ | |
this.sender = sender; | |
this.scope = scope; | |
this.checkInterval = checkInterval; | |
} | |
#region Implementation of ISynchronousMessageSender | |
public TEntity Send<TKey, TEntity>(TKey entityKey, object content) | |
{ | |
return Send<TKey, TEntity>(entityKey, content, builder => { }); | |
} | |
public TEntity Send<TKey, TEntity>(TKey entityKey, object content, Action<EnvelopeBuilder> configure) | |
{ | |
sender.SendOne(content, configure); | |
return WaitForEntity<TKey, TEntity>(entityKey); | |
} | |
private T1 WaitForEntity<T, T1>(T entityKey) | |
{ | |
var reader = scope.Resolve<IAtomicEntityReader<T, T1>>(); | |
T1 entity; | |
while (!reader.TryGet(entityKey, out entity)) | |
{ | |
Thread.Sleep(checkInterval); | |
} | |
return entity; | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Autofac; | |
using Autofac.Core; | |
using Lokad.Cqrs.Core; | |
namespace Lokad.Cqrs.Extensions.Build | |
{ | |
public sealed class SynchronousSendMessageModule : HideObjectMembersFromIntelliSense, IModule | |
{ | |
private TimeSpan checkInterval = TimeSpan.FromSeconds(1); | |
#region IModule Members | |
public void Configure(IComponentRegistry componentRegistry) | |
{ | |
componentRegistry.Register(BuildMessageSender); | |
} | |
#endregion | |
public void CheckEvery(TimeSpan interval) | |
{ | |
checkInterval = interval; | |
} | |
private ISynchronousMessageSender BuildMessageSender(IComponentContext c) | |
{ | |
var sender = c.Resolve<IMessageSender>(); | |
var scope = c.Resolve<ILifetimeScope>(); | |
return new SimpleSynchronousMessageSender(sender, scope, checkInterval); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment