コード例 #1
0
 public static IBindingInNamedWithOrOnSyntax <object> WhenNotificationMatchesType <TNotification>(
     this IBindingWhenSyntax <object> syntax)
     where TNotification : INotification
 {
     return(syntax.When(request =>
                        typeof(TNotification).IsAssignableFrom(request.Service.GenericTypeArguments.Single())));
 }
コード例 #2
0
 // note: May be done better to check Request constraints, but for sample is fine
 public static IBindingInNamedWithOrOnSyntax <object> WhenNotificationMatchesType(
     this IBindingWhenSyntax <object> syntax, params Type[][] typeArgsVariants) =>
 syntax.When(request =>
 {
     var typeArgs = request.Service.GenericTypeArguments;
     return(typeArgsVariants.Any(closedTypeArgs => closedTypeArgs.Select((t, i) => t.IsAssignableFrom(typeArgs[i])).All(id => id)));
 });
 /// <summary>
 /// Condition that matches when the target has the given name.
 /// </summary>
 /// <typeparam name="T">The type of the binding.</typeparam>
 /// <param name="syntax">The syntax.</param>
 /// <param name="name">The name.</param>
 /// <returns>The syntax to define more things for the binding.</returns>
 public static IBindingInNamedWithOrOnSyntax <T> WhenTargetNamed <T>(this IBindingWhenSyntax <T> syntax, string name)
 {
     return(syntax.When(
                request =>
                request.Target != null &&
                request.Target.Name.ToUpper(CultureInfo.InvariantCulture) == name.ToUpper(CultureInfo.InvariantCulture)));
 }
コード例 #4
0
ファイル: WhenExtensions.cs プロジェクト: DnDGen/TreasureGen
        public static IBindingInNamedWithOrOnSyntax <T> WhenInjectedInto <T>(
            this IBindingWhenSyntax <T> syntax, Type parentType, string name)
        {
            var condition = ComputeMatchCondition <T>(syntax, parentType);

            return(syntax.When(request => condition(request) &&
                               request.ActiveBindings.Any(b => b.Metadata.Name == name)));
        }
コード例 #5
0
        public static IBindingInNamedWithOrOnSyntax <T> WhenInjectedInto <T>
        (
            this IBindingWhenSyntax <T> syntax,
            params Type[] types
        )
        {
            var conditions = ComputeMatchConditions(syntax, types).ToArray();

            return(syntax.When(request => conditions.Any(condition => condition(request))));
        }
コード例 #6
0
ファイル: Test.cs プロジェクト: BrunoJuchli/NinjectExamples
 public static IBindingInNamedWithOrOnSyntax <T> WhenFileCode <T>(
     this IBindingWhenSyntax <T> syntax,
     FileCode fileCode)
 {
     return(syntax.When(req => req
                        .Parameters
                        .OfType <FileCodeParameter>()
                        .Single()
                        .FileCode.Value == fileCode.Value));
 }
コード例 #7
0
 /// <summary>
 /// Specifies that the binding should be considered only when injecting into a constructor parameter with the given name.
 /// </summary>
 /// <typeparam name="T">The service type.</typeparam>
 /// <param name="builder">The fluent syntax.</param>
 /// <param name="parameterName">The name of the constructor parameter.</param>
 /// <returns>The fluent syntax.</returns>
 public static IBindingInNamedWithOrOnSyntax <T> InjectedIntoParameter <T>(this IBindingWhenSyntax <T> builder, string parameterName)
 {
     return(ProfilerInterceptor.GuardInternal(() =>
     {
         return builder.When(req =>
         {
             var target = req.Target as ParameterTarget;
             return req.Target == null || target != null && target.Site.Name == parameterName;
         });
     }));
 }
