Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:CSF.FlexDi.Resolution.ResolutionResult"/> class.
        /// </summary>
        /// <param name="success">If set to <c>true</c> success.</param>
        /// <param name="resolutionPath">Resolution path.</param>
        /// <param name="resolvedObject">Resolved object.</param>
        public ResolutionResult(bool success, ResolutionPath resolutionPath, object resolvedObject)
        {
            if (resolutionPath == null)
            {
                throw new ArgumentNullException(nameof(resolutionPath));
            }

            IsSuccess      = success;
            ResolvedObject = resolvedObject;
        }
Beispiel #2
0
 /// <summary>
 /// Performs the same check as <see cref="M:CSF.FlexDi.Resolution.IDetectsCircularDependencies.HasCircularDependency(CSF.FlexDi.Registration.IServiceRegistration,CSF.FlexDi.Resolution.ResolutionPath)" /> but raises an exception if a circular dependency
 /// is found.
 /// </summary>
 /// <param name="registration">The registration to find.</param>
 /// <param name="resolutionPath">A resolution path.</param>
 public void ThrowOnCircularDependency(IServiceRegistration registration, ResolutionPath resolutionPath)
 {
     if (HasCircularDependency(registration, resolutionPath))
     {
         throw new CircularDependencyException(Resources.ExceptionFormats.CircularDependencyDetected)
               {
                   ResolutionPath = resolutionPath
               };
     }
 }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:CSF.FlexDi.Resolution.ResolutionRequest"/> class.
        /// </summary>
        /// <param name="serviceType">Service type.</param>
        /// <param name="name">Name.</param>
        /// <param name="resolutionPath">Resolution path.</param>
        public ResolutionRequest(Type serviceType, string name, ResolutionPath resolutionPath = null)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            ServiceType    = serviceType;
            Name           = name;
            ResolutionPath = resolutionPath ?? new ResolutionPath();
        }
Beispiel #4
0
        ResolutionRequest ConvertToResolutionRequest(ParameterInfo parameter,
                                                     ResolutionPath path,
                                                     IServiceRegistration registration)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            var childPath = path.CreateChild(registration);

            return(new ResolutionRequest(parameter.ParameterType, parameter.Name, childPath));
        }
Beispiel #5
0
        /// <summary>
        /// Gets a value which indicates whether the given resolution path contains the given registration.  This would
        /// indicate a circular dependency, because it means that in order to resolve the given registration, that
        /// same registration has been traversed again.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if a circular dependency was detected, <c>false</c> otherwise.</returns>
        /// <param name="registration">The registration to find.</param>
        /// <param name="resolutionPath">A resolution path.</param>
        public bool HasCircularDependency(IServiceRegistration registration, ResolutionPath resolutionPath)
        {
            if (resolutionPath == null)
            {
                throw new ArgumentNullException(nameof(resolutionPath));
            }
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            return(resolutionPath.Contains(registration));
        }
Beispiel #6
0
        /// <summary>
        /// Resolves a parameter for a <see cref="IFactoryAdapter"/>.
        /// </summary>
        /// <returns>The resolved parameter value.</returns>
        /// <param name="parameter">The parameter.</param>
        /// <param name="path">The resolution path.</param>
        /// <param name="registration">The registration for the service currently being resolved.</param>
        protected virtual object ResolveParameter(ParameterInfo parameter,
                                                  ResolutionPath path,
                                                  IServiceRegistration registration)
        {
            var request = ConvertToResolutionRequest(parameter, path, registration);
            var result  = resolver.Resolve(request);

            if (!result.IsSuccess)
            {
                var message = String.Format(Resources.ExceptionFormats.FailedToResolveParameter,
                                            parameter.ParameterType.FullName,
                                            parameter.Name);
                throw new CannotResolveParameterException(message)
                      {
                          ResolutionPath = path,
                      };
            }

            return(result.ResolvedObject);
        }
Beispiel #7
0
        /// <summary>
        /// Creates a service/component instance from a factory adapter, resolution path and registration.s
        /// </summary>
        /// <returns>The created component instance.</returns>
        /// <param name="factory">The factory adapter from which to create the instance.</param>
        /// <param name="path">The current resolution path.</param>
        /// <param name="registration">The registration for the component to be created.</param>
        public virtual object CreateFromFactory(IFactoryAdapter factory,
                                                ResolutionPath path,
                                                IServiceRegistration registration)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (!factory.RequiresParameterResolution)
            {
                return(factory.Execute(Enumerable.Empty <object>().ToArray()));
            }

            var parameters = factory.GetParameters();

            var resolvedParameters = parameters
                                     .Select(param => ResolveParameter(param, path, registration))
                                     .ToArray();

            return(factory.Execute(resolvedParameters));
        }
Beispiel #8
0
 /// <summary>
 /// Creates an instance of <see cref="ResolutionResult"/> representing successful resolution.
 /// </summary>
 /// <param name="resolutionPath">Resolution path.</param>
 /// <param name="resolvedObject">Resolved object.</param>
 public static ResolutionResult Success(ResolutionPath resolutionPath, object resolvedObject)
 => new ResolutionResult(true, resolutionPath, resolvedObject);
Beispiel #9
0
 /// <summary>
 /// Creates an instance of <see cref="ResolutionResult"/> representing failed resolution.
 /// </summary>
 /// <param name="resolutionPath">Resolution path.</param>
 public static ResolutionResult Failure(ResolutionPath resolutionPath)
 => new ResolutionResult(false, resolutionPath, null);
Beispiel #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:CSF.FlexDi.Resolution.ResolutionRequest"/> class.
 /// </summary>
 /// <param name="serviceType">Service type.</param>
 /// <param name="resolutionPath">Resolution path.</param>
 public ResolutionRequest(Type serviceType, ResolutionPath resolutionPath = null)
     : this(serviceType, null, resolutionPath)
 {
 }