Back to "मनपसंद कॉन्फ़िग खंड विस्तार"

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

ASP.NET C#

मनपसंद कॉन्फ़िग खंड विस्तार

Friday, 27 September 2024

परिचय

ऐसा लगता है कि हर किसी के पास इस कोड का एक संस्करण है, मैं पहली बार यहाँ से आया था फिलिप डब्ल्यू. और हाल ही में मेरे पुराने सहयोगी फिल हैकिंग है उसका संस्करण.

बस इस पूर्णता के लिए मैं यह कैसे करते हैं के लिए.

क्यों?

विन्यास के साथ काम करना क्यों आसान है? उस समय पूर्वमी जिस तरह का उपयोग कर रहा है IOptions<T> और IOptionsSnapshot<T> लेकिन यह के साथ काम करने के लिए एक दर्द का एक सा है. यह तरीका आपको स्वाभाविक रूप से विन्यास के साथ काम करने देता है । सीमाएँ हैं कि आप उपयोग नहीं कर सकते IOptions<T> पैटर्न, तो आप अनुप्रयोग को फिर से चालू किए बिना विन्यास को फिर से लोड करने की क्षमता नहीं पा सकते. लेकिन ईमानदारी से यह एक ऐसी बात है जिसे मैं लगभग कभी नहीं करना चाहता ।

कोड

मेरे संस्करण में मैं हाल ही में स्थिर इंटरफेस सदस्यों को निर्धारित करने के लिए प्रयोग करता हूँ कि इस वर्ग के सभी उदाहरणों को घोषित करें Section गुण. कॉन्फ़िगरेशन से अंश प्राप्त करने के लिए यह उपयोग में लिया जाता है.

public interface IConfigSection {
    public static abstract string Section { get; }
}

प्रत्येक कार्यान्वयन के लिए जिसे आप निर्दिष्ट करते हैं

public class NewsletterConfig : IConfigSection
{
    public static string Section => "Newsletter";
    
    public string SchedulerServiceUrl { get; set; } = string.Empty;
    public string AppHostUrl { get; set; } = string.Empty;
}

इस मामले में यह कॉन्फ़िगरेशन में एक खंड के लिए देख रहा है 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 यह है कि अंदर पारित करने की जरूरत नहीं है.

मतलब अब आप के बारे में विकल्प है कैसे कॉन्फ़िग बाँध के बारे में।

एक और फायदा यह है कि अगर आपको बाद में इन मूल्यों का इस्तेमाल करना पड़े 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> पैटर्न.

logo

©2024 Scott Galloway