Skip to content

Commit

Permalink
Finished DomainEvents implementation.
Browse files Browse the repository at this point in the history
Added ContextState helper.
  • Loading branch information
cmendible committed May 12, 2014
1 parent b1a4c53 commit 3ea8e6d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 18 deletions.
80 changes: 80 additions & 0 deletions Hexa.Core/ContextState/ContextState.cs
@@ -0,0 +1,80 @@
namespace Hexa.Core
{
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.ServiceModel;
using System.Web;

/// <summary>
/// Context State Helper
/// </summary>
public static class ContextState
{
/// <summary>
/// Gets an item by the specified key.
/// </summary>
/// <typeparam name="T">Type of the item to get</typeparam>
/// <param name="key">The key.</param>
/// <returns>
/// An item of type T
/// </returns>
public static T Get<T>(string key)
{
if (OperationContext.Current != null)
{
return (T)OperationContextState[key];
}
else if (HttpContext.Current != null)
{
return (T)HttpContext.Current.Items[key];
}
else
{
return (T)CallContext.GetData(key);
}
}

/// <summary>
/// Stores the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public static void Store(string key, object value)
{
if (OperationContext.Current != null)
{
OperationContextState[key] = value;
}
else if (HttpContext.Current != null)
{
HttpContext.Current.Items[key] = value;
}
else
{
CallContext.SetData(key, value);
}
}

/// <summary>
/// Gets the state of the operation context.
/// </summary>
/// <value>
/// The state of the operation context.
/// </value>
private static IDictionary<string, object> OperationContextState
{
get
{
OperationContextExtension extension = OperationContext.Current.Extensions.Find<OperationContextExtension>();

if (extension == null)
{
extension = new OperationContextExtension();
OperationContext.Current.Extensions.Add(extension);
}

return extension.State;
}
}
}
}
43 changes: 43 additions & 0 deletions Hexa.Core/ContextState/OperationContextExtension.cs
@@ -0,0 +1,43 @@
namespace Hexa.Core
{
using System.Collections.Generic;
using System.ServiceModel;

/// <summary>
/// OperationContext Extension
/// </summary>
public class OperationContextExtension : IExtension<OperationContext>
{
/// <summary>
/// Initializes a new instance of the <see cref="OperationContextExtension"/> class.
/// </summary>
public OperationContextExtension()
{
State = new Dictionary<string, object>();
}

/// <summary>
/// Gets the state.
/// </summary>
/// <value>
/// The state.
/// </value>
public IDictionary<string, object> State { get; private set; }

/// <summary>
/// Attaches the specified owner.
/// </summary>
/// <param name="owner">The owner.</param>
public void Attach(OperationContext owner)
{
}

/// <summary>
/// Detaches the specified owner.
/// </summary>
/// <param name="owner">The owner.</param>
public void Detach(OperationContext owner)
{
}
}
}
49 changes: 32 additions & 17 deletions Hexa.Core/Domain/Events/DomainEvents.cs
Expand Up @@ -6,34 +6,51 @@
namespace Hexa.Core.Domain
{
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.ServiceModel;
using System.Web;

/// <summary>
/// Domain Event Publisher
/// Domain Event Publisher
/// </summary>
public static class DomainEvents
{
/// <summary>
/// The domain events key
/// </summary>
private const string DomainEventsKey = "Hexa.Core.DomainEvents.Events";

/// <summary>
/// The callback actions
/// </summary>
[ThreadStatic]
private static List<Delegate> actions;

[ThreadStatic]
private static ConcurrentQueue<Action> events;

/// <summary>
/// The event publisher
/// Gets the events.
/// </summary>
private static Func<IEventPublisher> eventPublisher = () => new ConsumeEventPublisher();
/// <value>
/// The events.
/// </value>
private static ConcurrentQueue<Action> Events
{
get
{
if (ContextState.Get<ConcurrentQueue<Action>>(DomainEvents.DomainEventsKey) == null)
{
ContextState.Store(DomainEvents.DomainEventsKey, new ConcurrentQueue<Action>());
}

return ContextState.Get<ConcurrentQueue<Action>>(DomainEvents.DomainEventsKey);
}
}

/// <summary>
/// The publish method
/// The event publisher
/// </summary>
private static MethodInfo publishMethod = typeof(IEventPublisher).GetMethod("Publish");
private static IEventPublisher eventPublisher = new ConsumeEventPublisher();

/// <summary>
/// Clears the callbacks.
Expand All @@ -52,12 +69,7 @@ public static void ClearCallbacks()
public static void Dispatch<T>(T @event)
where T : class
{
if (DomainEvents.events == null)
{
DomainEvents.events = new ConcurrentQueue<Action>();
}

DomainEvents.events.Enqueue(() => eventPublisher().Publish<T>(@event));
DomainEvents.Events.Enqueue(() => eventPublisher.Publish<T>(@event));

if (DomainEvents.actions != null)
{
Expand Down Expand Up @@ -89,10 +101,13 @@ public static void Register<T>(Action<T> callback)
DomainEvents.actions.Add(callback);
}

/// <summary>
/// Raises the queued events.
/// </summary>
public static void Raise()
{
Action dispatch;
while (DomainEvents.events.TryDequeue(out dispatch))
while (DomainEvents.Events.TryDequeue(out dispatch))
{
dispatch();
}
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion Hexa.Core/Hexa.Core.csproj
Expand Up @@ -115,11 +115,13 @@
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="ContextState\ContextState.cs" />
<Compile Include="ContextState\OperationContextExtension.cs" />
<Compile Include="Data\DbProviders.cs" />
<Compile Include="Data\IDatabaseManager.cs" />
<Compile Include="Data\SQLScriptHelper.cs" />
<Compile Include="Domain\Events\Publisher\ConsumeEventPublisher.cs" />
<Compile Include="Domain\Events\Publisher\IConsumeEvent.cs" />
<Compile Include="Domain\Events\IConsumeEvent.cs" />
<Compile Include="Domain\Events\DomainEvents.cs" />
<Compile Include="Domain\Events\Publisher\IEventPublisher.cs" />
<Compile Include="Domain\Model\BaseEntity.cs" />
Expand Down

0 comments on commit 3ea8e6d

Please sign in to comment.