コード例 #8
0
        /// <summary>
        /// Specifies that the binding should be considered only when injecting into the given property.
        /// </summary>
        /// <typeparam name="T">The service type.</typeparam>
        /// <typeparam name="TTarget">The type defining the property.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="builder">The fluent syntax.</param>
        /// <param name="propertyExpr">An expression referencing the property, e.g. <code>(TransactionService s) => s.CreditCardService</code></param>
        /// <returns>The fluent syntax.</returns>
        public static IBindingInNamedWithOrOnSyntax <T> InjectedIntoProperty <T, TTarget, TProperty>(this IBindingWhenSyntax <T> builder, Expression <Func <TTarget, TProperty> > propertyExpr)
        {
            return(ProfilerInterceptor.GuardInternal(() =>
            {
                MemberInfo member = ((MemberExpression)propertyExpr.Body).Member;
                if (!(member is PropertyInfo))
                {
                    throw new ArgumentException("Expression should represent a property. Note that field injection is not supported.");
                }

                return builder.When(req =>
                {
                    var target = req.Target as PropertyTarget;
                    return req.Target == null || target != null && target.Site == member;
                });
            }));
        }
コード例 #9
0
 public static IBindingInNamedWithOrOnSyntax <T> WhenClassifiedBy <T>(this IBindingWhenSyntax <T> syntax, string classification)
 {
     return(syntax.When(request => request.IsValidForClassification(classification)));
 }
コード例 #10
0
 public static IBindingInNamedWithOrOnSyntax <T> WhenNoAnchestorNamed <T>(this IBindingWhenSyntax <T> syntax, string name)
 {
     return(syntax.When(r => !IsAnyAnchestorNamed(r, name)));
 }
コード例 #11
0
 public static IBindingInNamedWithOrOnSyntax <object> WhenEntityMatchesType <TEntityType>(
     this IBindingWhenSyntax <object> syntax)
 {
     return(syntax.When(request => DoesEntityMatchType(request, typeof(TEntityType))));
 }
コード例 #12
0
 public static IBindingInNamedWithOrOnSyntax <T> MakePreferredBinding <T>(
     this IBindingWhenSyntax <T> syntax)
 {
     return(syntax.When(req => true));
 }
コード例 #13
0
ファイル: Test.cs プロジェクト: BrunoJuchli/NinjectExamples
 public static IBindingInNamedWithOrOnSyntax <T> WhenIsDescendantOf <T>(this IBindingWhenSyntax <T> syntax, Type ancestor)
 {
     return(syntax.When(request => request.ActiveBindings.Any(p => p.Service.Name == ancestor.Name)));
 }
コード例 #14
0
 /// <summary>
 /// Indicates that the binding should be used only for requests that support the specified condition.
 /// </summary>
 /// <typeparam name="T">The type used in the binding.</typeparam>
 /// <param name="whenSyntax">The when syntax.</param>
 /// <param name="condition">The condition.</param>
 /// <returns>The syntax</returns>
 public static IBindingInNamedWithOrOnSyntax <T> WhenInjectedIntoActivity <T>(this IBindingWhenSyntax <T> whenSyntax, Func <Activity, bool> condition)
 {
     return(whenSyntax.When(request => request.HasRootActivityParameter() &&
                            condition(request.GetRootActivityParameter().Root)));
 }
コード例 #15
0
 public static IBindingInNamedWithOrOnSyntax <object> WhenSignalMatchesType <TSignal>(this IBindingWhenSyntax <object> syntax)
     where TSignal : ISignal
 {
     return(syntax.When(request => typeof(TSignal).IsAssignableFrom(request.Service.GenericTypeArguments.Single())));
 }
コード例 #16
0
 public static IBindingInNamedWithOrOnSyntax <T> WhenInjectedIntoDescendentOf <T>(this IBindingWhenSyntax <T> when, Type ancestor)
 {
     return(when.When(r => IsTypeRequestedByAncestor(r, ancestor)));
 }
コード例 #17
0
        public static IBindingInNamedWithOrOnSyntax <T> WhenInCurrentTest <T>(this IBindingWhenSyntax <T> binding)
        {
            var testName = TestContext.CurrentContext.Test.FullName;

            return(binding.When(w => TestContext.CurrentContext.Test.FullName == testName));
        }