/// <summary> /// Attempts to handle a missing binding for a service. /// </summary> /// <param name="service">The service.</param> /// <returns><c>True</c> if the missing binding can be handled; otherwise <c>false</c>.</returns> protected override bool HandleMissingBinding(Type service) { var binding = new Binding(service) { ProviderCallback = MockProvider.GetCreationCallback(this), ScopeCallback = ctx => null, IsImplicit = true }; AddBinding(binding); return true; }
/// <summary> /// Returns any bindings from the specified collection that match the specified request. /// </summary> /// <param name="bindings">The multimap of all registered bindings.</param> /// <param name="request">The request in question.</param> /// <returns>The series of matching bindings.</returns> public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, IRequest request) { var service = request.Service; if (typeof(Controller).IsAssignableFrom(service)) { var binding = new Binding(service) { ProviderCallback = StandardProvider.GetCreationCallback(service) }; binding.Parameters.Add( typeof(AsyncController).IsAssignableFrom(service) ? new PropertyValue("ActionInvoker", ctx => ctx.Kernel.Get<NinjectAsyncActionInvoker>()) : new PropertyValue("ActionInvoker", ctx => ctx.Kernel.Get<NinjectActionInvoker>())); return new[] { binding }; } return Enumerable.Empty<IBinding>(); }
public void Handle(XmlModule module, XElement element) { XAttribute serviceAttribute = element.RequiredAttribute("service"); Type service = GetTypeFromAttributeValue(serviceAttribute); var binding = new Binding(service); var builder = new BindingBuilder<object>(binding, _kernel); module.AddBinding(binding); if (!HandleTarget(element, builder)) throw new ConfigurationErrorsException("The 'bind' element does not define either a 'to' or 'toProvider' attribute."); ReadName(element, builder); ReadMetadata(element, builder); ReadScope(element, builder); }
private static void RegisterType(IKernel kernel, Type type, IEnumerable<INinjectRegistrationAttribute> attributes) { var builder = new BindingBuilder() as IBindingBuilder; foreach (INinjectRegistrationAttribute attribute in attributes) { builder = attribute.Register(builder); } foreach (var service in builder.Services) { var binding = new Binding(service); binding.BindingConfiguration.ScopeCallback = builder.ScopeCallback; binding.BindingConfiguration.ProviderCallback = StandardProvider.GetCreationCallback(type); binding.BindingConfiguration.Target = BindingTarget.Type; kernel.AddBinding(binding); } }
/// <summary> /// /// </summary> /// <param name="service"></param> /// <returns></returns> protected override bool HandleMissingBinding(Type service) { bool selfBindable = TypeIsSelfBindable(service); if (selfBindable) Bind(service).ToSelf().InSingletonScope(); else { var binding = new Binding(service) { ProviderCallback = MockProvider.GetCreationCallback(), ScopeCallback = ctx => StaticScope, IsImplicit = true }; AddBinding(binding); } return true; }
internal LomsKernel() { }// The idea is that only the KernelContainer can create the Kernel (or for Tests we set the InternalsVisibleTo) /// <remarks> /// We use a convention to find the default implementation for an internface. It should be in the same /// namespace with the same name without the "I" prefix. For example the default implementation for /// MyNamespace.IMyService would be MyNamespace.MyService /// If there's no implementation and the interface is a ServiceContract (suffix "Service" and decorated with DataContractAttribute) /// we will look for an implementation with "Client" suffix or create a dynamic proxy using the ChannelFactory if nothing is found. /// </remarks> protected override bool HandleMissingBinding(Type requestedType) { if (!IsViewModel(requestedType) && base.HandleMissingBinding(requestedType)) return true; // We only continue if the base couldn't resolve the binding (currently SelfBindable types). if (requestedType.IsGenericType) // Adjust to use open generics requestedType = requestedType.GetGenericTypeDefinition(); var implementationType = GetDefaultImplementationType(requestedType); var providerCallBack = GetProviderCallback(requestedType, implementationType); if (providerCallBack != null) { var binding = new Binding(requestedType) { ProviderCallback = providerCallBack, IsImplicit = true }; AddBinding(binding); return true; } return false; }
public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, IRequest request) { if (request.Service == typeof(IBar)) { var binding = new Binding(request.Service); var builder = new BindingBuilder<IBar>(binding, this.kernel, string.Empty); builder.To<Bar>().WithConstructorArgument("name", "parent"); return new[] { binding }; } return Enumerable.Empty<IBinding>(); }
/// <summary> /// Attempts to handle a missing binding for a service. /// </summary> /// <param name="service">The service.</param> /// <returns><c>True</c> if the missing binding can be handled; otherwise <c>false</c>.</returns> protected virtual bool HandleMissingBinding(Type service) { Ensure.ArgumentNotNull(service, "service"); if (!TypeIsSelfBindable(service)) return false; var binding = new Binding(service) { ProviderCallback = StandardProvider.GetCreationCallback(service), IsImplicit = true }; AddBinding(binding); return true; }
/// <summary> /// Injects the specified existing instance, without managing its lifecycle. /// </summary> /// <param name="instance">The instance to inject.</param> /// <param name="parameters">The parameters to pass to the request.</param> public virtual void Inject(object instance, params IParameter[] parameters) { Ensure.ArgumentNotNull(instance, "instance"); Ensure.ArgumentNotNull(parameters, "parameters"); Type service = instance.GetType(); var planner = Components.Get<IPlanner>(); var pipeline = Components.Get<IPipeline>(); var binding = new Binding(service); var request = CreateRequest(service, null, parameters, false); var context = CreateContext(request, binding); context.Plan = planner.GetPlan(service); var reference = new InstanceReference { Instance = instance }; pipeline.Activate(context, reference); }