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
إمتدادات القسم الخاص بالتركيب
Friday, 27 September 2024
المقدمة
يبدو أن الجميع لديه نسخة من هذا الرمز , لقد נתקלت في هذا المقاربة لأول مرة من فيليب و وفي الآونة الأخيرة كان زميلي السابق فيل هاك إصداره.
فقط لإنهاء هذا ما أفعله
لماذا
السبب هو لجعل الأمر أسهل للعمل مع конфигурациة. في الوقت الراهن الطريقة الأولى تستخدم IOptions<T> و IOptionsSnapshot<T> لكن هذا يصعب عمله قليلا. هذه المقاربة تسمح لك بالعمل مع الإعداد بطريقة أكثر طبيعية
المحدودة هي أنك لا تستطيع IOptions<T> النمط , بحيث لا تستطيع ' لا تحصل على القدرة على إعادة تحميل الإعداد دون إعادة بدأ التطبيق МSK2 لكن بصراحة هذا شيء لا أريد فعله تقريباً قط
الرمز
في إصداري أستخدم أعضاء الواجهة المتحركة الأخيرة لتحديد أن كل الأمثلة لهذه الفئة يجب أن تعلن Section property. هذا يستخدم للحصول على القسم من ال構成.
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 لا يحتاج إلى أن يمر ذلك في .
مما يعني أن لديك الآن خيارات حول كيفية ربط 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 {
}
في الختام
حسناً، أن ' هو ... بسيط جداً لكنه ' تقنية أستخدمها في جميع مشاريعي . إنها طريقة جميلة للعمل مع الإعداد و أعتقد أنها "M SK5" أكثر طبيعية من IOptions<T> النمط.