Esempio n. 1
0
        public static IBindingWithOrOnSyntax <TImplementation> AsService <TImplementation>(
            this IBindingWithSyntax <TImplementation> syntax)
        {
            Type serviceType = null;

            if (typeof(TImplementation).IsClass && !typeof(TImplementation).IsAbstract)
            {
                if (serviceType == null)
                {
                    foreach (var interfaceType in typeof(TImplementation).GetInterfaces())
                    {
                        var bindings = syntax.Kernel.GetBindings(interfaceType).ToList();
                        foreach (var binding in bindings)
                        {
                            if (ReferenceEquals(binding.BindingConfiguration, syntax.BindingConfiguration))
                            {
                                serviceType = interfaceType;
                                break;
                            }
                        }
                        if (interfaceType != null)
                        {
                            break;
                        }
                    }
                }

                if (serviceType == null)
                {
                    if (typeof(TImplementation).BaseType.IsAbstract)
                    {
                        var bindings = syntax.Kernel.GetBindings(typeof(TImplementation).BaseType).ToList();
                        foreach (var binding in bindings)
                        {
                            if (ReferenceEquals(binding.BindingConfiguration, syntax.BindingConfiguration))
                            {
                                serviceType = typeof(TImplementation).BaseType;
                                break;
                            }
                        }
                    }
                }
            }

            if (serviceType == null)
            {
                throw new Exception();
            }

            var serviceBinding = new ServiceBindingInfo
            {
                ServiceType        = serviceType,
                ImplementationType = typeof(TImplementation)
            };

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

            return(syntax.WithMetadata(nameof(ServiceBindingInfo), serviceBinding));
        }
        public void Register(Type type, Type implementation, bool singleInstance, KeyValuePair <string, object>[] unmappedConstructorParameters)
        {
            IBindingWithSyntax <object> bind = Container.Bind(type).To(implementation);

            if (singleInstance)
            {
                bind = ((IBindingInSyntax <object>)bind).InSingletonScope();
            }
            else
            {
                bind = ((IBindingInSyntax <object>)bind).InTransientScope();
            }

            if (unmappedConstructorParameters == null)
            {
                return;
            }

            foreach (var parameter in unmappedConstructorParameters)
            {
                bind = bind.WithConstructorArgument(parameter.Key, parameter.Value);
            }
        }