public void Initialize()
        {
            this.settings = new FunctionGenerator.PhysicalAssemblySettings("TryCatch", "dll", AppDomain.CurrentDomain.BaseDirectory);

            this.functionGenerator = new FunctionGenerator(this.settings);
            this.provider = new TryCatchBlockProvider(this.functionGenerator);
        }
        /// <summary>
        /// Creates an instance of <see cref="IBoilerplateContext"/>
        /// </summary>
        /// <param name="identity">The current identity being used (rights and roles contract requirements/restrictions will apply to this identity)</param>
        /// <param name="accessProvider">An access provider for specific types (available through IBoilerplateContext.Open&lt;T&gt;())</param>
        /// <param name="permissionsProvider">The provider that will be used for all permissions verification attempts</param>
        /// <param name="visibility">The visibility level that this context has. This will affect operations that rely on visibility (e.g. logging).</param>
        /// <returns>An instance of <see cref="IBoilerplateContext"/></returns>
        public static IBoilerplateContext New(IIdentity identity = null, 
                                              ITypeAccessProvider accessProvider = null, 
                                              IPermissionsProvider permissionsProvider = null,
                                              Visibility visibility = Visibility.None)
        {   
            var actualIdentity = identity ?? Identity.Default;
            var actualTypeAccessProvider = accessProvider ?? TypeAccessProvider.Empty;
            var actualPermissionsProvider = permissionsProvider ?? PermissionsProvider.Default;

            var functionGenerator = new FunctionGenerator();

            //Core providers
            var translationProvider = new TranslationProvider(functionGenerator);
            var validationProvider = new ValidationProvider(functionGenerator);
            var logProvider = new LogProvider(functionGenerator, visibility);

            //Set up error handling
            var tryCatchProvider = new TryCatchBlockProvider(functionGenerator);
            var exceptionHandlerProvider = new ExceptionHandlerProvider(logProvider);
            var errorContext = new ImmutableErrorContext(logProvider, tryCatchProvider, exceptionHandlerProvider);

            var bundle = new ContextBundle(permissionsProvider: actualPermissionsProvider,
                                           errorContext: errorContext,
                                           translationProvider: translationProvider,
                                           accessProvider: actualTypeAccessProvider,
                                           validationProvider: validationProvider,
                                           logProvider: logProvider,
                                           visibility: visibility);

            return new InitialBoilerplateContext<ContractContext>(bundle, actualIdentity);
        }
Example #3
0
        public void ThrownArgumentExceptionWithTwoBlocksWillBeHandled()
        {
            var message = "Error message";

            var handledExceptionTypes = ImmutableQueue <Type> .Empty
                                        .Enqueue(typeof(ArgumentException))
                                        .Enqueue(typeof(Exception));

            var retryCount = 0;
            var handler    = new Mock <IVoidReturnExceptionHandler <ArgumentException> >(MockBehavior.Strict);

            handler.Setup(x => x.Handle(It.Is <ArgumentException>(p => p.Message == message)));
            handler.Setup(x => x.RetryCount).Returns(retryCount);

            var handlerProvider = new Mock <IExceptionHandlerProvider>(MockBehavior.Strict);

            handlerProvider.Setup(x => x.TryGetHandler <ArgumentException>()).Returns(handler.Object);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider  = new TryCatchBlockProvider(generator);
            var tryCatch  = provider.GetTryCatchFor(handlerProvider.Object);

            tryCatch.Try(() => { throw new ArgumentException(message); });
        }
Example #4
0
        public void ExceptionHandlerCanReturnResult()
        {
            var message = "Error message";

            var handledExceptionTypes = ImmutableQueue <Type> .Empty
                                        .Enqueue(typeof(Exception));

            var retryCount = 0;
            var handler    = new Mock <IResultExceptionHandler <Exception, int> >(MockBehavior.Strict);

            handler.Setup(x => x.RetryCount).Returns(retryCount);
            handler.Setup(x => x.Handle(It.Is <Exception>(p => p.Message == message))).Returns(5);

            var handlerProvider = new Mock <IExceptionHandlerProvider>(MockBehavior.Strict);

            handlerProvider.Setup(x => x.TryGetHandler <Exception, int>()).Returns(handler.Object);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider  = new TryCatchBlockProvider(generator);
            var tryCatch  = provider.GetTryCatchFor(handlerProvider.Object);

            var result = tryCatch.Try <int>(() => { throw new Exception(message); });

            result.Should().Be(5, "because that's what the handler returned");
        }
