Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

V1.1 #19

Open
wants to merge 1 commit into
base: v1.1
Choose a base branch
from
Open

V1.1 #19

Changes from all commits
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
154 changes: 78 additions & 76 deletions NCommon/src/Events/DomainEvent.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
#region license
//Copyright 2010 Ritesh Rao

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using NCommon.Extensions;
using NCommon.State;

namespace NCommon.Events
{
///<summary>
/// DomainEvent class that allowes raising domain events from domain entities and allow registring
/// custom callbacks to execute when a <see cref="IDomainEvent"/> is raised.
///</summary>
public class DomainEvent
{
const string CallbackListKey = "DomainEvent.Callbacks";

///<summary>
/// Registers a callback to be called when a domain event is raised.
///</summary>
///<param name="callback">An <see cref="Action{T}"/> to be invoked.</param>
///<typeparam name="T">The domain event that the callback is registered to handle.</typeparam>
public static void RegisterCallback<T>(Action<T> callback) where T : IDomainEvent
{
var state = ServiceLocator.Current.GetInstance<IState>();
var callbacks = state.Local.Get<IList<Delegate>>(CallbackListKey);
if (callbacks == null)
{
callbacks = new List<Delegate>();
state.Local.Put(CallbackListKey, callbacks);
}
callbacks.Add(callback);
}

///<summary>
/// Clears all callbacks registered on the current thread.
///</summary>
public static void ClearCallbacks()
{
var state = ServiceLocator.Current.GetInstance<IState>();
state.Application.Remove<IList<Delegate>>(CallbackListKey);
}

///<summary>
/// Raises a <see cref="IDomainEvent"/>.
///</summary>
///<param name="event">A instance <see cref="IDomainEvent"/> to raise.</param>
///<typeparam name="T">A type implementing <see cref="IDomainEvent"/></typeparam>
public static void Raise<T>(T @event) where T : IDomainEvent
{
var state = ServiceLocator.Current.GetInstance<IState>();
var handlers = ServiceLocator.Current.GetAllInstances<Handles<T>>();
if (handlers != null)
handlers.ForEach(x => x.Handle(@event));

var callbacks = state.Local.Get<IList<Delegate>>(CallbackListKey);
if (callbacks != null && callbacks.Count > 0)
callbacks.OfType<Action<T>>().ForEach(x => x(@event));
}
}
#region license
//Copyright 2010 Ritesh Rao

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using NCommon.Extensions;
using NCommon.State;

namespace NCommon.Events
{
///<summary>
/// DomainEvent class that allowes raising domain events from domain entities and allow registring
/// custom callbacks to execute when a <see cref="IDomainEvent"/> is raised.
///</summary>
public class DomainEvent
{
const string CallbackListKey = "DomainEvent.Callbacks";

///<summary>
/// Registers a callback to be called when a domain event is raised.
///</summary>
///<param name="callback">An <see cref="Action{T}"/> to be invoked.</param>
///<typeparam name="T">The domain event that the callback is registered to handle.</typeparam>
public static void RegisterCallback<T>(Action<T> callback) where T : IDomainEvent
{
var state = ServiceLocator.Current.GetInstance<IState>();
var callbacks = state.Local.Get<IList<Delegate>>(CallbackListKey);
if (callbacks == null)
{
callbacks = new List<Delegate>();
state.Local.Put(CallbackListKey, callbacks);
}
callbacks.Add(callback);
}

///<summary>
/// Clears all callbacks registered on the current thread.
///</summary>
public static void ClearCallbacks()
{
var state = ServiceLocator.Current.GetInstance<IState>();
state.Application.Remove<IList<Delegate>>(CallbackListKey);
}

///<summary>
/// Raises a <see cref="IDomainEvent"/>.
///</summary>
///<param name="event">A instance <see cref="IDomainEvent"/> to raise.</param>
///<typeparam name="T">A type implementing <see cref="IDomainEvent"/></typeparam>
public static void Raise<T>(T @event) where T : IDomainEvent
{
var handlers = ServiceLocator.Current.GetAllInstances<Handles<T>>();
if (handlers != null)
handlers.ForEach(x => x.Handle(@event));

var state = ServiceLocator.Current.GetInstance<IState>();
if (state == null)
return;
var callbacks = state.Local.Get<IList<Delegate>>(CallbackListKey);
if (callbacks != null && callbacks.Count > 0)
callbacks.OfType<Action<T>>().ForEach(x => x(@event));
}
}
}