/// <inheritdoc/>
 /// <remarks>
 /// The <typeparamref name="T"/> object should have a constructor that
 /// receives a single parameter of the type
 /// <see cref="SqlConnectionProvider"/>.
 /// </remarks>
 public T CreateProvider(IDictionary <string, string> options)
 {
     return(CreateProvider(options,
                           sql_connection_provider =>
                           RuntimeTypeFactory <T>
                           .CreateInstance(typeof(T), sql_connection_provider)));
 }
Пример #2
0
 /// <summary>
 /// Creates new instances to all types that can be assigned
 /// to <typeparamref name="T" />.
 /// the types.
 /// </summary>
 /// <param name="types">
 /// A list of <see cref="Type"/> objects containing the type to be
 /// filtered and instantiated.
 /// </param>
 /// <param name="fallback">
 /// A value that indicates if we should fallback to the default constructor
 /// when a constructor that accepts the given <paramref name="args"/> as
 /// parameters is not found. Default to <c>true</c>.
 /// </param>
 /// <param name="throw">
 /// A value that indicates if exceptions should be propagated to the
 /// caller or silently ignored. Default to <c>true</c>.
 /// </param>
 /// <param name="args">
 /// An array of arguments that match the parameters of the constructor to
 /// invoke. If args is an empty array or null, or if a matching constructor
 /// is not found the constructor that takes no parameters(the default
 /// constructor) is invoked.
 /// </param>
 /// <returns>
 /// An collection of <typeparamref name="T"/> containing an instance for
 /// each type of the <paramref name="types"/> list that can be assigned to
 /// <typeparamref name="T" />.
 /// </returns>
 public static IEnumerable <T> CreateInstances <T>(
     this IEnumerable <Type> types, bool fallback, bool @throw,
     params object[] args) where T : class
 {
     return
         (types
          .Where(
              t =>
              typeof(T).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
          .Select(t =>
                  RuntimeTypeFactory <T>
                  .CreateInstance(t, fallback, @throw, args)));
 }
Пример #3
0
        /// <summary>
        /// Creates an instance of all configured providers that has a factory
        /// that implements the <see cref="IProviderFactory"/> intrface and has a
        /// constructor that accepts no parameters or a single parameter of the type
        /// <see cref="IConfiguration"/>.
        /// </summary>
        /// <param name="predicate">
        /// A <see cref="Func{TResult}"/> that is used to evaluate if we need to
        /// create a instance of the given provider node.
        /// </param>
        /// <param name="settings">
        /// A <see cref="IConfiguration"/> containing the configured providers.
        /// </param>
        /// <returns>
        /// A array containing all providers that was created using the configured
        /// factories that implements the <see cref="IProviderFactory"/> interface.
        /// </returns>
        public static TResult[] CreateProviders <TFactory, TResult>(
            this IConfiguration settings,
            Func <IProvidersNodeGroup, IProviderNode, bool> predicate,
            Func <TFactory, IDictionary <string, string>, TResult> factory)
            where TFactory : class
        {
            var providers = settings.Providers;
            var list      = new List <TResult>();

            foreach (IProvidersNodeGroup group in providers)
            {
                foreach (IProviderNode node in group)
                {
                    if (!predicate(group, node))
                    {
                        continue;
                    }

                    Type     type      = GetType(node);
                    TFactory factory_t = RuntimeTypeFactory <TFactory>
                                         .CreateInstance(type, true, false, settings);

                    if (factory_t != null)
                    {
                        try {
                            list.Add(factory(factory_t, node.Options.ToDictionary()));
                        } catch (Exception e) {
                            MustLogger.ForCurrentProcess.Error(
                                StringResources.Log_MethodThrowsException.Fmt(
                                    "CreateProviders", kClassName), e);
                            throw new ConfigurationException(e);
                        }
                    }
                }
            }
            return(list.ToArray());
        }