Example #5
0
        /// <summary>
        /// Creates an instance of <see cref="IBoilerplateContext"/>
        /// </summary>
        /// <param name="identity">The current identity being used (rights and roles contract requirements/restrictions will apply to this identity)</param>
        /// <param name="accessProvider">An access provider for specific types (available through IBoilerplateContext.Open&lt;T&gt;())</param>
        /// <param name="permissionsProvider">The provider that will be used for all permissions verification attempts</param>
        /// <param name="visibility">The visibility level that this context has. This will affect operations that rely on visibility (e.g. logging).</param>
        /// <returns>An instance of <see cref="IBoilerplateContext"/></returns>
        public static IBoilerplateContext New(IIdentity identity = null,
                                              ITypeAccessProvider accessProvider       = null,
                                              IPermissionsProvider permissionsProvider = null,
                                              Visibility visibility = Visibility.None)
        {
            var actualIdentity            = identity ?? Identity.Default;
            var actualTypeAccessProvider  = accessProvider ?? TypeAccessProvider.Empty;
            var actualPermissionsProvider = permissionsProvider ?? PermissionsProvider.Default;

            var functionGenerator = new FunctionGenerator();

            //Core providers
            var translationProvider = new TranslationProvider(functionGenerator);
            var validationProvider  = new ValidationProvider(functionGenerator);
            var logProvider         = new LogProvider(functionGenerator, visibility);

            //Set up error handling
            var tryCatchProvider         = new TryCatchBlockProvider(functionGenerator);
            var exceptionHandlerProvider = new ExceptionHandlerProvider(logProvider);
            var errorContext             = new ImmutableErrorContext(logProvider, tryCatchProvider, exceptionHandlerProvider);

            var bundle = new ContextBundle(permissionsProvider: actualPermissionsProvider,
                                           errorContext: errorContext,
                                           translationProvider: translationProvider,
                                           accessProvider: actualTypeAccessProvider,
                                           validationProvider: validationProvider,
                                           logProvider: logProvider,
                                           visibility: visibility);

            return(new InitialBoilerplateContext <ContractContext>(bundle, actualIdentity));
        }
        public void Initialize()
        {
            this.settings = new FunctionGenerator.PhysicalAssemblySettings("TryCatch", "dll", AppDomain.CurrentDomain.BaseDirectory);

            this.functionGenerator = new FunctionGenerator(this.settings);
            this.provider          = new TryCatchBlockProvider(this.functionGenerator);
        }
Example #7
0
        public void ThrownArgumentNullExceptionWithArgumentExceptionBlockWillNotHandled()
        {
            var message = "Error message";

            var handledExceptionTypes = ImmutableQueue <Type> .Empty
                                        .Enqueue(typeof(ArgumentNullException));

            var handler = new Mock <IExceptionHandler <ArgumentNullException> >(MockBehavior.Strict);

            var handlerProvider = new Mock <IExceptionHandlerProvider>(MockBehavior.Strict);

            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider  = new TryCatchBlockProvider(generator);
            var tryCatch  = provider.GetTryCatchFor(handlerProvider.Object);

            tryCatch.Try(() => { throw new ArgumentException(message); });
        }
Example #8
0
        public void TryBlockBodyCanReturnResult()
        {
            var handledExceptionTypes = ImmutableQueue <Type> .Empty
                                        .Enqueue(typeof(Exception));

            var handler = new Mock <IResultExceptionHandler <Exception, int> >(MockBehavior.Strict);

            var handlerProvider = new Mock <IExceptionHandlerProvider>(MockBehavior.Strict);

            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider  = new TryCatchBlockProvider(generator);
            var tryCatch  = provider.GetTryCatchFor(handlerProvider.Object);

            var result = tryCatch.Try <int>(() => 5);

            result.Should().Be(5, "because that's what the handler returned");
        }
Example #9
0
        public void ThrownExceptionWillBeHandled()
        {
            var message = "Error message";

            var handledExceptionTypes = ImmutableQueue<Type>.Empty.Enqueue(typeof(Exception));

            var retryCount = 0;
            var handler = new Mock<IVoidReturnExceptionHandler<Exception>>(MockBehavior.Strict);
            handler.Setup(x => x.Handle(It.Is<Exception>(p => p.Message == message)));
            handler.Setup(x => x.RetryCount).Returns(retryCount);

            var handlerProvider = new Mock<IExceptionHandlerProvider>(MockBehavior.Strict);
            handlerProvider.Setup(x => x.TryGetHandler<Exception>()).Returns(handler.Object);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider = new TryCatchBlockProvider(generator);
            var tryCatch = provider.GetTryCatchFor(handlerProvider.Object);

            tryCatch.Try(() => { throw new Exception(message); });
        }
