Ejemplo n.º 1
0
        public IRegisterTimeContainer Register(IEnumerable <Type> types, IRegisterTimeContainer container)
        {
            var implementationTypes = types
                                      .Where(type => !type.IsInterface)
                                      .Where(type => !type.IsAbstract)
                                      .Where(type => type.IsPublic)
                                      .Where(type => !type.IsNested)
                                      .Where(type => type.GetInterfaces().Count() == 1)
                                      .Where(type => type.GetConstructors().Count() == 1);

            var byInterface = implementationTypes
                              .GroupBy(type => type.GetInterfaces().Single().ToTypeKey());

            foreach (var implementations in byInterface)
            {
                var inter = implementations.Key;

                var composites = implementations.Where(type => type.IsComposite());
                var decorators = implementations.Where(type => type.IsDecorator());

                var normals = implementations.Except(composites).Except(decorators);

                if (composites.Count() > 1)
                {
                    throw new TypePatternRegistrationException("You cannot register more than one composite.");
                }
                if (!composites.Any() && !normals.Any())
                {
                    throw new TypePatternRegistrationException("You cannot register only decorators.");
                }

                if (composites.Any())
                {
                    container.Register(inter, composites.Single());
                }
                else if (normals.Count() == 1)
                {
                    container.Register(inter, normals.Single());
                }

                if (normals.Any())
                {
                    container.RegisterAll(inter, normals);
                }

                foreach (var decorator in decorators)
                {
                    container.RegisterDecorator(inter, decorator);
                }
            }

            return(container);
        }
        public IRegisterTimeContainer Register(IReadOnlyCollection <Type> types, IRegisterTimeContainer container)
        {
            var implementationTypes = types
                                      .Where(type => type.GetInterfaces().Length == 1)
                                      .Where(type => type.GetConstructors().Length == 1)
                                      .ToArray();

            var byInterface = implementationTypes
                              .SelectMany(type =>
            {
                return(type.GetInterfaces().Select(i => new[]
                {
                    i.ToTypeKey(),
                    type
                }));
            })
                              .GroupBy(pair => pair[0], pair => pair[1]);

            foreach (var registrationGroup in byInterface)
            {
                var inter           = registrationGroup.Key;
                var implementations = registrationGroup.ToList();

                var composites = implementations.Where(type => type.IsComposite()).ToArray();
                var decorators = implementations.Where(type => type.IsDecorator()).ToArray();

                var normals = implementations.Except(composites).Except(decorators).ToArray();

                if (composites.Length > 1)
                {
                    throw new TypePatternRegistrationException("You cannot register more than one composite.");
                }
                if (!composites.Any() && !normals.Any())
                {
                    throw new TypePatternRegistrationException("You cannot register only decorators.");
                }

                if (composites.Any())
                {
                    var composite = composites.Single();
                    container.Register(inter, composite, GetScope(composite));
                }

                if (normals.Length == 1)
                {
                    var normal = normals.Single();
                    container.Register(inter, normal, GetScope(normal));
                }
                else if (normals.Any())
                {
                    container.RegisterAll(inter, normals.Select(type => (type, GetScope(type))));
                }

                foreach (var decorator in decorators)
                {
                    container.RegisterDecorator(inter, decorator, GetScope(decorator));
                }
            }

            return(container);
        }
 public static IRegisterTimeContainer Register <TService, TImplementation>(this IRegisterTimeContainer container)
     where TImplementation : TService
 => container.Register(typeof(TService), typeof(TImplementation), Lifestyle.Transient);
 public static IRegisterTimeContainer RegisterAll(this IRegisterTimeContainer container, Type service, IEnumerable <Type> implementations)
 => container.RegisterAll(service, implementations.Select(type => (type, type.GetLifestyle())));
 public static IRegisterTimeContainer RegisterDecorator(this IRegisterTimeContainer container, Type service, Type implementation)
 => container.RegisterDecorator(service, implementation, implementation.GetLifestyle());
 public IRegisterTimeContainer Register(IReadOnlyCollection <Type> types, IRegisterTimeContainer container)
 {
     return(container);
 }