public IConstructorParameter FindByType(IConstructorParameters parameters, Type type)
        {
            IConstructorParameter selected = parameters.Parameters.First(x => type.IsAssignableFrom(x.Type));

            parameters.Parameters.Remove(selected);

            return(selected);
        }
        public IEnumerable <IParameter> GenerateParameters(IConstructor constructor, IConstructorParameters constructorParameters)
        {
            foreach (IParameter parameter in constructor.Parameters)
            {
                IConstructorParameter constructorParameter = ConstructorParameterByTypeFinder.FindByType(constructorParameters, parameter.Type);
                parameter.Value = constructorParameter.Value;

                yield return(parameter);
            }
        }
Beispiel #3
0
        public StorageBuilder Register(Type type, params object[] constructorParameters)
        {
            IConstructorParameters parameters = ConstructorParametersGenerator.GenerateParameters(constructorParameters);

            return(Update(x =>
            {
                AssemblyRegistrar.RegisterIfNotExist(x.Assemblies, type.Assembly);
                IEnumerable <IService> services = ServicesGenerator.GenerateServices(type, Object.Assemblies, null, parameters);
                ServiceRegistrar.Register(Object.Services, services);
            }));
        }
        public List <Type> GenerateList(IConstructorParameters parameters)
        {
            IEnumerable <Type> generate(IConstructorParameters p)
            {
                foreach (IConstructorParameter parameter in parameters.Parameters)
                {
                    yield return(parameter.Type);
                }
            }

            return(generate(parameters).ToList());
        }
        public object CreateInstance(Type @class, IConstructorParameters constructorParameters)
        {
            ConstructorInfo[]          constructorInfos = ConstructorInfoListGenerator.GenerateList(@class);
            IEnumerable <IConstructor> constructors     = ConstructorListGenerator.GenerateList(constructorInfos);
            IConstructor             constructor        = ConstructorFinder.FindBy(constructors, constructorParameters);
            IEnumerable <IParameter> parameters         = ParametersGenerator.GenerateParameters(constructor, constructorParameters);

            object[] values   = ParametersValuesExtractor.ExtractValues(parameters);
            object   instance = ConstructorInvoker.InvokeConstructor(constructor, values);

            return(instance);
        }
Beispiel #6
0
        public void Register <T>(params object[] parameters)
        {
            Type type = TypeGetter.GetType <T>();
            IConstructorParameters constructorParameters = ConstructorParametersByObjectsGenerator.GenerateParameters(parameters);

            IEnumerable <IService> services = ServicesGenerator.GenerateServices(type, Storage.Assemblies, this, constructorParameters);

            foreach (IService service in services)
            {
                if (ServiceIsAutoValueChecker.Check(service))
                {
                    ServiceInitializer.Initialize(service, this);
                }
            }

            ServiceRegistrar.Register(Storage.Services, services);
        }
Beispiel #7
0
        public IConstructor FindBy(IEnumerable <IConstructor> ctors, IConstructorParameters constructorParameters)
        {
            // TODO please.

            List <IConstructor> matchingLen = ctors
                                              .Where(x => x.Parameters.Count() >= constructorParameters.Parameters.Count)
                                              .ToList();

            List <IConstructor> matchingParameters = matchingLen.Where(
                x => x.Parameters.All(y => constructorParameters.Parameters.FirstOrDefault(z => z.Type.IsAssignableFrom(y.Type)) != null))
                                                     .ToList();

            List <IConstructor> ordered = matchingParameters.OrderBy(x => x.Parameters.Count()).ToList();

            IConstructor result = ordered.FirstOrDefault();

            return(result);
        }
Beispiel #8
0
        public IEnumerable <IService> GenerateServices(Type type, IAssemblyList assemblies, IReadOnlyContainer container,
                                                       IConstructorParameters constructorParameters = null, object instance = null)
        {
            if (TypeIsClassValidator.Validate(type))
            {
                IService service = ServiceGenerator.GenerateService(type, container, instance, constructorParameters);
                yield return(service);
            }

            else
            {
                IEnumerable <Type> types = ImplementationsFinder.FindImplementations(assemblies, type);

                foreach (Type @class in types)
                {
                    IService service = ServiceGenerator.GenerateService(@class, container, null, constructorParameters);
                    yield return(service);
                }
            }
        }
