Exemplo n.º 1
0
        public IService GenerateService(Type @class, IReadOnlyContainer container, object instance = null, IConstructorParameters constructorParameters = null)
        {
            if (ClassHasFactoryChecker.HasFactory(@class))
            {
                IServiceFactory factory = FactoryProvider.ProvideServiceFactory(@class, container);
                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 ServiceFlags ProvideFlags(Type type)
        {
            ServiceFlags flags = ServiceFlags.CreateNew();
            IEnumerable <ServiceFlagAttribute> flagAttributes = AttributesFinder.FindAttributesEverywhere(type, (member, attr) =>
            {
                ServiceFlagAttribute flagAttr = (ServiceFlagAttribute)attr;
                ServiceFlag sflag             = flagAttr.ServiceFlag;

                return(new ServiceFlagAttribute()
                {
                    ServiceFlag = new ServiceFlag(sflag.Name, sflag.Value)
                    {
                        Member = MemberGenerator.GenerateMember(member)
                    }
                });
            });

            foreach (ServiceFlagAttribute attribute in flagAttributes)
            {
                flags.AddFlag(attribute.ServiceFlag.Name, attribute.ServiceFlag.Value, attribute.ServiceFlag.Member);
            }

            return(flags);
        }
Exemplo n.º 3
0
 public IEnumerable <ServiceFlag> ProvideFlags(ServiceFlags flags, string name)
 {
     return(flags.Where(x => x.Name == name));
 }
Exemplo n.º 4
0
 public IEnumerable <IMember> ProvideMembers(ServiceFlags serviceFlags)
 {
     return(Array.ConvertAll(serviceFlags.GetFlags(ServiceFlagConstants.Instance).ToArray(), x => x.Member));
 }
Exemplo n.º 5
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
                });
            }
        }
Exemplo n.º 6
0
 public object CreateInstance(ServiceFlags flags, Type type, IReadOnlyContainer container)
 {
     return(CtorInstanceCreator.CreateInstance(flags, type, container));
 }