Skip to content

Commit

Permalink
refactor project
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiningRush committed Jan 15, 2018
1 parent 4ba2ba9 commit 2427799
Show file tree
Hide file tree
Showing 28 changed files with 333 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<ItemGroup>
<ProjectReference Include="..\ServiceAnt\ServiceAnt.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="ServiceAnt\Handler\" />
</ItemGroup>
</Project>
9 changes: 5 additions & 4 deletions src/ServiceAnt.IocInstaller.Autofac/ServiceAntModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Autofac;
using ServiceAnt.Base;
using ServiceAnt.Handler.Request;
using ServiceAnt.Infrastructure.Dependency;
using ServiceAnt.Request.Handler;
Expand Down Expand Up @@ -35,7 +36,7 @@ public static void RegisterHandlers(IComponentContext container)
{
foreach (var aHandlerAssembly in _handlerAssemblies)
{
var handlerTypes = aHandlerAssembly.GetTypes().Where(p => typeof(ServiceAnt.Handler.IHandler).IsAssignableFrom(p) && !p.IsInterface);
var handlerTypes = aHandlerAssembly.GetTypes().Where(p => typeof(IHandler).IsAssignableFrom(p) && !p.IsInterface);

foreach (var aHandler in handlerTypes)
{
Expand Down Expand Up @@ -67,7 +68,7 @@ private static void RegisterHandlerType(IComponentContext container, Type aHandl
var interfaces = aHandlerType.GetInterfaces();
foreach (var aInterface in interfaces)
{
if (!typeof(ServiceAnt.Handler.IHandler).IsAssignableFrom(aInterface))
if (!typeof(IHandler).IsAssignableFrom(aInterface))
{
continue;
}
Expand All @@ -76,9 +77,9 @@ private static void RegisterHandlerType(IComponentContext container, Type aHandl
if (genericArgs.Length == 1)
{
if (typeof(IRequestHandler).IsAssignableFrom(aInterface))
container.Resolve<IServiceBus>().AddRequestHandler(genericArgs[0], new ServiceAnt.Handler.IocHandlerFactory(container.Resolve<IocResolver>(), aHandlerType, genericArgs[0]));
container.Resolve<IServiceBus>().AddRequestHandler(genericArgs[0], new IocHandlerFactory(container.Resolve<IocResolver>(), aHandlerType, genericArgs[0]));
else
container.Resolve<IServiceBus>().AddSubscription(genericArgs[0], new ServiceAnt.Handler.IocHandlerFactory(container.Resolve<IocResolver>(), aHandlerType, genericArgs[0]));
container.Resolve<IServiceBus>().AddSubscription(genericArgs[0], new IocHandlerFactory(container.Resolve<IocResolver>(), aHandlerType, genericArgs[0]));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/ServiceAnt.IocInstaller.Castle/ServiceAntInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Install(IWindsorContainer container, IConfigurationStore store)
foreach (var aHandlerAssembly in _handlerAssemblies)
{
container.Register(Classes.FromAssembly(aHandlerAssembly)
.BasedOn<ServiceAnt.Handler.IHandler>()
.BasedOn<Base.IHandler>()
.WithService.Self()
.LifestyleTransient());
}
Expand All @@ -62,15 +62,15 @@ public void Install(IWindsorContainer container, IConfigurationStore store)

private void Kernel_ComponentRegistered(string key, IHandler handler)
{
if (!typeof(ServiceAnt.Handler.IHandler).GetTypeInfo().IsAssignableFrom(handler.ComponentModel.Implementation))
if (!typeof(Base.IHandler).GetTypeInfo().IsAssignableFrom(handler.ComponentModel.Implementation))
{
return;
}

var interfaces = handler.ComponentModel.Implementation.GetTypeInfo().GetInterfaces();
foreach (var aInterface in interfaces)
{
if (!typeof(ServiceAnt.Handler.IHandler).GetTypeInfo().IsAssignableFrom(aInterface))
if (!typeof(Base.IHandler).GetTypeInfo().IsAssignableFrom(aInterface))
{
continue;
}
Expand All @@ -81,9 +81,9 @@ private void Kernel_ComponentRegistered(string key, IHandler handler)
if (genericArgs.Length == 1)
{
if (typeof(IRequestHandler).GetTypeInfo().IsAssignableFrom(aInterface))
_serviceBus.AddRequestHandler(genericArgs[0], new ServiceAnt.Handler.IocHandlerFactory(resolver, handler.ComponentModel.Implementation, genericArgs[0]));
_serviceBus.AddRequestHandler(genericArgs[0], new Base.IocHandlerFactory(resolver, handler.ComponentModel.Implementation, genericArgs[0]));
else
_serviceBus.AddSubscription(genericArgs[0], new ServiceAnt.Handler.IocHandlerFactory(resolver, handler.ComponentModel.Implementation, genericArgs[0]));
_serviceBus.AddSubscription(genericArgs[0], new Base.IocHandlerFactory(resolver, handler.ComponentModel.Implementation, genericArgs[0]));
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/ServiceAnt/Base/IHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ServiceAnt.Base
{
/// <summary>
/// All handler function should inherit this interface
/// </summary>
public interface IHandler
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace ServiceAnt.Handler
namespace ServiceAnt.Base
{
/// <summary>
/// The interface is used to get handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,52 @@
using ServiceAnt.Infrastructure.Dependency;
using System;

namespace ServiceAnt.Handler
namespace ServiceAnt.Base
{
/// <summary>
/// This factory is used to get hanlder with IOC
/// </summary>
public class IocHandlerFactory : IHandlerFactory
{
private IIocResolver _iocResolver;
private Type _handlerType;
private Type _localEventType;

/// <summary>
/// ctor
/// </summary>
/// <param name="iocResolver"></param>
/// <param name="handlerType"></param>
/// <param name="localEventType"></param>
public IocHandlerFactory(IIocResolver iocResolver, Type handlerType, Type localEventType)
{
_iocResolver = iocResolver;
_handlerType = handlerType;
_localEventType = localEventType;
}

/// <summary>
/// get handler
/// </summary>
/// <returns></returns>
public IHandler GetHandler()
{
return _iocResolver.Resolve<IHandler>(_handlerType);
}

/// <summary>
/// release handler if need
/// </summary>
/// <param name="obj"></param>
public void ReleaseHandler(object obj)
{
_iocResolver.Release(obj);
}

/// <summary>
/// get the trigger class type in excuting process
/// </summary>
/// <returns></returns>
public Type GetLocalEventType()
{
return _localEventType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace ServiceAnt.Handler
namespace ServiceAnt.Base
{
internal class SingletonHandlerFactory : IHandlerFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace ServiceAnt.Handler
namespace ServiceAnt.Base
{
/// <summary>
/// It is used to trigger event or request
Expand Down
6 changes: 0 additions & 6 deletions src/ServiceAnt/Handler/IHandler.cs

This file was deleted.

9 changes: 5 additions & 4 deletions src/ServiceAnt/IServiceBus.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ServiceAnt.Handler;
using ServiceAnt.Base;
using ServiceAnt.Handler.Request;
using ServiceAnt.Request;
using ServiceAnt.Subscription;
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -48,22 +49,22 @@ public interface IServiceBus : IAddSubscription, IAddRequestHandler
/// </summary>
/// <param name="event"></param>
/// <returns></returns>
Task Publish(ITrigger @event);
Task Publish(IEventTrigger @event);

/// <summary>
/// Send a request sync
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="event"></param>
/// <returns></returns>
T Send<T>(ITrigger @event);
T Send<T>(IRequestTrigger @event);

/// <summary>
/// Send a request async
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="event"></param>
/// <returns></returns>
Task<T> SendAsync<T>(ITrigger @event);
Task<T> SendAsync<T>(IRequestTrigger @event);
}
}
47 changes: 25 additions & 22 deletions src/ServiceAnt/InProcessServiceBus.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ServiceAnt.Handler;
using ServiceAnt.Base;
using ServiceAnt.Handler.Request;
using ServiceAnt.Handler.Subscription.Handler;
using ServiceAnt.Request;
using ServiceAnt.Request.Handler;
using ServiceAnt.Subscription;
using ServiceAnt.Subscription.Handler;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ServiceAnt
Expand Down Expand Up @@ -79,7 +79,7 @@ public void AddSubscription(Type eventType, IHandlerFactory factory)
/// </summary>
/// <typeparam name="TEvent">The event must inherit TransportTray</typeparam>
/// <param name="action">Handler delegate</param>
public void AddSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : ITrigger
public void AddSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : IEventTrigger
{
_subcriptionManager.AddSubscription<TEvent>(action);
}
Expand All @@ -89,7 +89,7 @@ public void AddSubscription(Type eventType, IHandlerFactory factory)
/// </summary>
/// <typeparam name="TEvent">The event must inherit TransportTray </typeparam>
/// <param name="factory"></param>s
public void AddSubscription<TEvent>(IHandlerFactory factory) where TEvent : ITrigger
public void AddSubscription<TEvent>(IHandlerFactory factory) where TEvent : IEventTrigger
{
_subcriptionManager.AddSubscription<TEvent>(factory);
}
Expand All @@ -110,7 +110,7 @@ public void RemoveDynamicSubscription(string eventName, Func<dynamic, Task> acti
/// </summary>
/// <typeparam name="TEvent">The event must inherit TransportTray</typeparam>
/// <param name="action">Handler delegate</param>
public void RemoveSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : ITrigger
public void RemoveSubscription<TEvent>(Func<TEvent, Task> action) where TEvent : IEventTrigger
{
_subcriptionManager.RemoveSubscription<TEvent>(action);
}
Expand All @@ -120,7 +120,7 @@ public void RemoveDynamicSubscription(string eventName, Func<dynamic, Task> acti
/// </summary>
/// <param name="event"></param>
/// <returns></returns>
public Task Publish(ITrigger @event)
public Task Publish(IEventTrigger @event)
{
var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event));
return asyncTask;
Expand All @@ -130,7 +130,7 @@ public Task Publish(ITrigger @event)
/// Publish a event sync
/// </summary>
/// <param name="event"></param>
public void PublishSync(ITrigger @event)
public void PublishSync(IEventTrigger @event)
{
var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event));
asyncTask.Wait();
Expand Down Expand Up @@ -164,7 +164,7 @@ private async Task ProcessEvent(string eventName, string message)
}
catch (Exception ex)
{
LogMessage( LogLevel.ERROR, "There has raised a error when publishing event.", ex);
LogMessage( LogLevel.ERROR, "There has caught a error when publishing event.", ex);
}
}
}
Expand All @@ -186,23 +186,23 @@ public void AddRequestHandler(Type eventType, IHandlerFactory factory)
/// <summary>
/// Register handler with handler factory
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <typeparam name="TRequest"></typeparam>
/// <param name="factory"></param>
public void AddRequestHandler<TEvent>(IHandlerFactory factory)
where TEvent : ITrigger
public void AddRequestHandler<TRequest>(IHandlerFactory factory)
where TRequest : IRequestTrigger
{
_requestHandlerManager.AddRequestHandler<TEvent>(factory);
_requestHandlerManager.AddRequestHandler<TRequest>(factory);
}

/// <summary>
/// Register handler with delegate
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <typeparam name="TRequest"></typeparam>
/// <param name="action"></param>
public void AddRequestHandler<TEvent>(Func<TEvent, IRequestHandlerContext, Task> action)
where TEvent : ITrigger
public void AddRequestHandler<TRequest>(Func<TRequest, IRequestHandlerContext, Task> action)
where TRequest : IRequestTrigger
{
_requestHandlerManager.AddRequestHandler<TEvent>(action);
_requestHandlerManager.AddRequestHandler<TRequest>(action);
}

/// <summary>
Expand All @@ -218,10 +218,10 @@ public void AddDynamicRequestHandler(string eventName, Func<dynamic, IRequestHan
/// <summary>
/// Remove handler by event type
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <typeparam name="TRequest"></typeparam>
/// <param name="action"></param>
public void RemoveRequestHandler<TEvent>(Func<TEvent, IRequestHandlerContext, Task> action)
where TEvent : ITrigger
public void RemoveRequestHandler<TRequest>(Func<TRequest, IRequestHandlerContext, Task> action)
where TRequest : IRequestTrigger
{
_requestHandlerManager.RemoveRequestHandler(action);
}
Expand All @@ -242,7 +242,7 @@ public void RemoveDynamicRequestHandler(string eventName, Func<dynamic, IRequest
/// <typeparam name="T"></typeparam>
/// <param name="event"></param>
/// <returns></returns>
public T Send<T>(ITrigger @event)
public T Send<T>(IRequestTrigger @event)
{
var asyncTask = ProcessRequest<T>(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event));
asyncTask.ConfigureAwait(false);
Expand All @@ -255,7 +255,7 @@ public T Send<T>(ITrigger @event)
/// <typeparam name="T"></typeparam>
/// <param name="event"></param>
/// <returns></returns>
public async Task<T> SendAsync<T>(ITrigger @event)
public async Task<T> SendAsync<T>(IRequestTrigger @event)
{
return await ProcessRequest<T>(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event));
}
Expand Down Expand Up @@ -306,7 +306,10 @@ private async Task<T> ProcessRequest<T>(string eventName, string message)

private void LogMessage(LogLevel type, string value, Exception ex)
{
OnLogBusMessage?.Invoke(type, value, ex);
if (OnLogBusMessage != null)
OnLogBusMessage(type, value, ex);
else
throw ex;
}
}
}
7 changes: 2 additions & 5 deletions src/ServiceAnt/Request/Handler/ActionRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using ServiceAnt.Handler;
using ServiceAnt.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServiceAnt.Request.Handler
Expand All @@ -11,7 +8,7 @@ namespace ServiceAnt.Request.Handler
/// The container of request handler registing with delegate
/// </summary>
/// <typeparam name="TRequest"></typeparam>
public class ActionRequestHandler<TRequest> : IRequestHandler<TRequest> where TRequest : ITrigger
public class ActionRequestHandler<TRequest> : IRequestHandler<TRequest> where TRequest : IRequestTrigger
{
private Func<TRequest, IRequestHandlerContext, Task> _action;

Expand Down
5 changes: 3 additions & 2 deletions src/ServiceAnt/Request/Handler/IRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ServiceAnt.Handler;
using ServiceAnt.Base;
using ServiceAnt.Handler;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,7 +19,7 @@ public interface IRequestHandler : IHandler
/// The interface of handling request
/// </summary>
/// <typeparam name="TRequest">request parameter</typeparam>
public interface IRequestHandler<TRequest> : IRequestHandler where TRequest : ITrigger
public interface IRequestHandler<TRequest> : IRequestHandler where TRequest : IRequestTrigger
{
/// <summary>
/// Handle request async
Expand Down
Loading

0 comments on commit 2427799

Please sign in to comment.