diff --git a/src/ServiceAnt/Handler/TransportTray.cs b/src/ServiceAnt/Handler/TransportTray.cs index dd9d9be..b031c80 100644 --- a/src/ServiceAnt/Handler/TransportTray.cs +++ b/src/ServiceAnt/Handler/TransportTray.cs @@ -6,26 +6,31 @@ namespace ServiceAnt.Handler { - public abstract class TransportTray + /// + /// It is used to trigger event or request + /// + public interface ITrigger { - public TransportTray() - { - Id = Guid.NewGuid(); - CreationDate = DateTime.UtcNow; - } - - public Guid Id { get; } - public DateTime CreationDate { get; } } - - public abstract class TransportTray : TransportTray + + /// + /// It is used to trigger event or request + /// + /// + public abstract class Trigger : ITrigger { - public TEntity TransportEntity { get; set; } + /// + /// Dto + /// + public TDto Dto { get; set; } - public TransportTray(TEntity entity) : base() + /// + /// ctor + /// + /// + public Trigger(TDto inputDto) { - TransportEntity = entity; + Dto = inputDto; } } - } diff --git a/src/ServiceAnt/IServiceBus.cs b/src/ServiceAnt/IServiceBus.cs index 4a58729..deaea87 100644 --- a/src/ServiceAnt/IServiceBus.cs +++ b/src/ServiceAnt/IServiceBus.cs @@ -48,7 +48,7 @@ public interface IServiceBus : IAddSubscription, IAddRequestHandler /// /// /// - Task Publish(TransportTray @event); + Task Publish(ITrigger @event); /// /// Send a request sync @@ -56,7 +56,7 @@ public interface IServiceBus : IAddSubscription, IAddRequestHandler /// /// /// - T Send(TransportTray @event); + T Send(ITrigger @event); /// /// Send a request async @@ -64,6 +64,6 @@ public interface IServiceBus : IAddSubscription, IAddRequestHandler /// /// /// - Task SendAsync(TransportTray @event); + Task SendAsync(ITrigger @event); } } diff --git a/src/ServiceAnt/InProcessServiceBus.cs b/src/ServiceAnt/InProcessServiceBus.cs index eb15d39..77bce4c 100644 --- a/src/ServiceAnt/InProcessServiceBus.cs +++ b/src/ServiceAnt/InProcessServiceBus.cs @@ -5,6 +5,7 @@ using ServiceAnt.Handler.Subscription.Handler; using ServiceAnt.Request.Handler; using ServiceAnt.Subscription; +using ServiceAnt.Subscription.Handler; using System; using System.Linq; using System.Threading.Tasks; @@ -78,7 +79,7 @@ public void AddSubscription(Type eventType, IHandlerFactory factory) /// /// The event must inherit TransportTray /// Handler delegate - public void AddSubscription(Func action) where TEvent : TransportTray + public void AddSubscription(Func action) where TEvent : ITrigger { _subcriptionManager.AddSubscription(action); } @@ -88,7 +89,7 @@ public void AddSubscription(Type eventType, IHandlerFactory factory) /// /// The event must inherit TransportTray /// s - public void AddSubscription(IHandlerFactory factory) where TEvent : TransportTray + public void AddSubscription(IHandlerFactory factory) where TEvent : ITrigger { _subcriptionManager.AddSubscription(factory); } @@ -109,7 +110,7 @@ public void RemoveDynamicSubscription(string eventName, Func acti /// /// The event must inherit TransportTray /// Handler delegate - public void RemoveSubscription(Func action) where TEvent : TransportTray + public void RemoveSubscription(Func action) where TEvent : ITrigger { _subcriptionManager.RemoveSubscription(action); } @@ -119,7 +120,7 @@ public void RemoveDynamicSubscription(string eventName, Func acti /// /// /// - public Task Publish(TransportTray @event) + public Task Publish(ITrigger @event) { var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event)); return asyncTask; @@ -129,7 +130,7 @@ public Task Publish(TransportTray @event) /// Publish a event sync /// /// - public void PublishSync(TransportTray @event) + public void PublishSync(ITrigger @event) { var asyncTask = ProcessEvent(_subcriptionManager.GetEventName(@event.GetType()), JsonConvert.SerializeObject(@event)); asyncTask.Wait(); @@ -188,7 +189,7 @@ public void AddRequestHandler(Type eventType, IHandlerFactory factory) /// /// public void AddRequestHandler(IHandlerFactory factory) - where TEvent : TransportTray + where TEvent : ITrigger { _requestHandlerManager.AddRequestHandler(factory); } @@ -199,7 +200,7 @@ public void AddRequestHandler(IHandlerFactory factory) /// /// public void AddRequestHandler(Func action) - where TEvent : TransportTray + where TEvent : ITrigger { _requestHandlerManager.AddRequestHandler(action); } @@ -220,7 +221,7 @@ public void AddDynamicRequestHandler(string eventName, Func /// public void RemoveRequestHandler(Func action) - where TEvent : TransportTray + where TEvent : ITrigger { _requestHandlerManager.RemoveRequestHandler(action); } @@ -241,7 +242,7 @@ public void RemoveDynamicRequestHandler(string eventName, Func /// /// - public T Send(TransportTray @event) + public T Send(ITrigger @event) { var asyncTask = ProcessRequest(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event)); asyncTask.ConfigureAwait(false); @@ -254,7 +255,7 @@ public T Send(TransportTray @event) /// /// /// - public async Task SendAsync(TransportTray @event) + public async Task SendAsync(ITrigger @event) { return await ProcessRequest(_requestHandlerManager.GetRequestName(@event.GetType()), JsonConvert.SerializeObject(@event)); } @@ -298,7 +299,7 @@ private async Task ProcessRequest(string eventName, string message) } } - return (T)requestContext.Response; + return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(requestContext.Response)); } #endregion diff --git a/src/ServiceAnt/Request/Handler/ActionRequestHandler.cs b/src/ServiceAnt/Request/Handler/ActionRequestHandler.cs index 3c13036..eb4315b 100644 --- a/src/ServiceAnt/Request/Handler/ActionRequestHandler.cs +++ b/src/ServiceAnt/Request/Handler/ActionRequestHandler.cs @@ -11,7 +11,7 @@ namespace ServiceAnt.Request.Handler /// The container of request handler registing with delegate /// /// - public class ActionRequestHandler : IRequestHandler where TRequest : TransportTray + public class ActionRequestHandler : IRequestHandler where TRequest : ITrigger { private Func _action; diff --git a/src/ServiceAnt/Request/Handler/IRequestHandler.cs b/src/ServiceAnt/Request/Handler/IRequestHandler.cs index 2abaa01..5891746 100644 --- a/src/ServiceAnt/Request/Handler/IRequestHandler.cs +++ b/src/ServiceAnt/Request/Handler/IRequestHandler.cs @@ -18,7 +18,7 @@ public interface IRequestHandler : IHandler /// The interface of handling request /// /// request parameter - public interface IRequestHandler : IRequestHandler where TRequest : TransportTray + public interface IRequestHandler : IRequestHandler where TRequest : ITrigger { /// /// Handle request async diff --git a/src/ServiceAnt/Request/IAddRequestHandler.cs b/src/ServiceAnt/Request/IAddRequestHandler.cs index f73186a..323c6d9 100644 --- a/src/ServiceAnt/Request/IAddRequestHandler.cs +++ b/src/ServiceAnt/Request/IAddRequestHandler.cs @@ -25,7 +25,7 @@ public interface IAddRequestHandler /// /// void AddRequestHandler(IHandlerFactory factory) - where TEvent : TransportTray; + where TEvent : ITrigger; /// /// Register handler with delegate @@ -33,7 +33,7 @@ void AddRequestHandler(IHandlerFactory factory) /// /// void AddRequestHandler(Func action) - where TEvent : TransportTray; + where TEvent : ITrigger; /// /// Register dynamic handler with delegate @@ -48,7 +48,7 @@ void AddRequestHandler(Func action /// /// void RemoveRequestHandler(Func action) - where TEvent : TransportTray; + where TEvent : ITrigger; /// /// Remove dynamic handler by eventName diff --git a/src/ServiceAnt/Request/IRequestHandlerManager.cs b/src/ServiceAnt/Request/IRequestHandlerManager.cs index 7d9d5b7..a3d076c 100644 --- a/src/ServiceAnt/Request/IRequestHandlerManager.cs +++ b/src/ServiceAnt/Request/IRequestHandlerManager.cs @@ -23,7 +23,7 @@ public interface IRequestHandlerManager : IAddRequestHandler /// /// request object /// - List GetHandlerFactoriesForRequest(TransportTray @request); + List GetHandlerFactoriesForRequest(ITrigger @request); /// /// Get the request name by TransportTray type diff --git a/src/ServiceAnt/Request/InMemoryRequestHandlerManager.cs b/src/ServiceAnt/Request/InMemoryRequestHandlerManager.cs index bcb874f..6c82656 100644 --- a/src/ServiceAnt/Request/InMemoryRequestHandlerManager.cs +++ b/src/ServiceAnt/Request/InMemoryRequestHandlerManager.cs @@ -50,7 +50,7 @@ public void AddRequestHandler(Type eventType, IHandlerFactory factory) /// /// /// - public void AddRequestHandler(Func action) where TEvent : TransportTray + public void AddRequestHandler(Func action) where TEvent : ITrigger { var eventHandler = new ActionRequestHandler(action); @@ -62,7 +62,7 @@ public void AddRequestHandler(Type eventType, IHandlerFactory factory) /// /// /// - public void AddRequestHandler(IHandlerFactory factory) where TEvent : TransportTray + public void AddRequestHandler(IHandlerFactory factory) where TEvent : ITrigger { DoAddRequestHandler(factory, typeof(TEvent)); } @@ -72,7 +72,7 @@ public void AddRequestHandler(Type eventType, IHandlerFactory factory) /// /// request object /// - public List GetHandlerFactoriesForRequest(TransportTray request) + public List GetHandlerFactoriesForRequest(ITrigger request) { return GetOrCreateHandlerFactories(GetRequestName(request.GetType())); } @@ -138,7 +138,7 @@ public void RemoveDynamicRequestHandler(string eventName, Func /// /// - public void RemoveRequestHandler(Func action) where TEvent : TransportTray + public void RemoveRequestHandler(Func action) where TEvent : ITrigger { GetOrCreateHandlerFactories(typeof(TEvent).Name) .Locking(factories => diff --git a/src/ServiceAnt/Subscription/Handler/ActionEventHandler.cs b/src/ServiceAnt/Subscription/Handler/ActionEventHandler.cs index 9b7b446..69316af 100644 --- a/src/ServiceAnt/Subscription/Handler/ActionEventHandler.cs +++ b/src/ServiceAnt/Subscription/Handler/ActionEventHandler.cs @@ -1,42 +1,78 @@ -using System; +using ServiceAnt.Subscription.Handler; +using System; using System.Threading.Tasks; namespace ServiceAnt.Handler.Subscription.Handler { - public class ActionEventHandler : IEventHandler where TEvent : TransportTray + /// + /// Handle event by action + /// + /// + public class ActionEventHandler : IEventHandler where TEvent : ITrigger { private Func _action; + /// + /// ctor + /// + /// public ActionEventHandler(Func action) { _action = action; } + /// + /// handle event + /// + /// + /// public async Task HandleAsync(TEvent param) { await _action(param); } + /// + /// check if input action is same with self + /// + /// + /// public bool IsSame(Func compareAction) { return compareAction == _action; } } + /// + /// Handle event by dynamic action + /// public class ActionEventHandler : IDynamicEventHandler { private Func _action; + /// + /// ctor + /// + /// public ActionEventHandler(Func action) { _action = action; } + /// + /// handle event + /// + /// + /// public async Task HandleAsync(dynamic param) { await _action(param); } + /// + /// check if input action is same with self + /// + /// + /// public bool IsSame(Func compareAction) { return compareAction == _action; diff --git a/src/ServiceAnt/Subscription/Handler/IEventHandler.cs b/src/ServiceAnt/Subscription/Handler/IEventHandler.cs index 3361e2f..83793ba 100644 --- a/src/ServiceAnt/Subscription/Handler/IEventHandler.cs +++ b/src/ServiceAnt/Subscription/Handler/IEventHandler.cs @@ -1,13 +1,23 @@ -using System; +using ServiceAnt.Handler; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ServiceAnt.Handler.Subscription.Handler +namespace ServiceAnt.Subscription.Handler { - public interface IEventHandler : IHandler where TEvent : TransportTray + /// + /// It is used to handle event + /// + /// + public interface IEventHandler : IHandler where TEvent : ITrigger { + /// + /// handle event + /// + /// + /// Task HandleAsync(TEvent param); } } diff --git a/src/ServiceAnt/Subscription/IAddSubscription.cs b/src/ServiceAnt/Subscription/IAddSubscription.cs index 75500d0..d4a4977 100644 --- a/src/ServiceAnt/Subscription/IAddSubscription.cs +++ b/src/ServiceAnt/Subscription/IAddSubscription.cs @@ -22,7 +22,7 @@ public interface IAddSubscription /// The event must inherit TransportTray /// void AddSubscription(IHandlerFactory factory) - where TEvent : TransportTray; + where TEvent : ITrigger; /// /// Add Subscription for event by delegate @@ -30,7 +30,7 @@ void AddSubscription(IHandlerFactory factory) /// The event must inherit TransportTray /// Handler delegate void AddSubscription(Func action) - where TEvent : TransportTray; + where TEvent : ITrigger; /// /// Add Subscription for event by delegate with dynamic parameter @@ -45,7 +45,7 @@ void AddSubscription(Func action) /// The event must inherit TransportTray /// Handler delegate void RemoveSubscription(Func action) - where TEvent : TransportTray; + where TEvent : ITrigger; /// /// Remove subscription from servicebus diff --git a/src/ServiceAnt/Subscription/ISubscriptionManager.cs b/src/ServiceAnt/Subscription/ISubscriptionManager.cs index 0c3b6d1..f521954 100644 --- a/src/ServiceAnt/Subscription/ISubscriptionManager.cs +++ b/src/ServiceAnt/Subscription/ISubscriptionManager.cs @@ -24,7 +24,7 @@ public interface ISubscriptionManager : IAddSubscription /// /// /// - List GetHandlerFactoriesForEvent(TransportTray @event); + List GetHandlerFactoriesForEvent(ITrigger @event); /// /// Get event name by event type diff --git a/src/ServiceAnt/Subscription/InMemorySubscriptionsManager.cs b/src/ServiceAnt/Subscription/InMemorySubscriptionsManager.cs index c3add3d..2113310 100644 --- a/src/ServiceAnt/Subscription/InMemorySubscriptionsManager.cs +++ b/src/ServiceAnt/Subscription/InMemorySubscriptionsManager.cs @@ -51,7 +51,7 @@ public void AddSubscription(Type eventType, IHandlerFactory factory) /// /// The event must inherit TransportTray /// Handler delegate - public void AddSubscription(Func action) where TEvent : TransportTray + public void AddSubscription(Func action) where TEvent : ITrigger { var eventHandler = new ActionEventHandler(action); @@ -63,7 +63,7 @@ public void AddSubscription(Type eventType, IHandlerFactory factory) /// /// The event must inherit TransportTray /// - public void AddSubscription(IHandlerFactory factory) where TEvent : TransportTray + public void AddSubscription(IHandlerFactory factory) where TEvent : ITrigger { DoAddSubscription(factory, typeof(TEvent)); } @@ -83,7 +83,7 @@ public List GetHandlerFactoriesForEvent(string eventName) /// /// /// - public List GetHandlerFactoriesForEvent(TransportTray @event) + public List GetHandlerFactoriesForEvent(ITrigger @event) { return GetOrCreateHandlerFactories(GetEventName(@event.GetType())); } @@ -122,7 +122,7 @@ public void RemoveDynamicSubscription(string eventName, Func acti /// /// The event must inherit TransportTray /// Handler delegate - public void RemoveSubscription(Func action) where TEvent : TransportTray + public void RemoveSubscription(Func action) where TEvent : ITrigger { GetOrCreateHandlerFactories(typeof(TEvent).Name) .Locking(factories => diff --git a/test/ServiceAnt.IocInstaller.Autofac.Test/ServiceAntModule_Test.cs b/test/ServiceAnt.IocInstaller.Autofac.Test/ServiceAntModule_Test.cs index 42bd887..0f4376b 100644 --- a/test/ServiceAnt.IocInstaller.Autofac.Test/ServiceAntModule_Test.cs +++ b/test/ServiceAnt.IocInstaller.Autofac.Test/ServiceAntModule_Test.cs @@ -5,6 +5,7 @@ using ServiceAnt.Handler.Subscription.Handler; using ServiceAnt.Request.Handler; using ServiceAnt.Handler; +using ServiceAnt.Subscription.Handler; namespace ServiceAnt.IocInstaller.Autofac.Test { @@ -26,7 +27,7 @@ public async Task CanHandleEventByIocHandler() var autofacContainer = newContainer.Build(); ServiceAntModule.RegisterHandlers(autofacContainer); - await autofacContainer.Resolve().Publish(new TestTray() { Result = testValue }); + await autofacContainer.Resolve().Publish(new TestTrigger() { Result = testValue }); Assert.AreEqual(testValue, RESULT_CONTAINER); } @@ -40,19 +41,19 @@ public async Task CanHandleRequestByIocHandler() var autofacContainer = newContainer.Build(); ServiceAntModule.RegisterHandlers(autofacContainer); - var result = await autofacContainer.Resolve().SendAsync(new TestTray() { Result = testValue }); + var result = await autofacContainer.Resolve().SendAsync(new TestTrigger() { Result = testValue }); Assert.AreEqual(testValue, result); } - public class TestTray : TransportTray + public class TestTrigger : ITrigger { public string Result { get; set; } } - public class IocEventHandler : IEventHandler + public class IocEventHandler : IEventHandler { - public Task HandleAsync(TestTray param) + public Task HandleAsync(TestTrigger param) { RESULT_CONTAINER = param.Result; @@ -60,9 +61,9 @@ public Task HandleAsync(TestTray param) } } - public class IocRequestHandler : IRequestHandler + public class IocRequestHandler : IRequestHandler { - public Task HandleAsync(TestTray param, IRequestHandlerContext handlerContext) + public Task HandleAsync(TestTrigger param, IRequestHandlerContext handlerContext) { handlerContext.Response = param.Result; return Task.Delay(1); diff --git a/test/ServiceAnt.IocInstaller.Castle.test/ServiceAntInstaller_Test.cs b/test/ServiceAnt.IocInstaller.Castle.test/ServiceAntInstaller_Test.cs index 4215ca8..c9157e9 100644 --- a/test/ServiceAnt.IocInstaller.Castle.test/ServiceAntInstaller_Test.cs +++ b/test/ServiceAnt.IocInstaller.Castle.test/ServiceAntInstaller_Test.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Castle.MicroKernel.Registration; using ServiceAnt.Request.Handler; +using ServiceAnt.Subscription.Handler; namespace ServiceAnt.IocInstaller.Castle.Test { @@ -30,7 +31,7 @@ public async Task CanHandleEventByIocHandler() var newContainer = new WindsorContainer(); newContainer.Install(new ServiceAntInstaller(System.Reflection.Assembly.GetExecutingAssembly())); - await newContainer.Resolve().Publish(new TestTray() { Result = testValue }); + await newContainer.Resolve().Publish(new TestTrigger() { Result = testValue }); Assert.AreEqual(testValue, RESULT_CONTAINER); } @@ -42,19 +43,19 @@ public async Task CanHandleRequestByIocHandler() var newContainer = new WindsorContainer(); newContainer.Install(new ServiceAntInstaller(System.Reflection.Assembly.GetExecutingAssembly())); - var result = await newContainer.Resolve().SendAsync(new TestTray() { Result = testValue }); + var result = await newContainer.Resolve().SendAsync(new TestTrigger() { Result = testValue }); Assert.AreEqual(testValue, result); } - public class TestTray : TransportTray + public class TestTrigger : ITrigger { public string Result { get; set; } } - public class IocEventHandler : IEventHandler + public class IocEventHandler : IEventHandler { - public Task HandleAsync(TestTray param) + public Task HandleAsync(TestTrigger param) { RESULT_CONTAINER = param.Result; @@ -62,9 +63,9 @@ public Task HandleAsync(TestTray param) } } - public class IocRequestHandler : IRequestHandler + public class IocRequestHandler : IRequestHandler { - public Task HandleAsync(TestTray param, IRequestHandlerContext handlerContext) + public Task HandleAsync(TestTrigger param, IRequestHandlerContext handlerContext) { handlerContext.Response = param.Result; return Task.Delay(1); diff --git a/test/ServiceAnt.test/InProcessEventBus_Test.cs b/test/ServiceAnt.test/InProcessEventBus_Test.cs index df99a0d..4ea9ff1 100644 --- a/test/ServiceAnt.test/InProcessEventBus_Test.cs +++ b/test/ServiceAnt.test/InProcessEventBus_Test.cs @@ -14,21 +14,22 @@ namespace YiBan.Common.BaseAbpModule.Tests.Events [TestClass] public class InProcessServiceBus_Test { - private class TestEventData : TransportTray + private class TestEventData : ITrigger { public string Msg { get; set; } } - private class TestEventDataWithParam : TransportTray + private class TestEventDataWithParam : ITrigger { public TestEventDataWithParam(string Msg) { } public string Msg { get; set; } } - private class TestEventDataT : TransportTray + private class TestEventDataT : Trigger { - public TestEventDataT(T test) : base(test) { } + public TestEventDataT(T test) : base(test) + { } public string Msg { get; set; } } @@ -431,14 +432,14 @@ public void GenericRequest_ShouldResponse() { return Task.Run(() => { - context.Response = eventData.TransportEntity.Msg + eventData.Msg; + context.Response = eventData.Dto.Msg + eventData.Msg; }); }); var testEventData = new TestEventDataT(new TestEventData() { Msg = "non" }) { Msg = "success" }; var result = eventBus.Send(testEventData); - Assert.AreEqual(testEventData.TransportEntity.Msg + testEventData.Msg, result); + Assert.AreEqual(testEventData.Dto.Msg + testEventData.Msg, result); } [TestMethod]