Exemplo n.º 1
0
        public void Register(ContainerRegistrationContext context)
        {
            ICReg reg;
            var   possibleConstructors = _constructorTrait.ReturnPossibleConstructors(_implementationType).ToList();
            var   bestConstructor      = _constructorTrait.ChooseConstructor(_implementationType, possibleConstructors);

            if (bestConstructor == null)
            {
                throw new ArgumentException($"Cannot find public constructor for {_implementationType.FullName}");
            }
            switch (_liveScopeTrait.Lifetime)
            {
            case Lifetime.AlwaysNew:
                reg = new AlwaysNewImpl(_implementationType, bestConstructor, _propertiesTrait.ArePropertiesAutowired);
                break;

            case Lifetime.Singleton:
                reg = new SingletonImpl(_implementationType, new AlwaysNewImpl(_implementationType, bestConstructor, _propertiesTrait.ArePropertiesAutowired), context.SingletonCount);
                context.SingletonCount++;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            context.AddCReg(_asTrait.GetAsTypesFor(_implementationType), _asTrait.PreserveExistingDefaults, reg);
        }
Exemplo n.º 2
0
 public void Register(ContainerRegistrationContext context)
 {
     foreach (var assembly in _froms)
     {
         foreach (var type in assembly.GetTypes())
         {
             if (!type.IsClass)
             {
                 continue;
             }
             if (type.IsAbstract)
             {
                 continue;
             }
             if (type.IsGenericTypeDefinition)
             {
                 continue;
             }
             if (type.IsDelegate())
             {
                 continue;
             }
             if (!_scanTrait.MatchFilter(type))
             {
                 continue;
             }
             if (_constructorTrait.ChooseConstructor(type, _constructorTrait.ReturnPossibleConstructors(type)) == null)
             {
                 continue;
             }
             ((IContanerRegistration) new SingleRegistration(type, _asTrait, _liveScopeTrait, _constructorTrait, _propertiesTrait)).Register(context);
         }
     }
 }
Exemplo n.º 3
0
        // ReSharper restore MemberCanBePrivate.Global

        internal ContainerImpl(ReadOnlySpan <IRegistration> registrations, ContainerVerification containerVerification)
        {
            var context = new ContainerRegistrationContext(this, Registrations);

            foreach (var registration in registrations)
            {
                ((IContanerRegistration)registration).Register(context);
            }

            SingletonLocks = new object[context.SingletonCount];
            for (var i = 0; i < context.SingletonCount; i++)
            {
                SingletonLocks[i] = new object();
            }

            Singletons = new object[context.SingletonCount];
            Instances  = context.Instances;
            context.AddCReg(Enumerable.Repeat(new KeyAndType(null, typeof(IContainer)), 1), true,
                            new ContainerInjectImpl());
            if (containerVerification == ContainerVerification.None)
            {
                return;
            }
            foreach (var(_, reg) in Registrations)
            {
                reg.Verify(containerVerification, this);
            }
        }
Exemplo n.º 4
0
        public void Register(ContainerRegistrationContext context)
        {
            ICRegILGen reg = new FactoryWithContainerParamImpl(context.AddInstance(_factory), _implementationType);

            if (_liveScopeTrait.Lifetime == Lifetime.Singleton)
            {
                reg = new SingletonImpl(_implementationType, reg, context.SingletonCount);
                context.SingletonCount++;
            }
            context.AddCReg(_asTrait.GetAsTypesFor(_implementationType), _asTrait.PreserveExistingDefaults, (ICReg)reg);
        }
        public void Register(ContainerRegistrationContext context)
        {
            var reg = new InstanceImpl(_instance, context.AddInstance(_instance));

            context.AddCReg(_asTrait.GetAsTypesFor(_implementationType), _asTrait.PreserveExistingDefaults, reg);
        }