コード例 #1
0
        public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding, IBindingToSyntax <Func <string, ILog> > logFactory)
        {
            logBinding.To <ConsoleLog>();

            logFactory.ToMethod(c => caller => c.Kernel.Get <ConsoleLog>());
            eventPublisherBinding.To <NullEventPublisher>();
        }
        public static void Convert(Func <LambdaExpression> expression, IBindingToSyntax syntax)
        {
            Should.NotBeNull(expression, "expression");
            var hasClosure = expression.HasClosure();

            if (hasClosure || syntax.Builder.Contains(BindingBuilderConstants.NoCache))
            {
                var ex = expression();
                if (hasClosure)
                {
                    ex.TraceClosureWarn();
                }
                Convert(ex, syntax);
                return;
            }
            Action <IBindingToSyntax>[] callbacks;
            lock (Cache)
            {
                if (!Cache.TryGetValue(expression, out callbacks))
                {
                    callbacks         = ConvertInternal(expression(), syntax, false);
                    Cache[expression] = callbacks;
                }
            }
            for (int i = 0; i < callbacks.Length; i++)
            {
                callbacks[i].Invoke(syntax);
            }
        }
コード例 #3
0
        /// <summary>
        /// Indicates that the service should be bound to a mocked instance of the specified type.
        /// </summary>
        /// <typeparam name="T">The service that is being mocked.</typeparam>
        /// <param name="builder">The builder that is building the binding.</param>
        /// <returns>The syntax for adding more information to the binding.</returns>
        public static IBindingWhenInNamedWithOrOnSyntax <T> ToMock <T>(this IBindingToSyntax <T> builder)
        {
            var bindingConfiguration = builder.BindingConfiguration;

            bindingConfiguration.ProviderCallback = builder.Kernel.Components.Get <IMockProviderCallbackProvider>().GetCreationCallback();
            return(builder as IBindingWhenInNamedWithOrOnSyntax <T>);
        }
コード例 #4
0
 public static IBindingModeInfoBehaviorSyntax ToSelf <TTarget>(
     [NotNull] this IBindingToSyntax <TTarget> syntaxGeneric,
     [NotNull] Expression <Func <TTarget, object> > selfPath)
 {
     Should.NotBeNull(selfPath, "selfPath");
     return(ToSelf(syntaxGeneric, BindingExtensions.GetMemberPath(selfPath)));
 }
コード例 #5
0
 public static IBindingModeInfoBehaviorSyntax <TSource> To <TTarget, TSource>(
     [NotNull] this IBindingToSyntax <TTarget, TSource> syntax,
     [NotNull] Func <Expression <Func <TSource, IBindingSyntaxContext <TTarget, TSource>, object> > > expression)
     where TTarget : class
 {
     return(syntax.ToInternal <TSource>(expression));
 }
コード例 #6
0
 public static IBindingModeInfoBehaviorSyntax To <TTarget, TSource>(
     [NotNull] this IBindingToSyntax <TTarget, TSource> syntax,
     [NotNull] Expression <Func <TSource, object> > sourcePath)
 {
     Should.NotBeNull(sourcePath, "sourcePath");
     return(syntax.To(BindingExtensions.GetMemberPath(sourcePath)));
 }
コード例 #7
0
 public static IBindingModeInfoBehaviorSyntax <TSource> ToAction <TTarget, TSource>(
     [NotNull] this IBindingToSyntax <TTarget> syntax, TSource source,
     [NotNull] Func <Expression <Action <TSource, IBindingSyntaxContext <TTarget, TSource> > > > expression)
     where TTarget : class
 {
     return(syntax.ToInternal(expression, source));
 }
コード例 #8
0
 public static IBindingModeInfoBehaviorSyntax <TTarget> ToSelf <TTarget>([NotNull] this IBindingToSyntax <TTarget> syntax,
                                                                         [NotNull] string selfPath)
     where TTarget : class
 {
     Should.NotBeNull(syntax, "syntax");
     return(syntax.To((TTarget)syntax.Builder.GetData(BindingBuilderConstants.Target, true), selfPath));
 }
コード例 #9
0
ファイル: NLogModule.cs プロジェクト: zxw-ing/microdot
        /// <summary>
        /// Used by Microdot hosts to initialize logging and tracing.
        /// </summary>
        /// <param name="logBinding"></param>
        /// <param name="eventPublisherBinding"></param>
        public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding, IBindingToSyntax <Func <string, ILog> > funcLog)
        {
            // Bind microdot log that takes the caller name from class asking for the log
            logBinding
            .To <NLogLogger>()
            .InScope(GetTypeOfTarget)
            .WithConstructorArgument("receivingType", (context, target) => GetTypeOfTarget(context));

            eventPublisherBinding
            .To <LogEventPublisher>()
            .InSingletonScope();

            // Bind Orleans log with string context
            funcLog.ToMethod(c =>
            {
                return(loggerName =>
                {
                    var dict = c.Kernel.Get <DisposableCollection <string, ILog> >();
                    return dict.GetOrAdd(loggerName
                                         , logName =>
                    {
                        var caller = new ConstructorArgument("caller", logName);
                        return c.Kernel.Get <NLogLogger>(caller);
                    });
                });
            })
            .InTransientScope();
        }
