From 3e27d5680956d85a67006bd48383b3a55d458c4c Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Mon, 5 Dec 2016 23:41:17 +0100 Subject: [PATCH] Some more interface foundations --- Connection/TapetiWorker.cs | 2 +- IConnection.cs | 9 +++ ITopology.cs | 20 +++++ Tapeti.csproj | 5 +- TapetiConnection.cs | 31 -------- TapetiConnectionBuilder.cs | 30 +++++++ TapetiConnectionExtensions.cs | 23 ------ TapetiTopologyBuilder.cs | 146 ++++++++++++++++++++++++++++++++++ Test/Program.cs | 15 ++-- Test/TestQueueController.cs | 1 - 10 files changed, 218 insertions(+), 64 deletions(-) create mode 100644 IConnection.cs create mode 100644 ITopology.cs create mode 100644 TapetiConnectionBuilder.cs delete mode 100644 TapetiConnectionExtensions.cs create mode 100644 TapetiTopologyBuilder.cs diff --git a/Connection/TapetiWorker.cs b/Connection/TapetiWorker.cs index c4b13ef..99c4b2f 100644 --- a/Connection/TapetiWorker.cs +++ b/Connection/TapetiWorker.cs @@ -16,7 +16,7 @@ namespace Tapeti.Connection private readonly IMessageSerializer messageSerializer; private readonly IRoutingKeyStrategy routingKeyStrategy; private readonly Lazy taskQueue = new Lazy(); - private IConnection connection; + private RabbitMQ.Client.IConnection connection; private IModel channel; diff --git a/IConnection.cs b/IConnection.cs new file mode 100644 index 0000000..92f5ce5 --- /dev/null +++ b/IConnection.cs @@ -0,0 +1,9 @@ +using System; + +namespace Tapeti +{ + public interface IConnection : IDisposable + { + ISubscriber Subscribe(); + } +} diff --git a/ITopology.cs b/ITopology.cs new file mode 100644 index 0000000..05c1934 --- /dev/null +++ b/ITopology.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace Tapeti +{ + public interface ITopology + { + IEnumerable Queues(); + } + + + public interface IQueue + { + IEnumerable Bindings(); + } + + + public interface IBinding + { + } +} diff --git a/Tapeti.csproj b/Tapeti.csproj index f81dac3..79e761d 100644 --- a/Tapeti.csproj +++ b/Tapeti.csproj @@ -59,10 +59,13 @@ + + - + + diff --git a/TapetiConnection.cs b/TapetiConnection.cs index a4139c9..7e36e20 100644 --- a/TapetiConnection.cs +++ b/TapetiConnection.cs @@ -60,37 +60,6 @@ namespace Tapeti } - public TapetiConnection RegisterController(Type type) - { - var queueAttribute = type.GetCustomAttribute(); - if (queueAttribute == null) - throw new ArgumentException("Queue attribute required on class", nameof(type)); - - if (queueAttribute.Dynamic) - { - if (!string.IsNullOrEmpty(queueAttribute.Name)) - throw new ArgumentException("Dynamic queue attributes must not have a Name"); - - registrations.Value.Add(new ControllerDynamicQueueRegistration( - DependencyResolver.Resolve, - DependencyResolver.Resolve, - type, SubscribeExchange)); - } - else - { - if (string.IsNullOrEmpty(queueAttribute.Name)) - throw new ArgumentException("Non-dynamic queue attribute must have a Name"); - - registrations.Value.Add(new ControllerQueueRegistration( - DependencyResolver.Resolve, - type, SubscribeExchange, queueAttribute.Name)); - } - - (DependencyResolver as IDependencyInjector)?.RegisterController(type); - return this; - } - - public async Task Subscribe() { if (!registrations.IsValueCreated || registrations.Value.Count == 0) diff --git a/TapetiConnectionBuilder.cs b/TapetiConnectionBuilder.cs new file mode 100644 index 0000000..0f71cfe --- /dev/null +++ b/TapetiConnectionBuilder.cs @@ -0,0 +1,30 @@ +using System; + +namespace Tapeti +{ + public class TapetiConnectionBuilder + { + public IConnection Build() + { + throw new NotImplementedException(); + } + + + public TapetiConnectionBuilder SetExchange(string exchange) + { + return this; + } + + + public TapetiConnectionBuilder SetDependencyResolver(IDependencyResolver dependencyResolver) + { + return this; + } + + + public TapetiConnectionBuilder SetTopology(ITopology topology) + { + return this; + } + } +} diff --git a/TapetiConnectionExtensions.cs b/TapetiConnectionExtensions.cs deleted file mode 100644 index 70c8b02..0000000 --- a/TapetiConnectionExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Linq; -using System.Reflection; -using Tapeti.Annotations; - -namespace Tapeti -{ - public static class TapetiConnectionExtensions - { - public static TapetiConnection RegisterAllControllers(this TapetiConnection connection, Assembly assembly) - { - foreach (var type in assembly.GetTypes().Where(t => t.IsDefined(typeof(DynamicQueueAttribute)))) - connection.RegisterController(type); - - return connection; - } - - - public static TapetiConnection RegisterAllControllers(this TapetiConnection connection) - { - return RegisterAllControllers(connection, Assembly.GetCallingAssembly()); - } - } -} diff --git a/TapetiTopologyBuilder.cs b/TapetiTopologyBuilder.cs new file mode 100644 index 0000000..aefe9dd --- /dev/null +++ b/TapetiTopologyBuilder.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Tapeti.Annotations; + + +namespace Tapeti +{ + public class TopologyConfigurationException : Exception + { + public TopologyConfigurationException(string message) : base(message) { } + } + + + public class TapetiTopologyBuilder + { + private readonly List registrations = new List(); + + + public ITopology Build() + { + throw new NotImplementedException(); + } + + + public TapetiTopologyBuilder RegisterController(Type controller) + { + var controllerRegistration = GetAttributesRegistration(controller); + + foreach (var method in controller.GetMembers(BindingFlags.Public | BindingFlags.Instance) + .Where(m => m.MemberType == MemberTypes.Method && m.DeclaringType != typeof(object)) + .Select(m => (MethodInfo)m)) + { + } + + /* + if (queueAttribute.Dynamic) + { + if (!string.IsNullOrEmpty(queueAttribute.Name)) + throw new ArgumentException("Dynamic queue attributes must not have a Name"); + + registrations.Value.Add(new ControllerDynamicQueueRegistration( + DependencyResolver.Resolve, + DependencyResolver.Resolve, + type, SubscribeExchange)); + } + else + { + if (string.IsNullOrEmpty(queueAttribute.Name)) + throw new ArgumentException("Non-dynamic queue attribute must have a Name"); + + registrations.Value.Add(new ControllerQueueRegistration( + DependencyResolver.Resolve, + type, SubscribeExchange, queueAttribute.Name)); + } + + (DependencyResolver as IDependencyInjector)?.RegisterController(type); + */ + return this; + } + + + public TapetiTopologyBuilder RegisterAllControllers(Assembly assembly) + { + foreach (var type in assembly.GetTypes().Where(t => t.IsDefined(typeof(MessageControllerAttribute)))) + RegisterController(type); + + return this; + } + + + public TapetiTopologyBuilder RegisterAllControllers() + { + return RegisterAllControllers(Assembly.GetCallingAssembly()); + } + + + protected HandlerRegistration GetAttributesRegistration(MemberInfo member) + { + var registration = new HandlerRegistration(); + + var dynamicQueueAttribute = member.GetCustomAttribute(); + var staticQueueAttribute = member.GetCustomAttribute(); + + if (dynamicQueueAttribute != null && staticQueueAttribute != null) + throw new TopologyConfigurationException($"Cannot combine static and dynamic queue attributes on {member.Name}"); + + if (dynamicQueueAttribute != null) + registration.Dynamic = true; + else if (staticQueueAttribute != null) + { + registration.Dynamic = false; + registration.QueueName = staticQueueAttribute.Name; + } + + return registration; + } + + + protected class HandlerRegistration + { + public bool? Dynamic { get; set; } + public string QueueName { get; set; } + } + + + protected class Topology : ITopology + { + private readonly List queues = new List(); + + + public void Add(Queue queue) + { + queues.Add(queue); + } + + public IEnumerable Queues() + { + return queues; + } + } + + + protected class Queue : IQueue + { + private readonly List bindings = new List(); + + + public void Add(Binding binding) + { + bindings.Add(binding); + } + + public IEnumerable Bindings() + { + return bindings; + } + } + + + protected class Binding : IBinding + { + } + } +} diff --git a/Test/Program.cs b/Test/Program.cs index d27f8c8..e20454d 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -11,13 +11,14 @@ namespace Test { var container = new Container(); - using (var connection = new TapetiConnection - { - PublishExchange = "test", - SubscribeExchange = "test" - } - .WithDependencyResolver(new SimpleInjectorDependencyResolver(container)) - .RegisterAllControllers(typeof(Program).Assembly)) + using (var connection = new TapetiConnectionBuilder() + .SetExchange("test") + .SetDependencyResolver(new SimpleInjectorDependencyResolver(container)) + .SetTopology( + new TapetiTopologyBuilder() + .RegisterAllControllers() + .Build()) + .Build()) { container.Register(); diff --git a/Test/TestQueueController.cs b/Test/TestQueueController.cs index 09f779b..c408bce 100644 --- a/Test/TestQueueController.cs +++ b/Test/TestQueueController.cs @@ -1,5 +1,4 @@ using System; -using System.Threading.Tasks; using Tapeti; using Tapeti.Annotations;