/// <summary>
        /// Constructs the interception advice to trigger according to the binding used.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <returns>
        ///     An <see cref="IAdviceTargetSyntax"/> instance which allows the attachment of an <see cref="IInterceptor"/>.
        /// </returns>
        private static IAdviceTargetSyntax DoIntercept(IBindingSyntax binding)
        {
            IKernel kernel = binding.Kernel;
            IAdvice advice = kernel.Components.Get <IAdviceFactory>()
                             .Create(context => ReferenceEquals(binding.BindingConfiguration, context.Binding.BindingConfiguration));

            kernel.Components.Get <IAdviceRegistry>().Register(advice);

            return(new AdviceBuilder(advice));
        }
        public static IBindingSyntax MapKey <T>(this IBindingNamedSyntax <T> binding, string key)
        {
            IBindingSyntax bindingSyntax = binding;

            if (!string.IsNullOrEmpty(key))
            {
                bindingSyntax = binding.Named(key);
            }

            return(bindingSyntax);
        }
        public static void ReplaceExisting(this IBindingSyntax bindingInSyntax, Type type)
        {
            var kernel           = bindingInSyntax.Kernel;
            var bindingsToRemove = kernel.GetBindings(type).Where(b => string.Equals(b.Metadata.Name, bindingInSyntax.BindingConfiguration.Metadata.Name, StringComparison.Ordinal));

            foreach (var bindingToRemove in bindingsToRemove)
            {
                kernel.RemoveBinding(bindingToRemove);
            }

            var binding = new Binding(type, bindingInSyntax.BindingConfiguration);

            kernel.AddBinding(binding);
        }
Exemple #4
0
        public void Put(IBindingSyntax binding)
        {
            Requires.NotNull(binding, "binding");
            Requires.NotNull(binding.Binding, "binding.Binding");

            var singleBinding = binding.Binding as SingleBinding;

            Assumes.True(singleBinding != null);

            var instanceResolver = singleBinding.Resolver as InstanceResolver;

            Assumes.True(instanceResolver != null);
            Assumes.True(instanceResolver.Instance != null);

            _maps.Add(ComposeKey(singleBinding.Service, singleBinding.Name), instanceResolver.Instance);
        }
        /// <summary>
        /// Registers the type mapping.
        /// </summary>
        /// <param name="serviceType"><see cref="Type"/> that will be requested.</param>
        /// <param name="implementationType"><see cref="Type"/> that will actually be returned.</param>
        /// <param name="serviceName">Name to use for registration, null if a default registration.</param>
        /// <param name="lifetime">The lifetime strategy of the resolved instances.</param>
        protected override void DoRegister(Type serviceType, Type implementationType, string serviceName, ServiceLifetime lifetime)
        {
            if (_kernel == null)
            {
                throw new ObjectDisposedException("container");
            }
            var            binding = _kernel.Bind(serviceType).To(implementationType);
            IBindingSyntax syntax  = binding;

            if (serviceName != null)
            {
                syntax = binding.Named(serviceName);
            }
            OnRegistering(new NinjectServiceRegisterEventArgs(serviceType, implementationType, serviceName, lifetime, syntax));
            switch (lifetime)
            {
            case ServiceLifetime.Transient:
                binding.InScope(StandardScopeCallbacks.Transient);
                break;

            case ServiceLifetime.Singleton:
                binding.InScope(StandardScopeCallbacks.Singleton);
                break;

            case ServiceLifetime.PerThread:
                binding.InScope(ctx => System.Threading.Thread.CurrentThread);
                break;

            case ServiceLifetime.PerRequest:
#if NetCore
                binding.InScope(ctx => ctx.Kernel.Get <Microsoft.AspNetCore.Http.IHttpContextAccessor>()?.HttpContext);
#else
                binding.InScope(ctx => System.Web.HttpContext.Current);
#endif
                break;
            }
        }
Exemple #6
0
 public void Put(IBindingSyntax binding)
 {
     StructureMapContainerRegistrator.Register(NativeContainer, (dynamic)binding.Binding);
 }
Exemple #7
0
 internal NinjectServiceRegisterEventArgs(Type serviceType, Type implementType, string serviceName, ServiceLifetime lifetime, IBindingSyntax binding) :
     base(serviceType, implementType, serviceName, lifetime)
 {
     Binding = binding;
 }
Exemple #8
0
 public void Put(IBindingSyntax binding)
 {
     WindsorContainerRegistrator.Register(NativeContainer, (dynamic)binding.Binding);
 }