コード例 #10
0
 /// <summary>
 /// Indicates that a service should be bound to the first available implementation.
 /// </summary>
 /// <typeparam name="TService">The implementation type.</typeparam>
 /// <typeparam name="T1">The first implementation type to try.</typeparam>
 /// <typeparam name="T2">The second implementation type to try.</typeparam>
 /// <typeparam name="T3">The third implementation type to try.</typeparam>
 /// <param name="syntax">The fluent syntax.</param>
 /// <returns>The fluent syntax.</returns>
 public static object ToFirst <TService, T1, T2, T3>(this IBindingToSyntax <TService> syntax)
     where T1 : TService
     where T2 : TService
     where T3 : TService
 {
     return(syntax.ToFirst(typeof(T1), typeof(T2), typeof(T3)));
 }
コード例 #11
0
        public override void Load()
        {
            IBindingToSyntax <IRepository <BankAccount> > bind = Bind <IRepository <BankAccount> >();

            switch (_storageType.ToLower())
            {
            case "ef":
            {
                bind.To <BankAccountEFRepository>().WithConstructorArgument("MyDefaultConnection");
                break;
            }

            case "json":
            {
                bind.To <BankAccountJsonRepository>();
                break;
            }

            case "xml":
            {
                bind.To <BankAccountXmlRepository>();
                break;
            }

            default:
            {
                bind.To <BankAccountBinaryRepository>();
                break;
            }
            }
            Bind <IUnitOfWork>().To <UnitOfWork>()
            .WithConstructorArgument(bind);
            Bind <IBankAccountService>().To <BankAccountService>();
        }
        /// <summary>
        /// Indicates that the service should be bound to a WCF service channel using the endpoint declared in the Web.config or App.config file with the given name.
        /// </summary>
        /// <typeparam name="TContract">The service contract interface type.</typeparam>
        /// <param name="bindingSyntax">The result from Bind&lt;TContract&gt;().</param>
        /// <param name="endpointConfigurationName">The configuration name used for the endpoint.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="endpointConfigurationName"/> is null.</exception>
        /// <returns>The fluent syntax;</returns>
        public static IBindingWhenInNamedWithOrOnSyntax <TContract> ToServiceChannel <TContract>(
            this IBindingToSyntax <TContract> bindingSyntax,
            string endpointConfigurationName) where TContract : class
        {
            ThrowIfNull(endpointConfigurationName, "endpointConfigurationName");

            return(BindChannel(bindingSyntax, () => new ChannelFactory <TContract>(endpointConfigurationName)));
        }
        private static IBindingWhenInNamedWithOrOnSyntax <TContract> BindChannel <TContract>(
            IBindingToSyntax <TContract> bindingSyntax, Func <ChannelFactory <TContract> > factoryFactory) where TContract : class
        {
            // Don't create the ChannelFactory at binding time, since it's an expensive operation
            var lazyFactory = new Lazy <ChannelFactory <TContract> >(factoryFactory);

            return(bindingSyntax.ToMethod(x => lazyFactory.Value.CreateChannel()));
        }
コード例 #14
0
 private static IBindingModeInfoBehaviorSyntax<TSource> ToInternal<TSource>(this IBindingToSyntax syntax,
     Func<LambdaExpression> expression, TSource source)
 {
     Should.NotBeNull(syntax, nameof(syntax));
     syntax.Builder.AddOrUpdate(BindingBuilderConstants.Source, source);
     BindingServiceProvider.BindingProvider.BuildFromLambdaExpression(syntax.Builder, expression);
     return syntax.GetOrAddSyntaxBuilder<IBindingModeInfoBehaviorSyntax<TSource>, object, TSource>();
 }
コード例 #15
0
 public static IBindingModeInfoBehaviorSyntax <TTarget> ToSelf <TTarget>(
     [NotNull] this IBindingToSyntax <TTarget> syntax,
     [NotNull] Func <Expression <Func <TTarget, IBindingSyntaxContext <TTarget, TTarget>, object> > > expression)
     where TTarget : class
 {
     Should.NotBeNull(syntax, "syntax");
     return(syntax.ToInternal(expression, (TTarget)syntax.Builder.GetData(BindingBuilderConstants.Target, true)));
 }
