Esempio n. 1
0
        private ScopeRestrictedRegistry CreateScopeRestrictedRegistry(object tag, Action <ContainerBuilder> configurationAction)
        {
            var builder = new ContainerBuilder(new FallbackDictionary <string, object>(ComponentRegistry.Properties));

            foreach (var source in ComponentRegistry.Sources
                     .Where(src => src.IsAdapterForIndividualComponents))
            {
                builder.RegisterSource(source);
            }

            // Issue #272: Only the most nested parent registry with HasLocalComponents is registered as an external source
            // It provides all non-adapting registrations from itself and from it's parent registries
            var parent = Traverse.Across <ISharingLifetimeScope>(this, s => s.ParentLifetimeScope)
                         .Where(s => s.ComponentRegistry.HasLocalComponents)
                         .Select(s => new ExternalRegistrySource(s.ComponentRegistry))
                         .FirstOrDefault();

            if (parent != null)
            {
                builder.RegisterSource(parent);
            }

            configurationAction(builder);

            var locals = new ScopeRestrictedRegistry(tag, builder.Properties);

            builder.UpdateRegistry(locals);
            return(locals);
        }
Esempio n. 2
0
        ScopeRestrictedRegistry CreateScopeRestrictedRegistry(object tag, Action <ContainerBuilder> configurationAction)
        {
            var builder = new ContainerBuilder();

            foreach (var source in ComponentRegistry.Sources
                     .Where(src => src.IsAdapterForIndividualComponents))
            {
                builder.RegisterSource(source);
            }

            var parents = Traverse.Across <ISharingLifetimeScope>(this, s => s.ParentLifetimeScope)
                          .Where(s => s.ComponentRegistry.HasLocalComponents)
                          .Select(s => new ExternalRegistrySource(s.ComponentRegistry))
                          .Reverse();

            foreach (var external in parents)
            {
                builder.RegisterSource(external);
            }

            configurationAction(builder);

            var locals = new ScopeRestrictedRegistry(tag);

            builder.Update(locals);
            return(locals);
        }
Esempio n. 3
0
        public void traverses_the_object()
        {
            var type   = typeof(StubsForReflection);
            var result = Traverse.Across(type, x => x !.BaseType).ToList();

            result.ShouldNotBeNull();
            result.Count.ShouldBe(2);
            result[0].ShouldBe(typeof(StubsForReflection));
            result[1].ShouldBe(typeof(object));
        }
Esempio n. 4
0
        public static void EnforceBindable(Type implementationType, IEnumerable <Service> services)
        {
            if (implementationType == null)
            {
                throw new ArgumentNullException("implementationType");
            }
            if (services == null)
            {
                throw new ArgumentNullException("services");
            }

#if !ASPNETCORE50
            if (!implementationType.IsGenericTypeDefinition)
#else
            if (!implementationType.GetTypeInfo().IsGenericTypeDefinition)
#endif
            { throw new ArgumentException(
                        string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.ImplementorMustBeOpenGenericTypeDefinition, implementationType)); }

            foreach (IServiceWithType service in services)
            {
#if !ASPNETCORE50
                if (!service.ServiceType.IsGenericTypeDefinition)
#else
                if (!service.ServiceType.GetTypeInfo().IsGenericTypeDefinition)
#endif
                { throw new ArgumentException(
                            string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.ServiceTypeMustBeOpenGenericTypeDefinition, service)); }

#if !ASPNETCORE50
                if (service.ServiceType.IsInterface)
#else
                if (service.ServiceType.GetTypeInfo().IsInterface)
