मनपसंद कॉन्फ़िग खण्ड विस्तार
परिचय
ऐसा लगता है जैसे हर कोई इस कोड का एक संस्करण , मैं पहली बार इस दृष्टिकोण से आ गया फिलिप डब्लू और हाल ही में मेरे पुराने colleague Phil Haack है उसका संस्करण.
सिर्फ पूरा करने के लिए यह है कैसे मैं इसे करता हूँ
क्यों
क्यों यह कॉन्फ़िगरेशन के साथ काम करने में आसान बनाने के लिए है IOptions<T> और IOptionsSnapshot<T> लेकिन इस के साथ काम करने में थोड़ा दर्द है. यह दृष्टिकोण आपको एक अधिक प्राकृतिक तरीके से कॉन्फ़िगरेशन के साथ कार्य करने देता है
सीमाएं है कि आप कर सकते हैं-' IOptions<T> पैटर्न, तो आप नहीं है' अनुप्रयोग को फिर से आरंभ करने के बिना कॉन्फ़िगरेशन को पुनः लोड करने की क्षमता नहीं मिलता
कोड
मेरे संस्करण में मैं इस वर्ग के सभी इंस्टैंसों को एक घोषित करने की निर्दिष्ट करने के लिए हाल ही में स्थिर इंटरफेस सदस्य का उपयोग करता है Section गुण. यह कॉन्फ़िगरेशन से खण्ड प्राप्त करने के लिए प्रयोग किया जाता है
public interface IConfigSection {
public static abstract string Section { get; }
}
तो प्रत्येक कार्यान्वयन के लिए आप फिर निर्दिष्ट करें कि क्या सेक्शन यह bound होना चाहिए
public class NewsletterConfig : IConfigSection
{
public static string Section => "Newsletter";
public string SchedulerServiceUrl { get; set; } = string.Empty;
public string AppHostUrl { get; set; } = string.Empty;
}
इस मामले में यह कनफिगरेशन में एक खंड के लिए खोज रहा है called Newsletter.
"Newsletter": {
"SchedulerServiceUrl" : "http://localhost:5000",
"AppHostUrl" : "https://localhost:7240"
}
तब हम इस में बांधने के लिए सक्षम हो जाएगा Program.cs जैसे फ़ाइल:
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
services.ConfigurePOCO<NewsletterConfig>(config);
हम भी कॉन्फ़िगरेशन के मूल्य प्राप्त कर सकते हैं Program.cs जैसे फ़ाइल:
var newsletterConfig = services.ConfigurePOCO<NewsletterConfig>(config);
या फिर के लिए WebApplicationBuilder जैसे है
var newsletterConfig = builder.Configure<NewsletterConfig>();
नोट: चूंकि बिल्डर के पास पहुँच है ConfigurationManager इसे पास करने की ज़रूरत नहीं है
इसका अर्थ है कि अब आप config को कैसे बाइंड करने के बारे में विकल्प हैं
एक और लाभ है कि अगर आप इन मूल्यों को अपने बाद में उपयोग करने की जरूरत है Program.cs फ़ाइल तो वस्तु आपको उपलब्ध है
एक्सटेंशन विधि
यह सब सक्षम करने के लिए हम एक काफी सरल विस्तार विधि है जो वर्ग में विन्यास को बाइंडिंग का काम करता है
निम्न कोड निम्नलिखित अनुमति देता है:
// These get the values and bind them to the class while adding these to Singleton Scope
var newsletterConfig = services.ConfigurePOCO<NewsletterConfig>(config);
var newsletterConfig = services.ConfigurePOCO<NewsletterConfig>(configuration.GetSection(NewsletterConfig.Section));
// Or for Builder...These obviously only work for ASP.NET Core applications, take them out if you are using this in a different context.
var newsletterConfig = builder.Configure<NewsletterConfig>();
var newsletterConfig = builder.Configure<NewsletterConfig>(NewsletterConfig.Section);
// These just return a dictionary, which can be useful to get all the values in a section
var newsletterConfig = builder.GetConfigSection<NewsletterConfig>();
यह सब निम्नलिखित एक्सटेंशन वर्ग द्वारा सक्षम है
आप देख सकते हैं कि इस का मुख्य उद्वेग स्थिर अंतरफलक सदस्यों का उपयोग से धारा नाम निर्दिष्ट करने के लिए है
namespace Mostlylucid.Shared.Config;
public static class ConfigExtensions {
public static TConfig ConfigurePOCO<TConfig>(this IServiceCollection services, IConfigurationSection configuration)
where TConfig : class, new() {
if (services == null) throw new ArgumentNullException(nameof(services));
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
var config = new TConfig();
configuration.Bind(config);
services.AddSingleton(config);
return config;
}
public static TConfig ConfigurePOCO<TConfig>(this IServiceCollection services, ConfigurationManager configuration)
where TConfig : class, IConfigSection, new()
{
var sectionName = TConfig.Section;
var section = configuration.GetSection(sectionName);
return services.ConfigurePOCO<TConfig>(section);
}
public static TConfig Configure<TConfig>(this WebApplicationBuilder builder)
where TConfig : class, IConfigSection, new() {
var services = builder.Services;
var configuration = builder.Configuration;
var sectionName = TConfig.Section;
return services.ConfigurePOCO<TConfig>(configuration.GetSection(sectionName));
}
public static TConfig GetConfig<TConfig>(this WebApplicationBuilder builder)
where TConfig : class, IConfigSection, new() {
var configuration = builder.Configuration;
var sectionName = TConfig.Section;
var section = configuration.GetSection(sectionName).Get<TConfig>();
return section;
}
public static Dictionary<string, object> GetConfigSection(this IConfiguration configuration, string sectionName) {
var section = configuration.GetSection(sectionName);
var result = new Dictionary<string, object>();
foreach (var child in section.GetChildren()) {
var key = child.Key;
var value = child.Value;
result.Add(key, value);
}
return result;
}
public static Dictionary<string, object> GetConfigSection<TConfig>(this WebApplicationBuilder builder)
where TConfig : class, IConfigSection, new() {
var configuration = builder.Configuration;
var sectionName = TConfig.Section;
return configuration.GetConfigSection(sectionName);
}
}
उपयोग में
इन का उपयोग करने के लिए काफी सरल है
public class NewsletterService(NewsletterConfig config {
}
अंत में
अच्छी तरह से यह है कि-' यह है, ... बहुत सरल लेकिन यह है ' एक तकनीक मैं अपने सभी परियोजनाओं में प्रयोग करता हूँ। IOptions<T> पैटर्न