Beispiel #1
0
 /// <summary>
 /// Creates an instance of this exception.
 /// </summary>
 /// <param name="context">The context in which the instance was requested.</param>
 /// <param name="name">The name of the instance that was requested.</param>
 /// <param name="instanceProvider">The instance provider, which was unable to provide an instance of the specified type.</param>
 /// <param name="reason"></param>
 internal IncompatibleInstanceTypeException(IContext context, RIName name, IInstanceProvider instanceProvider, string reason)
     : base($"The instance by the name '{name}' cannot be instantiated. {reason}")
 {
     Context          = context;
     Name             = name;
     InstanceProvider = instanceProvider;
 }
Beispiel #2
0
        /// <summary>
        /// Retrieves the instance provider that is registered by the specified name. If no such instance provider is found then an
        /// <see cref="NoSuchInstanceProviderException"/> is thrown.
        /// </summary>
        /// <param name="name">The name of the instance provider that is retrieved.</param>
        /// <returns>The found sinstance provider.</returns>
        public IInstanceProvider GetInstanceProvider(RIName name)
        {
            if (!_instanceProviderDictionary.ContainsKey(name))
            {
                throw new NoSuchInstanceProviderException(name);
            }

            return(_instanceProviderDictionary[name]);
        }
Beispiel #3
0
        /// <summary>
        /// Registers an instance provider in the context by the specified name. If an instance provider by the same name already exists,
        /// then a <see cref="DuplicateInstanceNameException"/> is thrown.
        /// </summary>
        /// <param name="name">The name by which the instance provider will be registered in the context.</param>
        /// <param name="instanceProvider">The instance provider that is registered.</param>
        public void AddNamedInstanceProvider(RIName name, IInstanceProvider instanceProvider)
        {
            if (_instanceProviderDictionary.ContainsKey(name))
            {
                throw new DuplicateInstanceNameException(name);
            }

            _instanceProviderDictionary[name] = instanceProvider;
        }
Beispiel #4
0
        /// <summary>
        /// Looks up all registered instance providers for one registered by the specified name. When found the instance provider is
        /// requested to provide its instance, which hereafter is returned.
        /// If no such instance provider is found, then an <see cref="InstanceNotFoundException"/> is thrown.
        /// </summary>
        /// <param name="name">The name of the instance provider to look up.</param>
        /// <returns>The instance returned by the found instance provider.</returns>
        public object GetInstance(RIName name)
        {
            IInstanceProvider instanceProvider;

            if (!_instanceProviderDictionary.TryGetValue(name, out instanceProvider))
            {
                throw new InstanceNotFoundException(this, name);
            }
            return(instanceProvider.GetInstance());
        }
Beispiel #5
0
        /// <summary>
        /// Looks up all registered instance providers for one registered by the specified name. When found the instance provider is
        /// requested to provide its instance. Hereafter the method verifies whether the provided instance is a descendant of the specified
        /// type (checkType). If this is the case, then the instance is returned.
        /// If no such instance provider is found, then an <see cref= "InstanceNotFoundException"/> is thrown.
        /// If the instance was not a descendant of the specified type, then an <see cref="IncompatibleInstanceTypeException"/> is thrown.
        /// </summary>
        /// <param name="name">The name of the instance provider to look up.</param>
        /// <param name="checkType">The type against which the type of the provided instance is tested.</param>
        /// <returns>The provided instance.</returns>
        public object GetInstance(RIName name, Type checkType)
        {
            IInstanceProvider instanceProvider;

            if (!_instanceProviderDictionary.TryGetValue(name, out instanceProvider))
            {
                throw new InstanceNotFoundException(this, name);
            }

            var definedInstanceType = instanceProvider.DefinedType;

            if (!checkType.IsAssignableFrom(definedInstanceType))
            {
                throw new IncompatibleInstanceTypeException(this, name, instanceProvider, $"The type {definedInstanceType.FullName} does not implement or inherit {checkType.FullName}.");
            }

            return(instanceProvider.GetInstance());
        }
Beispiel #6
0
 /// <summary>
 /// Creates an instance of the reference instance provider.
 /// </summary>
 /// <param name="reference">The name by which the referenced instance provider is known.</param>
 /// <param name="context">The context in which the referenced instance provider is searched.</param>
 public ReferenceProvider(string reference, IContext context)
 {
     Reference = reference;
     _context  = context;
 }
 /// <summary>
 /// Create an instance of this exception.
 /// </summary>
 /// <param name="context">The context in which the instance was requested.</param>
 /// <param name="instanceName">The name of the instance that was requested.</param>
 internal InstanceNotFoundException(IContext context, RIName instanceName) : base($"No instance found in context by the name '{instanceName}'.")
 {
     Context      = context;
     InstanceName = instanceName;
 }
Beispiel #8
0
 /// <summary>
 /// Creates an instance of this exception.
 /// </summary>
 /// <param name="name">The name that is duplicated.</param>
 public DuplicateInstanceNameException(RIName name)
     : base($"An instance by the name '{name}' is already contained in the context.")
 {
     Name = name;
 }
Beispiel #9
0
 /// <summary>
 /// Looks up all registered instance providers for one registered by the specified name. When found the instance provider is
 /// requested to provide its instance. Hereafter the method verifies whether the provided instance is a descendant of the specified
 /// type (T). If this is the case, then the instance is casted to the specified type and then returned.
 /// If no such instance provider is found, then an <see cref= "InstanceNotFoundException"/> is thrown.
 /// If the instance was not a descendant of the specified type, then an <see cref="IncompatibleInstanceTypeException"/> is thrown.
 /// </summary>
 /// <typeparam name="T">The type by which the instance is checked and casted before returned.</typeparam>
 /// <param name="instanceName">The name by which the instance provider is registered.</param>
 /// <returns>The provided instance.</returns>
 public T GetInstance <T>(RIName instanceName)
 {
     return((T)GetInstance(instanceName, typeof(T)));
 }
Beispiel #10
0
 /// <summary>
 /// Creates an instance of this exception.
 /// </summary>
 /// <param name="name">The name by which no instance provider could be found.</param>
 internal NoSuchInstanceProviderException(RIName name)
     : base($"No instance provider found by the name '{name}'.")
 {
     Name = name;
 }