#endif
                {
                    if (GetInterface(implementationType, service.ServiceType) == null)
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.InterfaceIsNotImplemented, implementationType, service));
                    }
                }
                else
                {
#if !ASPNETCORE50
                    if (!Traverse.Across(implementationType, t => t.BaseType).Any(t => IsCompatibleGenericClassDefinition(t, service.ServiceType)))
#else
                    if (!Traverse.Across(implementationType, t => t.GetTypeInfo().BaseType).Any(t => IsCompatibleGenericClassDefinition(t, service.ServiceType)))
#endif
                    { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.TypesAreNotConvertible, implementationType, service)); }
                }
            }
        }
 public static void EnforceBindable(Type implementationType, IEnumerable <Service> services)
 {
     if (implementationType == null)
     {
         throw new ArgumentNullException("implementationType");
     }
     if (services == null)
     {
         throw new ArgumentNullException("services");
     }
     if (!implementationType.IsGenericTypeDefinition)
     {
         throw new ArgumentException(string.Format(OpenGenericServiceBinderResources.ImplementorMustBeOpenGenericTypeDefinition, implementationType));
     }
     using (IEnumerator <Service> enumerator = services.GetEnumerator())
     {
         Func <Type, bool> predicate = null;
         while (enumerator.MoveNext())
         {
             IServiceWithType service = (IServiceWithType)enumerator.Current;
             if (!service.ServiceType.IsGenericTypeDefinition)
             {
                 throw new ArgumentException(string.Format(OpenGenericServiceBinderResources.ServiceTypeMustBeOpenGenericTypeDefinition, service));
             }
             if (service.ServiceType.IsInterface)
             {
                 if (GetInterface(implementationType, service.ServiceType) == null)
                 {
                     throw new ArgumentException(string.Format(OpenGenericServiceBinderResources.InterfaceIsNotImplemented, implementationType, service));
                 }
             }
             else
             {
                 if (predicate == null)
                 {
                     predicate = t => IsCompatibleGenericClassDefinition(t, service.ServiceType);
                 }
                 if (!Traverse.Across <Type>(implementationType, t => t.BaseType).Any <Type>(predicate))
                 {
                     throw new ArgumentException(string.Format(OpenGenericServiceBinderResources.TypesAreNotConvertible, implementationType, service));
                 }
             }
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Throws an exception if an open generic implementation type cannot implement the set of specified open services.
        /// </summary>
        /// <param name="implementationType">The open generic implementation type.</param>
        /// <param name="services">The set of open generic services.</param>
        public static void EnforceBindable(Type implementationType, IEnumerable <Service> services)
        {
            if (implementationType == null)
            {
                throw new ArgumentNullException(nameof(implementationType));
            }

            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (!implementationType.IsGenericTypeDefinition)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.ImplementorMustBeOpenGenericTypeDefinition, implementationType));
            }

            foreach (var service in services.OfType <IServiceWithType>())
            {
                if (!service.ServiceType.IsGenericTypeDefinition)
                {
                    throw new ArgumentException(
                              string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.ServiceTypeMustBeOpenGenericTypeDefinition, service));
                }

                if (service.ServiceType.IsInterface)
                {
                    if (GetInterfaces(implementationType, service.ServiceType).Length == 0)
                    {
                        var message = string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.ImplementorDoesntImplementService, implementationType.FullName, service.ServiceType.FullName);
                        throw new InvalidOperationException(message);
                    }
                }
                else
                {
                    if (!Traverse.Across(implementationType, t => t.BaseType !).Any(t => IsCompatibleGenericClassDefinition(t, service.ServiceType)))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, OpenGenericServiceBinderResources.TypesAreNotConvertible, implementationType, service));
                    }
                }
            }
        }
Esempio n. 7
0
        public static void EnforceBindable(Type implementationType, IEnumerable <Service> services)
        {
            if (implementationType == null)
            {
                throw new ArgumentNullException("implementationType");
            }
            if (services == null)
            {
                throw new ArgumentNullException("services");
            }

            if (!implementationType.IsGenericTypeDefinition)
            {
                throw new ArgumentException(
                          string.Format("The implementation type '{0}' is not an open generic type definition.", implementationType));
            }

            foreach (IServiceWithType service in services)
            {
                if (!service.ServiceType.IsGenericTypeDefinition)
                {
                    throw new ArgumentException(
                              string.Format("The service '{0}' is not an open generic type definition.", service));
                }

                if (service.ServiceType.IsInterface)
                {
                    if (GetInterface(implementationType, service.ServiceType) == null)
                    {
                        throw new ArgumentException(string.Format("The implementation type '{0}' does not support the interface '{1}'.", implementationType, service));
                    }
                }
                else
                {
                    if (!Traverse.Across(implementationType, t => t.BaseType).Any(t => IsCompatibleGenericClassDefinition(t, service.ServiceType)))
                    {
                        throw new ArgumentException(string.Format("The service '{1}' is not assignable from implementation type '{0}'.", implementationType, service));
                    }
                }
            }
        }
Esempio n. 8
0
        private ScopeRestrictedRegistry CreateScopeRestrictedRegistry(object tag, Action <ContainerBuilder> configurationAction)
        {
            ContainerBuilder builder = new ContainerBuilder();

            foreach (IRegistrationSource source in from src in this.ComponentRegistry.Sources
                     where src.IsAdapterForIndividualComponents
                     select src)
            {
                builder.RegisterSource(source);
            }
            foreach (ExternalRegistrySource source2 in (from s in Traverse.Across <ISharingLifetimeScope>(this, s => s.ParentLifetimeScope)
                                                        where s.ComponentRegistry.HasLocalComponents
                                                        select new ExternalRegistrySource(s.ComponentRegistry)).Reverse <ExternalRegistrySource>())
            {
                builder.RegisterSource(source2);
            }
            configurationAction(builder);
            ScopeRestrictedRegistry componentRegistry = new ScopeRestrictedRegistry(tag);

            builder.Update(componentRegistry);
            return(componentRegistry);
        }
Esempio n. 9
0
 public void throws_on_null_next()
 {
     Should.Throw <ArgumentNullException>(() => Traverse.Across(new object(), null !).Materialize());
 }
 /// <summary>
 ///   Gets the types assignable from the specified type.
 /// </summary>
 /// <param name="type">
 ///   The type to traverse for assignable types.
 /// </param>
 /// <returns>
 ///   All available assignable types.
 /// </returns>
 public static IEnumerable <Type> TypesAssignableFrom(this Type type)
 {
     type = Guard.NotNull(type, nameof(type));
     return(type.GetInterfaces().Concat(Traverse.Across(type, t => t?.BaseType)));
 }