Beispiel #9
0
        public IService GenerateService(Type @class, object instance = null, IConstructorParameters constructorParameters = null)
        {
            if (ClassHasFactoryChecker.HasFactory(@class))
            {
                IServiceFactory factory = FactoryProvider.ProvideServiceFactory(@class);
                return(FactoryInvoker.Invoke(factory));
            }

            ServiceFlags         flags        = FlagsGenerator.GenerateFlags(@class);
            ServiceInfo          info         = InfoGenerator.Generate(@class);
            IServiceRegistration registration = RegistrationGenerator.Generate(flags, @class, instance, constructorParameters);

            return(new ServiceBuilder()
                   .AddFlags(flags)
                   .AddInfo(info)
                   .AddData(new ServiceData()
            {
                Instance = instance
            })
                   .AddRegistration(registration)
                   .Build());
        }
 public IServiceRegistration Generate(ServiceFlags flags, Type type, object instance = null, IConstructorParameters constructorParameters = null)
 {
     return(new ServiceRegistration()
     {
         TargetType = type,
         RegistrationFlags = RegistrationFlagsGenerator.GenerateFlags(flags, type, instance, constructorParameters)
     });
 }
Beispiel #11
0
        public IEnumerable <ServiceRegistrationFlag> GenerateFlags(ServiceFlags flags, Type type, object instance, IConstructorParameters constructorParameters)
        {
            if (instance != null)
            {
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.HasInstance, instance));
            }

            if (constructorParameters != null)
            {
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.HasConstructorParameters, true));

                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.ConstructorParameters, constructorParameters));
            }

            Type baseType = BaseTypeFinder.GetBaseTypeAnotherOf(type, null, typeof(object));

            if (baseType != null)
            {
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.AsClass, baseType));
            }

            IEnumerable <IInterface> interfaces = InterfacesGenerator.GenerateInterfaces(flags, type);

            foreach (IInterface @interface in interfaces)
            {
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.AsInterface, @interface));
            }

            ConstructorInfo[] constructors = ConstructorInfoListGenerator.GenerateList(type);

            if (flags.HasFlag(ServiceFlagConstants.ServiceCtor))
            {
                ServiceFlag  flag               = flags.GetFlag(ServiceFlagConstants.ServiceCtor);
                IMember      member             = flag.Member;
                IConstructor serviceConstructor = ConstructorGenerator.GenerateConstructor((ConstructorInfo)member.Instance);

                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.DefaultConstructor, serviceConstructor));
            }

            else
            {
                ConstructorInfo defaultConstructorInfo = DefaultConstructorInfoProvider.ProvideDefaultConstructor(constructors);
                IConstructor    defaultConstructor     = ConstructorGenerator.GenerateConstructor(defaultConstructorInfo);

                if (defaultConstructor != null)
                {
                    yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.DefaultConstructor, defaultConstructor));
                }
            }

            foreach (ConstructorInfo constructor in constructors)
            {
                IConstructor ctor = ConstructorGenerator.GenerateConstructor(constructor);
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.Constructor, ctor));
            }

            Type[] genericArguments = type.GetGenericArguments();

            if (genericArguments.Any())
            {
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.HasGenericParameters, null));

                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.GenericParameters, genericArguments));
            }

            foreach (ServiceFlag factoryFlag in flags.GetFlags(ServiceFlagConstants.ServiceFactory))
            {
                yield return(new ServiceRegistrationFlag(RegistrationFlagConstants.Factory, null)
                {
                    Member = factoryFlag.Member
                });
            }
        }
 public object CreateInstance(Type type, IConstructorParameters constructorParameter)
 {
     return(CtorInstanceCreator.CreateInstance(type, constructorParameter));
 }