Exemple #1
0
        public static TConfig BindToPOCO <TConfig>(this IConfiguration configuration) where TConfig : class, new()
        {
            Conditions.Begin()
            .NonNull(configuration, $"{nameof(configuration)} cannot be null")
            .ThrowIfAny();

            var config = new TConfig();

            configuration.Bind(config);
            return(config);
        }
Exemple #2
0
        /// <summary>
        /// DI of POCO config objects without having to spread IOptions<> around.
        /// Thank you, https://www.strathweb.com/2016/09/strongly-typed-configuration-in-asp-net-core-without-ioptionst/
        /// </summary>
        /// <typeparam name="TConfig"></typeparam>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns>The configured poco</returns>
        public static TConfig ConfigurePOCO <TConfig>(this IServiceCollection services, IConfiguration configuration) where TConfig : class, new()
        {
            Conditions.Begin()
            .NonNull(services, $"{nameof(services)} cannot be null")
            .ThrowIfAny();

            var config = configuration.BindToPOCO <TConfig>();

            services.AddSingleton(config);
            return(config);
        }
Exemple #3
0
        public ServersConfig(string niceName, string servers)
        {
            OriginalServerValue = servers;
            AvailableServers    = servers?
                                  .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                  .Select(inServer => QualifyServer(inServer))
                                  .ToArray() ?? null;

            Conditions.Begin()
            .HasContent(AvailableServers, $"{niceName} // No servers defined. Must be at least 1 in a csv string.")
            .ThrowIfAny();
        }
Exemple #4
0
        public static TConfig BindPOCOViaAttributes <TConfig>(this IServiceCollection services, IConfiguration configuration) where TConfig : class, new()
        {
            Conditions.Begin()
            .NonNull(configuration, $"{nameof(configuration)} cannot be null")
            .ThrowIfAny();

            var config = new TConfig();

            foreach (var propInfo in config.GetType().GetTypeInfo().GetProperties())
            {
                JsonPropertyAttribute serializationInfo = propInfo.GetCustomAttribute <JsonPropertyAttribute>();
                if (serializationInfo != null)
                {
                    var itsValue = configuration[serializationInfo.PropertyName];
                    propInfo.SetValue(config, itsValue);
                }
            }

            services.AddSingleton(config);
            return(config);
        }