Example #10
0
        public void ExceptionHandlerCanProvokeRetry()
        {
            var handledExceptionTypes = ImmutableQueue <Type> .Empty
                                        .Enqueue(typeof(Exception));

            var retryCount = 5;
            var handler    = new Mock <IResultExceptionHandler <Exception, int> >(MockBehavior.Strict);

            handler.Setup(x => x.RetryCount).Returns(retryCount);
            handler.Setup(x => x.RetryIntervalInMilliseconds).Returns(0);

            var handlerProvider = new Mock <IExceptionHandlerProvider>(MockBehavior.Strict);

            handlerProvider.Setup(x => x.TryGetHandler <Exception, int>()).Returns(handler.Object);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider  = new TryCatchBlockProvider(generator);
            var tryCatch  = provider.GetTryCatchFor(handlerProvider.Object);

            var numberOfTries = 0;
            var result        = tryCatch.Try <int>(() =>
            {
                numberOfTries++;

                if (numberOfTries < retryCount)
                {
                    throw new Exception();
                }
                return(5);
            });

            result.Should().Be(5, "because that's what the handler returned");
            numberOfTries.Should().Be(retryCount, "because that's the number of retries requested");
        }
Example #11
0
        public void ThrownArgumentNullExceptionWithArgumentExceptionBlockWillNotHandled()
        {
            var message = "Error message";

            var handledExceptionTypes = ImmutableQueue<Type>.Empty
                .Enqueue(typeof(ArgumentNullException));

            var handler = new Mock<IExceptionHandler<ArgumentNullException>>(MockBehavior.Strict);

            var handlerProvider = new Mock<IExceptionHandlerProvider>(MockBehavior.Strict);            
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider = new TryCatchBlockProvider(generator);
            var tryCatch = provider.GetTryCatchFor(handlerProvider.Object);

            tryCatch.Try(() => { throw new ArgumentException(message); });
        }
Example #12
0
        public void ExceptionHandlerCanProvokeRetry()
        {
            var handledExceptionTypes = ImmutableQueue<Type>.Empty
                .Enqueue(typeof(Exception));

            var retryCount = 5;
            var handler = new Mock<IResultExceptionHandler<Exception, int>>(MockBehavior.Strict);
            handler.Setup(x => x.RetryCount).Returns(retryCount);
            handler.Setup(x => x.RetryIntervalInMilliseconds).Returns(0);
                        
            var handlerProvider = new Mock<IExceptionHandlerProvider>(MockBehavior.Strict);
            handlerProvider.Setup(x => x.TryGetHandler<Exception, int>()).Returns(handler.Object);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider = new TryCatchBlockProvider(generator);
            var tryCatch = provider.GetTryCatchFor(handlerProvider.Object);

            var numberOfTries = 0;
            var result = tryCatch.Try<int>(() =>
                {
                    numberOfTries++;

                    if (numberOfTries < retryCount)
                        throw new Exception();
                    return 5;
                });

            result.Should().Be(5, "because that's what the handler returned");
            numberOfTries.Should().Be(retryCount, "because that's the number of retries requested");
        }
Example #13
0
        public void TryBlockBodyCanReturnResult()
        {
            var handledExceptionTypes = ImmutableQueue<Type>.Empty
                .Enqueue(typeof(Exception));

            var handler = new Mock<IResultExceptionHandler<Exception, int>>(MockBehavior.Strict);

            var handlerProvider = new Mock<IExceptionHandlerProvider>(MockBehavior.Strict);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider = new TryCatchBlockProvider(generator);
            var tryCatch = provider.GetTryCatchFor(handlerProvider.Object);

            var result = tryCatch.Try<int>(() => 5);

            result.Should().Be(5, "because that's what the handler returned");
        }
Example #14
0
        public void ExceptionHandlerCanReturnResult()
        {
            var message = "Error message";

            var handledExceptionTypes = ImmutableQueue<Type>.Empty
                .Enqueue(typeof(Exception));

            var retryCount = 0;
            var handler = new Mock<IResultExceptionHandler<Exception, int>>(MockBehavior.Strict);
            handler.Setup(x => x.RetryCount).Returns(retryCount);
            handler.Setup(x => x.Handle(It.Is<Exception>(p => p.Message == message))).Returns(5);
            
            var handlerProvider = new Mock<IExceptionHandlerProvider>(MockBehavior.Strict);
            handlerProvider.Setup(x => x.TryGetHandler<Exception, int>()).Returns(handler.Object);
            handlerProvider.Setup(x => x.HandledTypesInCatchOrder).Returns(handledExceptionTypes);
            handlerProvider.Setup(x => x.HandledExceptionTypes).Returns(handledExceptionTypes.ToImmutableHashSet());

            var generator = new FunctionGenerator();
            var provider = new TryCatchBlockProvider(generator);
            var tryCatch = provider.GetTryCatchFor(handlerProvider.Object);

            var result = tryCatch.Try<int>(() => { throw new Exception(message); });

            result.Should().Be(5, "because that's what the handler returned");
        }