コード例 #16
0
 private static IBindingModeInfoBehaviorSyntax <TSource> ToInternal <TSource>(this IBindingToSyntax syntax,
                                                                              Func <LambdaExpression> expression, TSource source)
 {
     Should.NotBeNull(syntax, "syntax");
     syntax.Builder.AddOrUpdate(BindingBuilderConstants.Source, source);
     LambdaExpressionToBindingExpressionConverter.Convert(expression, syntax);
     return(syntax.GetOrAddSyntaxBuilder <IBindingModeInfoBehaviorSyntax <TSource>, object, TSource>());
 }
コード例 #17
0
 public static IBindingModeInfoBehaviorSyntax <object> ToSource([NotNull] this IBindingToSyntax syntax,
                                                                [NotNull] Func <IDataContext, IObserver> bindingSourceDelegate)
 {
     Should.NotBeNull(syntax, "syntax");
     Should.NotBeNull(bindingSourceDelegate, "bindingSourceDelegate");
     syntax.Builder.GetOrAddBindingSources().Add(bindingSourceDelegate);
     return(syntax.GetOrAddSyntaxBuilder <IBindingModeInfoBehaviorSyntax <object>, object, object>());
 }
コード例 #18
0
 public static IBindingModeInfoBehaviorSyntax <TSource> To <TSource>([NotNull] this IBindingToSyntax syntax,
                                                                     [NotNull] TSource source, [NotNull] string sourcePath)
 {
     Should.NotBeNull(source, "source");
     Should.NotBeNull(sourcePath, "sourcePath");
     syntax.Builder.AddOrUpdate(BindingBuilderConstants.Source, source);
     syntax.Builder.GetOrAddBindingSources().Add(context => BindingExtensions.CreateBindingSource(context, sourcePath, source));
     return(syntax.GetOrAddSyntaxBuilder <IBindingModeInfoBehaviorSyntax <TSource>, object, TSource>());
 }
        /// <summary>
        /// Indicates that the service should be bound to a WCF service channel using the given binding and the endpoint declared in the Web.config or App.config file.
        /// </summary>
        /// <typeparam name="TContract">The service contract interface type.</typeparam>
        /// <param name="bindingSyntax">The result from Bind&lt;TContract&gt;().</param>
        /// <param name="binding">The <see cref="T:System.ServiceModel.Channels.Binding"/> used to configure the endpoint.</param>
        /// <param name="remoteAddress">The <see cref="T:System.ServiceModel.EndpointAddress"/> that provides the location of the service.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="binding"/> or <paramref name="remoteAddress"/> is null.</exception>
        /// <returns>The fluent syntax;</returns>
        public static IBindingWhenInNamedWithOrOnSyntax <TContract> ToServiceChannel <TContract>(
            this IBindingToSyntax <TContract> bindingSyntax,
            Binding binding, EndpointAddress remoteAddress) where TContract : class
        {
            ThrowIfNull(binding, "binding");
            ThrowIfNull(remoteAddress, "remoteAddress");

            return(BindChannel(bindingSyntax, () => new ChannelFactory <TContract>(binding, remoteAddress)));
        }
コード例 #20
0
 public static IBindingModeInfoBehaviorSyntax ToSelf([NotNull] this IBindingToSyntax syntax,
                                                     [NotNull] string selfPath)
 {
     Should.NotBeNull(selfPath, "selfPath");
     return(syntax.ToSource(context =>
     {
         object target = context.GetData(BindingBuilderConstants.Target, true);
         return new BindingSource(BindingServiceProvider.ObserverProvider.Observe(target, BindingPath.Create(selfPath), false));
     }));
 }
コード例 #21
0
 /// <summary>
 /// Indicates that the service should be mocked. The mock is activated in the singleton scope.
 /// </summary>
 /// <typeparam name="T">The service type.</typeparam>
 /// <param name="builder">The fluent syntax.</param>
 /// <returns>The fluent syntax.</returns>
 public static IBindingWhenInNamedWithOrOnSyntax <T> ToMock <T>(this IBindingToSyntax <T> builder)
 {
     return(ProfilerInterceptor.GuardInternal(() =>
     {
         var result = builder.To <T>();
         builder.BindingConfiguration.ScopeCallback = StandardScopeCallbacks.Singleton;
         builder.Kernel.Components.Get <IMockResolver>().AttachToBinding(builder.BindingConfiguration, typeof(T));
         return result;
     }));
 }
        public static void Convert(LambdaExpression expression, IBindingToSyntax syntax)
        {
            Should.NotBeNull(expression, "expression");
            var callbacks = ConvertInternal(expression, syntax, true);

            for (int i = 0; i < callbacks.Length; i++)
            {
                callbacks[i].Invoke(syntax);
            }
        }
