예제 #1
0
 /// <inheritdoc />
 public void Validate()
 {
     foreach (var serviceRegistration in this.registrationRepository.GetAllRegistrations())
     {
         serviceRegistration.GetExpression(this.ContainerContext, ResolutionContext.New(this.RootScope), serviceRegistration.ServiceType);
     }
 }
예제 #2
0
        private Delegate ActivateFactoryDelegate(Type type, Type[] parameterTypes, IResolutionScope resolutionScope, object name, bool nullResultAllowed)
        {
            var resolutionContext = ResolutionContext.New(resolutionScope, nullResultAllowed);

            resolutionContext.AddParameterExpressions(type, parameterTypes.Select(p => p.AsParameter()).ToArray());

            var typeInfo = new TypeInformation {
                Type = type, DependencyName = name
            };
            var registration = this.containerContext.RegistrationRepository.GetRegistrationOrDefault(typeInfo, resolutionContext);

            var initExpression = registration == null?
                                 this.resolverSelector.GetResolverExpression(this.containerContext, typeInfo, resolutionContext) :
                                     registration.GetExpression(this.containerContext, resolutionContext, type);

            if (initExpression == null)
            {
                if (resolutionContext.NullResultAllowed)
                {
                    return(null);
                }
                else
                {
                    throw new ResolutionFailedException(type);
                }
            }

            var expression = initExpression.AsLambda(resolutionContext.ParameterExpressions.SelectMany(x => x));

            var factory = expression.CompileDynamicDelegate(resolutionContext);

            Swap.SwapValue(ref this.delegateCache.FactoryDelegates, (t1, t2, t3, t4, c) =>
                           c.AddOrUpdate(t1, t2), name ?? type, factory, Constants.DelegatePlaceholder, Constants.DelegatePlaceholder);
            return(factory(resolutionScope));
        }
예제 #3
0
 /// <inheritdoc />
 public void Validate()
 {
     foreach (var serviceRegistration in this.registrationRepository.GetRegistrationMappings())
     {
         serviceRegistration.Value.GetExpression(this.ContainerContext, ResolutionContext.New(this.RootScope), serviceRegistration.Key);
     }
 }
예제 #4
0
        public object Resolve(Type typeFrom, object name, bool nullResultAllowed = false)
        {
            var hash          = name.GetHashCode();
            var cachedFactory = this.serviceDelegates[hash & this.indexBound].GetOrDefault(hash, name);

            return(cachedFactory != null?cachedFactory(this) : this.Activate(ResolutionContext.New(this, nullResultAllowed), typeFrom, hash, name));
        }
예제 #5
0
        public TTo BuildUp <TTo>(TTo instance)
        {
            var typeTo            = instance.GetType();
            var resolutionContext = ResolutionContext.New(this);
            var metaInfo          = MetaInformation.GetOrCreateMetaInfo(typeTo);
            var expr = this.expressionBuilder.CreateBasicFillExpression(this.containerContext,
                                                                        metaInfo.InjectionMembers, metaInfo.InjectionMethods, instance.AsConstant(), resolutionContext, typeTo);
            var factory = expr.CompileDelegate(resolutionContext);

            return((TTo)factory(this));
        }
예제 #6
0
        /// <inheritdoc />
        public TTo BuildUp <TTo>(TTo instance)
        {
            var typeTo            = instance.GetType();
            var registration      = this.serviceRegistrator.PrepareContext(typeTo, typeTo);
            var resolutionContext = ResolutionContext.New(this);
            var expr = this.expressionBuilder.CreateFillExpression(this.containerContext, registration.CreateServiceRegistration(false),
                                                                   instance.AsConstant(), resolutionContext, typeTo);
            var factory = expr.CompileDelegate(resolutionContext);

            return((TTo)factory(this));
        }
예제 #7
0
        public void IpAddressConverterShouldReturnEmptyStringFromNull()
        {
            // Arrange
            var resolutionContext = ResolutionContext.New <byte[]>(null);

            // Act
            var ipAddressByteConverter = new IpAddressByteConverter();
            var ipAddressString        = ipAddressByteConverter.Convert(resolutionContext);

            // Assert
            var result = Assert.IsType <string>(ipAddressString);

            Assert.NotNull(result);
            Assert.Equal(result, string.Empty);
        }
예제 #8
0
        public TTo BuildUp <TTo>(TTo instance)
        {
            var typeTo            = typeof(TTo);
            var resolutionContext = ResolutionContext.New(this);
            var metaInfo          = MetaInformation.GetOrCreateMetaInfo(typeTo);
            var expression        = this.expressionBuilder.CreateBasicFillExpression(this.containerContext,
                                                                                     metaInfo.SelectInjectionMembers(RegistrationContextData.Empty,
                                                                                                                     this.containerContext.ContainerConfigurator.ContainerConfiguration),
                                                                                     metaInfo.GetInjectionMethods(),
                                                                                     instance.AsConstant(),
                                                                                     resolutionContext,
                                                                                     typeTo);

            return((TTo)expression.CompileDelegate(resolutionContext)(this));
        }
예제 #9
0
        public void IpAddressConverterShouldReturnIpAddressAsStringFromByteArray()
        {
            // Arrange
            var ipAddress = IPAddress.Loopback;

            var resolutionContext = ResolutionContext.New(ipAddress.GetAddressBytes());

            // Act
            var ipAddressByteConverter = new IpAddressByteConverter();
            var ipAddressString        = ipAddressByteConverter.Convert(resolutionContext);

            // Assert
            var result = Assert.IsType <string>(ipAddressString);

            Assert.NotNull(result);
            Assert.Equal(result, ipAddress.ToString());
        }
예제 #10
0
        public object Activate(Type type, params object[] arguments)
        {
            if (!type.IsResolvableType())
            {
                throw new ArgumentException($"The given type ({type.FullName}) could not be activated on the fly by the container.");
            }

            var resolutionContext = ResolutionContext.New(this, dependencyOverrides: arguments);
            var metaInfo          = MetaInformation.GetOrCreateMetaInfo(type);
            var expression        = this.expressionBuilder.CreateBasicExpression(this.containerContext,
                                                                                 metaInfo.GetConstructors(),
                                                                                 metaInfo.SelectInjectionMembers(RegistrationContextData.Empty,
                                                                                                                 this.containerContext.ContainerConfigurator.ContainerConfiguration),
                                                                                 metaInfo.GetInjectionMethods(),
                                                                                 resolutionContext,
                                                                                 type);

            return(expression.CompileDelegate(resolutionContext)(this));
        }
예제 #11
0
 public ResolutionResult Resolve(ResolutionResult source)
 {
     return(new ResolutionResult(ResolutionContext.New(
                                     string.Format("the value is {0}", source.Value))));
 }
예제 #12
0
        public object Resolve(Type typeFrom, bool nullResultAllowed = false, object[] dependencyOverrides = null)
        {
            var cachedFactory = this.delegateCache.ServiceDelegates.GetOrDefault(typeFrom);

            return(cachedFactory != null?cachedFactory(this) : this.Activate(ResolutionContext.New(this, nullResultAllowed, dependencyOverrides), typeFrom));
        }
예제 #13
0
 /// <inheritdoc />
 public bool CanResolve(Type typeFrom, object name = null) =>
 this.registrationRepository.ContainsRegistration(typeFrom, name) ||
 this.resolverSelector.CanResolve(this.ContainerContext, new TypeInformation {
     Type = typeFrom, DependencyName = name
 }, ResolutionContext.New(this.RootScope));