Exemplo n.º 1
0
        private static List <IAopHandler> GetHandlers(MethodInfo method, Type innerType, out bool hasExcpetions)
        {
            var types = method.GetParameters().Select(x => x.ParameterType).ToArray();
            var mi    = innerType.GetMethod(method.Name, types);

            var baseHandlers = innerType.GetCustomAttributes <AopHandler>().ToList();
            var hanlders     = mi?.GetCustomAttributes <AopHandler>().ToList() ?? new List <AopHandler>();

            foreach (var baseHandler in baseHandlers)
            {
                if (hanlders.All(x => x.GetType() != baseHandler.GetType()))
                {
                    hanlders.Add(baseHandler);
                }
            }

            hasExcpetions = false;

            foreach (var attr in hanlders)
            {
                var type = attr.GetType();
                if (type.GetMethod("OnException").DeclaringType == type)
                {
                    hasExcpetions = true;
                }
                var key = attr.GetType().GetHashCode() ^ attr.GetHashCode();
                HandlerCache.Add(key, attr);
            }

            return(hanlders.OrderBy(x => x.Order).OfType <IAopHandler>().ToList());
        }
Exemplo n.º 2
0
        protected override void Given()
        {
            var container = new Container();

            container.Register <IAttributePipelineOperator <BaseAttribute>, BaseAttributePO>();
            container.Register <IAttributePipelineOperator <DerivedAttribute>, DerivedAttributePO>();
            this.handlerCache = new HandlerCache(container);
        }
Exemplo n.º 3
0
        public void Get_WhenTypeIsAValueTypeAndIsMissing_ReturnsDefault()
        {
            var sut = new HandlerCache();

            var result = sut.Get(typeof(int));

            Assert.AreEqual(0, result);
        }
Exemplo n.º 4
0
        public void Get_WhenTypeIsAReferenceTypeAndIsMissing_ReturnsNull()
        {
            var sut = new HandlerCache();

            var result = sut.Get(typeof(string));

            Assert.IsNull(result);
        }
