public void CheckCanCreate() { // Setup var factory = new FuncFactory<ServiceBase>(() => new ServiceWithDefaultConstructor()); // Assert Assert.IsTrue(factory.CanCreate); }
public void CheckDependencyComplexity() { // Setup var factory = new FuncFactory<ServiceBase>(() => new ServiceWithDefaultConstructor()); // Assert Assert.AreEqual(0, factory.DependencyComplexity); }
public void CheckDependencies() { // Setup var factory = new FuncFactory<ServiceBase>(() => new ServiceWithDefaultConstructor()); // Assert Assert.IsNotNull(factory.Dependencies); Assert.AreEqual(0, factory.Dependencies.Length); }
public void FulfillFuncFactory() { // Setup var factory = new FuncFactory<ServiceBase>(() => new ServiceWithDefaultConstructor()); var container = new Container(); // Execute var result = factory.Fulfill(container); // Assert Assert.IsTrue(result); }
public void ResolveServiceInstance() { // Setup var factory = new FuncFactory<ServiceBase>(() => new ServiceWithDefaultConstructor()); // Execute var result = factory.Create(); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof (ServiceWithDefaultConstructor)); }
public static T Trinary <T, X, Y, Z>(this FuncFactory <T> factory, Action <Stack, X, Y, Z> func) { return(factory.Operation((stack) => { var z = (Z)stack.Pop(); var y = (Y)stack.Pop(); var x = (X)stack.Pop(); func(stack, x, y, z); }, new [] { typeof(X), typeof(Y), typeof(Z) }, Type.EmptyTypes)); }
private static bool TryExecuteFunctionCore <TTuple, TResult>(FuncFactory <TTuple, TResult> factory, out TResult result) where TTuple : Template { try { result = factory.ExecuteMethod(); return(true); } catch (Exception) { result = default(TResult); return(false); } }
private ExceptionPropertyContinuation <TContinuationValue> CreateFor <TContinuationValue>( Func <T, TContinuationValue> propertyValueFetcher ) { var fetcher = FuncFactory.Memoize( () => propertyValueFetcher(Actual) ); return(ContinuationFactory.Create <TContinuationValue, ExceptionPropertyContinuation <TContinuationValue> >( fetcher, new WrappingContinuation <Exception, TContinuationValue>( this, c => fetcher() ) )); }
public IExceptionCollectionPropertyContinuation <TItem> CollectionProperty <TItem>( Func <T, IEnumerable <TItem> > propertyValueFetcher ) { var fetcher = FuncFactory.Memoize( () => propertyValueFetcher(Actual) ); return(ContinuationFactory.Create <IEnumerable <TItem>, ExceptionCollectionPropertyContinuation <TItem> >( fetcher, new WrappingContinuation <Exception, IEnumerable <TItem> >( this, c => fetcher() ) )); }
private static IEnumerable <TSource> FindAllCore <TTuple, TSource>(FuncFactory <TTuple, bool> factory, IEnumerable <TSource> source) where TTuple : Template <TSource> { List <TSource> temp = new List <TSource>(); using (IEnumerator <TSource> enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { TSource current = enumerator.Current; factory.GenericArguments.Arg1 = current; if (factory.ExecuteMethod()) { temp.Add(current); } } } return(temp); }
public static void Main(string[] args) { var people = new[] { new Person { FirstName = "Hello", LastName = "World" }, new Person { FirstName = "Foo", LastName = "Bar" }, }; Console.WriteLine(people.Where(FuncFactory.GetFilterFunc <Person>(FilterType.Contains, x => x.FirstName, "ello")).Any()); Console.WriteLine(people.Where(FuncFactory.GetFilterFunc <Person>(FilterType.Equals, x => x.FirstName, "ello")).Any()); Console.WriteLine(people.Where(FuncFactory.GetFilterFunc <Person>(FilterType.Contains, x => x.LastName, "ar")).Any()); Console.WriteLine(people.Where(FuncFactory.GetFilterFunc <Person>(FilterType.Equals, x => x.LastName, "ar")).Any()); Console.ReadKey(); }
public void ShouldMemoize() { // Arrange var calls = 0; Func <string> generator = () => { calls++; return(GetRandomString()); }; var sut = FuncFactory.Memoize(generator); // Act var result1 = sut(); var result2 = sut(); // Assert Expect(calls) .To.Equal(1); Expect(result1) .To.Equal(result2); }
/// <summary> /// Returns a value that indicates whether the specified <paramref name="method"/> can be invoked without an exception. /// </summary> /// <typeparam name="T1">The type of the first parameter of the method.</typeparam> /// <typeparam name="T2">The type of the second parameter of the method.</typeparam> /// <typeparam name="T3">The type of the third parameter of the method.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the method.</typeparam> /// <typeparam name="TResult">The type of the return value of the method.</typeparam> /// <param name="method">The function delegate to invoke to try and get the <typeparamref name="TResult"/>.</param> /// <param name="arg1">The first parameter of the <paramref name="method"/>.</param> /// <param name="arg2">The second parameter of the <paramref name="method"/>.</param> /// <param name="arg3">The third parameter of the <paramref name="method"/>.</param> /// <param name="arg4">The fourth parameter of the <paramref name="method"/>.</param> /// <param name="result">When this method returns, contains the <typeparamref name="TResult"/> from <paramref name="method"/>, or <b>default</b>(<typeparamref name="TResult"/>) if an exception is thrown.</param> /// <returns><c>true</c> if an instance of <typeparamref name="TResult"/> has been created; otherwise <c>false</c>.</returns> public static bool TryExecuteFunction <T1, T2, T3, T4, TResult>(Func <T1, T2, T3, T4, TResult> method, T1 arg1, T2 arg2, T3 arg3, T4 arg4, out TResult result) { var factory = FuncFactory.Create(method, arg1, arg2, arg3, arg4); return(TryExecuteFunctionCore(factory, out result)); }
internal static TResult ParseCore <TTuple, TSource, TResult>(FuncFactory <TTuple, TResult> factory, TSource source) where TTuple : Template <TSource> { factory.GenericArguments.Arg1 = source; return(factory.ExecuteMethod()); }
/// <summary> /// Returns a value that indicates whether the specified <paramref name="method"/> can be invoked without an exception. /// </summary> /// <typeparam name="T">The type of the first parameter of the method.</typeparam> /// <typeparam name="TResult">The type of the return value of the method.</typeparam> /// <param name="method">The function delegate to invoke to try and get the <typeparamref name="TResult"/>.</param> /// <param name="arg">The first parameter of the <paramref name="method"/>.</param> /// <param name="result">When this method returns, contains the <typeparamref name="TResult"/> from <paramref name="method"/>, or <b>default</b>(<typeparamref name="TResult"/>) if an exception is thrown.</param> /// <returns><c>true</c> if an instance of <typeparamref name="TResult"/> has been created; otherwise <c>false</c>.</returns> public static bool TryExecuteFunction <T, TResult>(Func <T, TResult> method, T arg, out TResult result) { var factory = FuncFactory.Create(method, arg); return(TryExecuteFunctionCore(factory, out result)); }
/// <summary> /// Creates an instance of <typeparamref name="TResult" /> using a constructor of one parameters. /// </summary> /// <typeparam name="T">The type of the parameter of the constructor.</typeparam> /// <typeparam name="TResult">The type to create.</typeparam> /// <param name="arg">The parameter of the constructor.</param> /// <returns>A reference to the newly created object.</returns> public static TResult CreateInstance <T, TResult>(T arg) { var factory = FuncFactory.Create <T, TResult>(null, arg); return(CreateInstanceCore(factory)); }
/// <summary> /// Retrieves all the elements that match the conditions defined by the specified function delegate. /// </summary> /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam> /// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T5">The type of the fifth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <param name="source">The sequence to search.</param> /// <param name="match">The function delegate that defines the conditions of the elements to search for.</param> /// <param name="arg1">The first parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg2">The second parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg3">The third parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg4">The fourth parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg5">The fifth parameter of the function delegate <paramref name="match"/>.</param> /// <returns>An <see cref="IEnumerable{T}"/> sequence containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty <see cref="IEnumerable{T}"/>.</returns> public static IEnumerable <TSource> FindAll <TSource, T1, T2, T3, T4, T5>(IEnumerable <TSource> source, Func <TSource, T1, T2, T3, T4, T5, bool> match, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { var factory = FuncFactory.Create(match, default(TSource), arg1, arg2, arg3, arg4, arg5); return(FindAllCore(factory, source)); }
/// <summary> /// Repetitively executes the specified <paramref name="faultSensitiveMethod"/> until the operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition. /// </summary> /// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <param name="faultSensitiveMethod">The fault sensitive function delegate that is invoked until an operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition.</param> /// <param name="arg1">The first parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg2">The second parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg3">The third parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg4">The fourth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="setup">The <see cref="TransientOperationOptions"/> which need to be configured.</param> /// <returns>The result from the <paramref name="faultSensitiveMethod"/>.</returns> public static TResult WithFunc <T1, T2, T3, T4, TResult>(Func <T1, T2, T3, T4, TResult> faultSensitiveMethod, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Action <TransientOperationOptions> setup = null) { var factory = FuncFactory.Create(faultSensitiveMethod, arg1, arg2, arg3, arg4); return(WithFuncCore(factory, setup)); }
public static FuncFactory <Y> Compose <X, Y>(this FuncFactory <X> factory, Func <X, Y> converter) { return(new FuncFactoryAdapter <X, Y>(factory, converter)); }
/// <summary> /// Repetitively executes the specified <paramref name="faultSensitiveMethod"/> until the operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition. /// </summary> /// <typeparam name="T">The type of the parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <param name="faultSensitiveMethod">The fault sensitive function delegate that is invoked until an operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition.</param> /// <param name="arg">The parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="setup">The <see cref="TransientOperationOptions"/> which need to be configured.</param> /// <returns>The result from the <paramref name="faultSensitiveMethod"/>.</returns> public static TResult WithFunc <T, TResult>(Func <T, TResult> faultSensitiveMethod, T arg, Action <TransientOperationOptions> setup = null) { var factory = FuncFactory.Create(faultSensitiveMethod, arg); return(WithFuncCore(factory, setup)); }
/// <summary> /// Creates an instance of <typeparamref name="TResult" /> using a constructor of four parameters. /// </summary> /// <typeparam name="T1">The type of the first parameter of the constructor.</typeparam> /// <typeparam name="T2">The type of the second parameter of the constructor.</typeparam> /// <typeparam name="T3">The type of the third parameter of the constructor.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the constructor.</typeparam> /// <typeparam name="TResult">The type to create.</typeparam> /// <param name="arg1">The first parameter of the constructor.</param> /// <param name="arg2">The second parameter of the constructor.</param> /// <param name="arg3">The third parameter of the constructor.</param> /// <param name="arg4">The fourth parameter of the constructor.</param> /// <returns>A reference to the newly created object.</returns> public static TResult CreateInstance <T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4) { var factory = FuncFactory.Create <T1, T2, T3, T4, TResult>(null, arg1, arg2, arg3, arg4); return(CreateInstanceCore(factory)); }
public FuncFactoryAdapter(FuncFactory <S> factory, Func <S, T> converter) { this.factory = factory; this.converter = converter; }
public CacheAsyncState <TResult> With <TTuple>(FuncFactory <TTuple, TResult> method) where TTuple : Template { EndInvoke = method.EndExecuteMethod; return(this); }
/// <summary> /// Creates an instance of <typeparamref name="TResult" /> using a constructor of ten parameters. /// </summary> /// <typeparam name="T1">The type of the first parameter of the constructor.</typeparam> /// <typeparam name="T2">The type of the second parameter of the constructor.</typeparam> /// <typeparam name="T3">The type of the third parameter of the constructor.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the constructor.</typeparam> /// <typeparam name="T5">The type of the fifth parameter of the constructor.</typeparam> /// <typeparam name="T6">The type of the sixth parameter of the constructor.</typeparam> /// <typeparam name="T7">The type of the seventh parameter of the constructor.</typeparam> /// <typeparam name="T8">The type of the eighth parameter of the constructor.</typeparam> /// <typeparam name="T9">The type of the ninth parameter of the constructor.</typeparam> /// <typeparam name="T10">The type of the tenth parameter of the constructor.</typeparam> /// <typeparam name="TResult">The type to create.</typeparam> /// <param name="arg1">The first parameter of the constructor.</param> /// <param name="arg2">The second parameter of the constructor.</param> /// <param name="arg3">The third parameter of the constructor.</param> /// <param name="arg4">The fourth parameter of the constructor.</param> /// <param name="arg5">The fifth parameter of the constructor.</param> /// <param name="arg6">The sixth parameter of the constructor.</param> /// <param name="arg7">The seventh parameter of the constructor.</param> /// <param name="arg8">The eighth parameter of the constructor.</param> /// <param name="arg9">The ninth parameter of the constructor.</param> /// <param name="arg10">The tenth parameter of the constructor.</param> /// <returns>A reference to the newly created object.</returns> public static TResult CreateInstance <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) { var factory = FuncFactory.Create <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(null, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); return(CreateInstanceCore(factory)); }
/// <summary> /// Retrieves all the elements that match the conditions defined by the specified function delegate. /// </summary> /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam> /// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="match"/>.</typeparam> /// <param name="source">The sequence to search.</param> /// <param name="match">The function delegate that defines the conditions of the elements to search for.</param> /// <param name="arg1">The first parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg2">The second parameter of the function delegate <paramref name="match"/>.</param> /// <returns>An <see cref="IEnumerable{T}"/> sequence containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty <see cref="IEnumerable{T}"/>.</returns> public static IEnumerable <TSource> FindAll <TSource, T1, T2>(IEnumerable <TSource> source, Func <TSource, T1, T2, bool> match, T1 arg1, T2 arg2) { var factory = FuncFactory.Create(match, default(TSource), arg1, arg2); return(FindAllCore(factory, source)); }
private static TResult CreateInstanceCore <TTuple, TResult>(FuncFactory <TTuple, TResult> factory) where TTuple : Template { return((TResult)Activator.CreateInstance(typeof(TResult), factory.GenericArguments.ToArray())); }
private static TResult WithFuncCore <TTuple, TResult>(FuncFactory <TTuple, TResult> factory, Action <TransientOperationOptions> setup) where TTuple : Template { var options = setup.ConfigureOptions(); if (!options.EnableRecovery) { return(factory.ExecuteMethod()); } DateTime timestamp = DateTime.UtcNow; TimeSpan latency = TimeSpan.Zero; TimeSpan totalWaitTime = TimeSpan.Zero; TimeSpan lastWaitTime = TimeSpan.Zero; bool isTransientFault = false; bool throwExceptions; List <Exception> aggregatedExceptions = new List <Exception>(); TResult result = default(TResult); for (int attempts = 0; ;) { bool exceptionThrown = false; TimeSpan waitTime = options.RetryStrategy(attempts); try { if (latency > options.MaximumAllowedLatency) { throw new LatencyException(string.Format(CultureInfo.InvariantCulture, "The latency of the operation exceeded the allowed maximum value of {0} seconds. Actual latency was: {1} seconds.", options.MaximumAllowedLatency.TotalSeconds, latency.TotalSeconds)); } return(factory.ExecuteMethod()); } catch (Exception ex) { try { lock (aggregatedExceptions) { aggregatedExceptions.Insert(0, ex); } isTransientFault = options.DetectionStrategy(ex); if (attempts >= options.RetryAttempts) { throw; } if (!isTransientFault) { throw; } lastWaitTime = waitTime; totalWaitTime = totalWaitTime.Add(waitTime); attempts++; Sleep(waitTime); latency = DateTime.UtcNow.Subtract(timestamp).Subtract(totalWaitTime); } catch (Exception) { throwExceptions = true; exceptionThrown = true; if (isTransientFault) { var evidence = new TransientFaultEvidence(attempts, lastWaitTime, totalWaitTime, latency, new MethodDescriptor(factory.DelegateInfo).ToString()); aggregatedExceptions.InsertTransientFaultException(evidence); FaultCallback?.Invoke(evidence); } break; } } finally { if (exceptionThrown) { IDisposable disposable = result as IDisposable; disposable?.Dispose(); } } } if (throwExceptions) { throw new AggregateException(aggregatedExceptions); } return(result); }
private static TimeMeasureProfiler <TResult> WithFunctionCore <TTuple, TResult>(FuncFactory <TTuple, TResult> factory, Action <TimeMeasureOptions> setup) where TTuple : Template { var options = setup.ConfigureOptions(); var descriptor = options.MethodDescriptor?.Invoke() ?? new MethodDescriptor(factory.DelegateInfo); var profiler = new TimeMeasureProfiler <TResult>() { Member = descriptor.ToString(), Data = descriptor.MergeParameters(options.RuntimeParameters ?? factory.GenericArguments.ToArray()) }; PerformTimeMeasuring(profiler, options, p => p.Result = factory.ExecuteMethod()); return(profiler); }
/// <summary> /// Retrieves all the elements that match the conditions defined by the specified function delegate. /// </summary> /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam> /// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T5">The type of the fifth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T6">The type of the sixth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T7">The type of the seventh parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T8">The type of the eighth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <typeparam name="T9">The type of the ninth parameter of the function delegate <paramref name="match"/>.</typeparam> /// <param name="source">The sequence to search.</param> /// <param name="match">The function delegate that defines the conditions of the elements to search for.</param> /// <param name="arg1">The first parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg2">The second parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg3">The third parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg4">The fourth parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg5">The fifth parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg6">The sixth parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg7">The seventh parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg8">The eighth parameter of the function delegate <paramref name="match"/>.</param> /// <param name="arg9">The ninth parameter of the function delegate <paramref name="match"/>.</param> /// <returns>An <see cref="IEnumerable{T}"/> sequence containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty <see cref="IEnumerable{T}"/>.</returns> public static IEnumerable <TSource> FindAll <TSource, T1, T2, T3, T4, T5, T6, T7, T8, T9>(IEnumerable <TSource> source, Func <TSource, T1, T2, T3, T4, T5, T6, T7, T8, T9, bool> match, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { var factory = FuncFactory.Create(match, default(TSource), arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); return(FindAllCore(factory, source)); }