コード例 #23
0
 public static IBindingModeInfoBehaviorSyntax To([NotNull] this IBindingToSyntax syntax, [NotNull] object source,
                                                 [NotNull] string sourcePath)
 {
     Should.NotBeNull(source, "source");
     Should.NotBeNull(sourcePath, "sourcePath");
     return(syntax.ToSource(context =>
     {
         IObserver observer = BindingServiceProvider.ObserverProvider.Observe(source, BindingPath.Create(sourcePath), false);
         return new BindingSource(observer);
     }));
 }
コード例 #24
0
 public FullBindingToSyntax
 (
     IBindingToSyntax <T1> eager,
     IBindingToSyntax <Lazy <T1> > lazy,
     IKernel kernel
 )
 {
     _eager  = eager;
     _lazy   = lazy;
     _kernel = kernel;
 }
コード例 #25
0
        /// <summary>
        /// Used by Microcore hosts to initialize logging and tracing.
        /// </summary>
        /// <param name="logBinding"></param>
        /// <param name="eventPublisherBinding"></param>
        public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding)
        {
            logBinding
            .To <SerilogLogger>()
            .InScope(GetTypeOfTarget)
            .WithConstructorArgument("receivingType", (context, target) => GetTypeOfTarget(context));

            eventPublisherBinding
            .To <LogEventPublisher>()
            .InSingletonScope();
        }
コード例 #26
0
        protected override void OnBuild(IDependencyInfo info)
        {
            IBindingToSyntax <object> initial = Kernel.Bind(info.ResolvedTypes.ToArray());

            IBindingWhenInNamedWithOrOnSyntax <object> binding = BuildTo(info, initial);

            BindScope(info, binding);
            BindNamedIfConfigured(info, binding);

            Definitions.Add(info);
        }
コード例 #27
0
        /// <summary>
        /// Tries to handle the ToProvider attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="builder">The builder.</param>
        /// <returns>True if the attribute was found.</returns>
        private static IBindingWhenInNamedWithOrOnSyntax<object> HandleToProviderAttribute(XElement element, IBindingToSyntax<object> builder)
        {
            XAttribute providerAttribute = element.Attribute("toProvider");

            if (providerAttribute == null)
            {
                return null;
            }

            Type provider = GetTypeFromAttributeValue(providerAttribute);
            return builder.ToProvider(provider);
        }
コード例 #28
0
        /// <summary>
        /// Tries to handle the to attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="builder">The builder.</param>
        /// <returns>True if the To Attribute was found</returns>
        private static IBindingWhenInNamedWithOrOnSyntax<object> HandleToAttribute(XElement element, IBindingToSyntax<object> builder)
        {
            XAttribute toAttribute = element.Attribute("to");

            if (toAttribute == null)
            {
                return null;
            }

            Type implementation = GetTypeFromAttributeValue(toAttribute);
            return builder.To(implementation);
        }
コード例 #29
0
        public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding)
        {
            if (_useHttpLog)
            {
                logBinding.To <HttpLog>();
            }
            else
            {
                logBinding.To <ConsoleLog>();
            }

            eventPublisherBinding.To <NullEventPublisher>();
        }
コード例 #30
0
        public static IBindingWithOrOnSyntax <TService> ToExtrnalService <TService>(this IBindingToSyntax <TService> syntax)
        {
            var serviceBinding = new ServiceBindingInfo
            {
                ServiceType = typeof(TService),
                IsExternal  = true
            };

            syntax.Kernel.Bind <ServiceBindingInfo>().ToConstant(serviceBinding);

            return(syntax.ToMethod(ThrowOnAttemptToResolve <TService>)
                   .WithMetadata(nameof(ServiceBindingInfo), serviceBinding));
        }
コード例 #31
0
        public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding)
        {
            if (_useHttpLog)
            {
                logBinding.To <HttpLog>();
            }
            else
            {
                logBinding.To <ConsoleLog>();
            }

            eventPublisherBinding.To <SpyEventPublisher>().InSingletonScope();
        }
        //private readonly IBindingWhenInNamedWithOrOnSyntax<T2> _eager;
        //private readonly IBindingWhenInNamedWithOrOnSyntax<Lazy<T1>> _lazy;

        public FullBindingWhenInNamedWithOrOnSyntax
        (
            IBindingToSyntax <T1> eager,
            IBindingToSyntax <Lazy <T1> > lazy,
            IKernel kernel
        )
        {
            // we don't want to convert them via To or ToMethod just yet lest the resolver
            // fail due to multiple eligible bindings
            //_eager = eager.To<T2>();
            //_lazy = lazy.ToMethod(ctx => new Lazy<T1>(() => Constants.NinjectKernel.Get<T1>()));
            _eager  = eager;
            _lazy   = lazy;
            _kernel = kernel;
        }