コード例 #1
0
        internal void Instantiate(ContainerService.Builder builder)
        {
            LifestyleAttribute lifestyle;

            if (builder.Type.IsSimpleType())
            {
                builder.SetError("can't create simple type");
            }
            else if (builder.Type == typeof(IContainer))
            {
                builder.AddInstance(this, false, false);
            }
            else if (builder.Configuration.ImplementationAssigned)
            {
                builder.AddInstance(builder.Configuration.Implementation, builder.Configuration.ContainerOwnsInstance, true);
            }
            else if (builder.Configuration.Factory != null)
            {
                if (!builder.Context.AnalizeDependenciesOnly)
                {
                    builder.CreateInstanceBy(CallTarget.F(builder.Configuration.Factory), builder.Configuration.ContainerOwnsInstance);
                }
            }
            else if (builder.Type.IsValueType)
            {
                builder.SetError("can't create value type");
            }
            else if (builder.Type.IsGenericType && builder.Type.ContainsGenericParameters)
            {
                builder.SetError("can't create open generic");
            }
            else if (!builder.CreateNew && builder.Type.TryGetCustomAttribute(out lifestyle) &&
                     lifestyle.Lifestyle == Lifestyle.PerRequest)
            {
                const string messageFormat = "service [{0}] with PerRequest lifestyle can't be resolved, use Func<{0}> instead";
                builder.SetError(string.Format(messageFormat, builder.Type.FormatName()));
            }
            else if (builder.Type.IsAbstract)
            {
                InstantiateInterface(builder);
            }
            else
            {
                InstantiateImplementation(builder);
            }
        }
コード例 #2
0
        internal ServiceDependency InstantiateDependency(ParameterInfo formalParameter, ContainerService.Builder builder)
        {
            ValueWithType actualArgument;

            if (builder.Arguments != null && builder.Arguments.TryGet(formalParameter.Name, out actualArgument))
            {
                return(containerContext.Constant(formalParameter, actualArgument.value));
            }
            var    parameters = builder.Configuration.ParametersSource;
            object actualParameter;

            if (parameters != null && parameters.TryGet(formalParameter.Name, formalParameter.ParameterType, out actualParameter))
            {
                return(containerContext.Constant(formalParameter, actualParameter));
            }
            var  dependencyConfiguration = builder.Configuration.GetOrNull(formalParameter);
            Type implementationType      = null;

            if (dependencyConfiguration != null)
            {
                if (dependencyConfiguration.ValueAssigned)
                {
                    return(containerContext.Constant(formalParameter, dependencyConfiguration.Value));
                }
                if (dependencyConfiguration.Factory != null)
                {
                    var dependencyBuilder = new ContainerService.Builder(new ServiceName(formalParameter.ParameterType))
                    {
                        Context        = builder.Context,
                        DependencyName = formalParameter.Name
                    };
                    builder.Context.Stack.Add(dependencyBuilder);
                    dependencyBuilder.CreateInstanceBy(CallTarget.F(dependencyConfiguration.Factory), true);
                    builder.Context.Stack.RemoveLast();
                    return(dependencyBuilder.GetService().AsDependency(containerContext, formalParameter.Name, false));
                }
                implementationType = dependencyConfiguration.ImplementationType;
            }
            implementationType = implementationType ?? formalParameter.ParameterType;
            FromResourceAttribute resourceAttribute;

            if (implementationType == typeof(Stream) && formalParameter.TryGetCustomAttribute(out resourceAttribute))
            {
                var resourceStream = builder.Type.Assembly.GetManifestResourceStream(builder.Type, resourceAttribute.Name);
                if (resourceStream == null)
                {
                    return(containerContext.Error(null, formalParameter.Name,
                                                  "can't find resource [{0}] in namespace of [{1}], assembly [{2}]",
                                                  resourceAttribute.Name, builder.Type, builder.Type.Assembly.GetName().Name));
                }
                return(containerContext.Resource(formalParameter, resourceAttribute.Name, resourceStream));
            }
            var dependencyName = ServiceName.Parse(implementationType.UnwrapEnumerable(),
                                                   InternalHelpers.ParseContracts(formalParameter));

            if (dependencyName.Type.IsSimpleType())
            {
                if (!formalParameter.HasDefaultValue)
                {
                    return(containerContext.Error(null, formalParameter.Name,
                                                  "parameter [{0}] of service [{1}] is not configured",
                                                  formalParameter.Name, builder.Type.FormatName()));
                }
                return(containerContext.Constant(formalParameter, formalParameter.DefaultValue));
            }
            var resultService = ResolveCore(dependencyName, false, null, builder.Context);

            if (resultService.Status.IsBad())
            {
                return(containerContext.ServiceError(resultService));
            }
            var isEnumerable = dependencyName.Type != implementationType;

            if (isEnumerable)
            {
                return(containerContext.Service(resultService, resultService.GetAllValues()));
            }
            if (resultService.Status == ServiceStatus.NotResolved)
            {
                if (formalParameter.HasDefaultValue)
                {
                    return(containerContext.Service(resultService, formalParameter.DefaultValue));
                }
                if (formalParameter.IsDefined <OptionalAttribute>() || formalParameter.IsDefined("CanBeNullAttribute"))
                {
                    return(containerContext.Service(resultService, null));
                }
                return(containerContext.NotResolved(resultService));
            }
            return(resultService.AsDependency(containerContext, null, false));
        }