Exemplo n.º 5
0
        public virtual void OnAction()
        {
            IAction[] actions = HandlerCache <IAction> .GetAction(this);

            foreach (var action in actions)
            {
                action.Execute();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HandlerProcessor"/> class.
        /// </summary>
        /// <param name="methodCache">The method cache.</param>
        /// <param name="handlerCache">The handler cache.</param>
        /// <param name="handlers">The handlers.</param>
        public HandlerProcessor(MethodCache methodCache, HandlerCache handlerCache, IList<object> handlers)
        {
            if (methodCache == null) { throw new ArgumentNullException("methodCache"); }
            if (handlerCache == null) { throw new ArgumentNullException("handlerCache"); }
            if (handlers == null) { throw new ArgumentNullException("handlers"); }

            this.methodCache = methodCache;
            this.handlerCache = handlerCache;
            this.handlers = handlers;
        }
Exemplo n.º 7
0
        public void Get_WhenTypeIsAValueTypeAndIsCached_ReturnsValueMatchingType()
        {
            var value = 1;

            var sut = new HandlerCache();
            sut.Set(value);

            var result = sut.Get(typeof(int));

            Assert.AreEqual(value, result);
        }
Exemplo n.º 8
0
        public void GetMissing_WhenValueIsNull_ThrowsArgumentNullException()
        {
            TestHelper.Act(
                () =>
                {
                    var sut = new HandlerCache();

                    sut.GetMissing = null;
                })
                .ExpectArgumentNullException("value");
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IEnumerable <ClientRefInfo> GetClients()
        {
            var cacheKey = string.Concat(this.GetType().Name, "GetClients");

            return(HandlerCache.Get <List <ClientRefInfo> >(cacheKey, () =>
            {
                return _clientRepos
                .TableNoTracking
                .ToList();
            }));
        }
Exemplo n.º 10
0
        public void Get_WhenTypeIsAReferenceTypeAndIsCached_ReturnsValueMatchingType()
        {
            var value = "hello, world!";

            var sut = new HandlerCache();
            sut.Set(value);

            var result = sut.Get(typeof(string));

            Assert.AreEqual(value, result);
        }
Exemplo n.º 11
0
        public override void SetParams()
        {
            ID = 1;


            DataMove = HandlerCache <DataMove> .SetData <Move>(this, new DataMove(20));

            ActionMove = new ActionMove();
            ActionMove.SetBot(this);

            HandlerCache <ActionMove> .SetAction <Move>(this, ActionMove);
        }
Exemplo n.º 12
0
        public void Process_WhenMultipleHandlersAreCalled_CallsEachHandler()
        {
            var handlerCache = new HandlerCache();
            var message = "message";

            var sut =
                new HandlerProcessor(
                    new MethodCache(),
                    handlerCache,
                    new object[] { new MessageHandler(message), new ConvertToUpperCaseHandler() });

            sut.Process();

            Assert.AreEqual("MESSAGE", ((Result)handlerCache.Get(typeof(Result))).Value);
        }
Exemplo n.º 13
0
        public void Bootstrap()
        {
            Container.Global.RunAllTypeProcessors();
            Container.Global.RunAllRegistries();

            Container.Global.RemoveAllRegistrationsAndInstancesOf <IHandlerCache>();
            Container.Global.Register <IHandlerCache>(c =>
            {
                var cache = new HandlerCache(Container.Global);
                var types = this.GetType().Assembly.GetTypes().Where(t => typeof(IHandler).IsAssignableFrom(t)).ToArray();
                cache.Warm(types);
                return(cache);
            });

            BinderCollection.Initialize(Container.Global);
        }
Exemplo n.º 14
0
        public void Get_WhenTypeIsMissingAndGetMissingIsSpecified_ReturnsValueFromGetMissing()
        {
            TestHelper.Theory("type", new[] { typeof(int), typeof(string) })
                .Act(
                    type =>
                    {
                        var value = new object();

                        var sut = new HandlerCache();
                        sut.GetMissing = missingType => value;

                        var result = sut.Get(type);

                        Assert.AreEqual(value, result);
                    })
                .Verify();
        }
Exemplo n.º 15
0
        public void Processes_WhenCalledMultipleTimes_RegistersEachHandlerType()
        {
            const string instanceName = "empty";
            var handlerCache = new HandlerCache();
            handlerCache.Set(0);

            var sut = new Registry();

            sut.Register(instanceName)
                .Processes<CounterHandler>()
                .Processes<CounterHandler>();

            var container = new Container(sut);

            HandlerProcessor processor = container.With(handlerCache).GetInstance<HandlerProcessor>(instanceName);

            processor.Process();

            Assert.AreEqual(2, handlerCache.Get(typeof(int)));
        }
Exemplo n.º 16
0
        public void Processes_WhenHandlerTypeHasProcessMethod_RegistersHandlerType()
        {
            const string instanceName = "empty";
            var handlerCache = new HandlerCache();

            var sut = new Registry();

            sut.Register(instanceName)
                .Processes<EmptyHandler>();

            var container = new Container(sut);

            HandlerProcessor processor = container.With(handlerCache).GetInstance<HandlerProcessor>(instanceName);

            processor.Process();

            Assert.IsTrue(container.Model.For<HandlerProcessor>().HasImplementations(), "Missing implementations.");
            Assert.IsNotNull(container.Model.Find<HandlerProcessor>(instanceName));
            Assert.AreEqual("message", handlerCache.Get(typeof(string)));
        }
        private HandlerCache GetCacheInstance(string queue)
        {
            if (string.IsNullOrWhiteSpace(queue))
            {
                throw new ArgumentException("Queue name cannot be empty");
            }

            if (_cache.ContainsKey(queue))
            {
                return(_cache[queue]);
            }
            else
            {
                var serviceConnection = _connectionBuilder.GetServiceConnection(queue);
                if (serviceConnection == null)
                {
                    throw new ArgumentException($"No service connection found for queue {queue}");
                }

                var cache = new HandlerCache(queue, serviceConnection);
                _cache[queue] = cache;
                return(cache);
            }
        }
Exemplo n.º 18
0
        public void Get_WhenTypeIsReferenceTypeAndValueIsNull_ReturnsValueOfGetMissingFunction()
        {
            var value = new object();

            var sut = new HandlerCache();
            sut.GetMissing = missingType => value;
            sut.Set<string>(null);

            var result = sut.Get(typeof(string));

            Assert.AreEqual(value, result);
        }
Exemplo n.º 19
0
 /// <summary>
 /// Clears the cache
 /// </summary>
 public void Clear()
 {
     HandlerCache.Clear();
     TimeoutCache.Clear();
 }