Example #1
0
        /// <summary>
        /// Bind the specified service (interface, abstract class, concrete class, unbound generic, etc) to something
        /// </summary>
        /// <param name="serviceType">Service to bind</param>
        /// <returns>Fluent interface to continue configuration</returns>
        public IBindTo Bind(Type serviceType)
        {
            var builderBindTo = new BuilderBindTo(serviceType, this.GetAssemblies);

            this.bindings.Add(builderBindTo);
            return(builderBindTo);
        }
        /// <summary>
        /// Once all bindings have been set, build an IContainer from which instances can be fetched
        /// </summary>
        /// <returns>An IContainer, which should be used from now on</returns>
        public IContainer BuildContainer()
        {
            var container = new Container(this.autobindAssemblies);

            // Just in case they want it
            var bindings = this.bindings.ToList();
            var containerBuilderBindTo = new BuilderBindTo(typeof(IContainer), this.GetAssemblies);

            containerBuilderBindTo.ToInstance(container).DisposeWithContainer(false).AsWeakBinding();
            bindings.Add(containerBuilderBindTo);

            // For each binding which is weak, if another binding exists with any of the same type+key which is strong, we remove this binding
            var groups = (from binding in bindings
                          from serviceType in binding.ServiceTypes
                          select new { ServiceType = serviceType, Binding = binding })
                         .ToLookup(x => x.ServiceType);

            var filtered = from binding in bindings
                           where !(binding.IsWeak &&
                                   binding.ServiceTypes.Any(serviceType => groups.Contains(serviceType) && groups[serviceType].Any(groupItem => !groupItem.Binding.IsWeak)))
                           select binding;

            foreach (var binding in filtered)
            {
                binding.Build(container);
            }
            return(container);
        }
Example #3
0
        /// <summary>
        /// Bind the specified service (interface, abstract class, concrete class, unbound generic, etc) to something
        /// </summary>
        /// <param name="serviceType">Service to bind</param>
        /// <returns>Fluent interface to continue configuration</returns>
        protected IBindTo Bind(Type serviceType)
        {
            if (this.builder == null || this.getAssemblies == null)
            {
                throw new InvalidOperationException("Bind should only be called from inside Load, and you must not call Load yourself");
            }

            var builderBindTo = new BuilderBindTo(serviceType, this.getAssemblies);

            this.builder.AddBinding(builderBindTo);
            return(builderBindTo);
        }
Example #4
0
 internal void AddBinding(BuilderBindTo binding)
 {
     this.bindings.Add(binding);
 }