mvvm-toolkit-di
यह क्या करती है
यह 'skill' आपके कोड के उन हिस्सों (जिन्हें ViewModel कहते हैं) को उन हिस्सों से जोड़ने में मदद करता है जो सेवाएं देते हैं (जैसे फ़ाइलें सेव करना या संदेश भेजना), और यह सब व्यवस्थित तरीके से होता है। यह 'Dependency Injection' नाम के सिस्टम का इस्तेमाल करता है, ताकि सही हिस्से अपने आप एक-दूसरे को ढूंढ लें।
कब इस्तेमाल करें
- ✓जब आप ग्राफ़िकल इंटरफ़ेस वाला नया ऐप बना रहे हों (WPF, MAUI आदि) और आपको यह व्यवस्थित करना हो कि आपके प्रोग्राम के अलग-अलग हिस्से आपस में कैसे बात करें।
- ✓यह तय करने के लिए कि कोई सेवा या ViewModel कितने समय तक मौजूद रहे (हमेशा के लिए, सिर्फ़ ज़रूरत पड़ने पर, आदि)।
- ✓यह सुनिश्चित करने के लिए कि ऐप के हिस्सों के बीच संदेश संचार सही तरीके से सेट हो और जहां ज़रूरत हो वहां उपलब्ध हो।
Skill का लिंक
https://github.com/github/awesome-copilot/blob/main/skills/mvvm-toolkit-di/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“मैं अपने ऐप को कैसे सेट करूं ताकि उसे पता रहे कि मेरे ViewModels और सेवाएं कैसे बनानी और देनी हैं?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
यह 'Dependency Injection' नाम के एक सिस्टम का इस्तेमाल करता है, जो एक आयोजक (organizer) की तरह काम करता है। आप उसे बताते हैं कि कौन-कौन सी सेवाएं मौजूद हैं और उन्हें कैसे बनाना है, और वह उन्हें अपने आप उस जगह पहुंचा देता है जहां उनकी ज़रूरत होती है, आमतौर पर क्लास के constructor के ज़रिए।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
CommunityToolkit.Mvvm + Microsoft.Extensions.DependencyInjection
The MVVM Toolkit deliberately ships no DI container — it composes with
Microsoft.Extensions.DependencyInjection, the same container ASP.NET
Core, Worker services, and the .NET Generic Host use.
TL;DR. Build the service provider once at startup (prefer
Host.CreateDefaultBuilder()). Register services and ViewModels. Inject through constructors. AvoidIoc.Default.GetService<T>()in user code.
When to use this skill
- Standing up the composition root for a new XAML app (WPF, WinUI 3, MAUI, Uno, Avalonia)
- Choosing service/VM lifetimes
- Wiring
IMessengeronce and injecting it intoObservableRecipientViewModels - Resolving a page's ViewModel without coupling to a service locator
- Diagnosing "Unable to resolve service for type X while attempting to activate Y"
For source generators and ViewModel patterns see the mvvm-toolkit
skill. For Messenger pub/sub see mvvm-toolkit-messenger.
Recommended composition root (Generic Host)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CommunityToolkit.Mvvm.Messaging;
public partial class App : Application
{
public IHost Host { get; }
public App()
{
Host = Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder()
.ConfigureServices((_, services) =>
{
services.AddSingleton<IFilesService, FilesService>();
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);
services.AddSingleton<ShellViewModel>();
services.AddTransient<ContactViewModel>();
services.AddTransient<EditorViewModel>();
})
.Build();
}
public static T GetService<T>() where T : class =>
((App)Current).Host.Services.GetRequiredService<T>();
}
Generic Host benefits:
appsettings.jsonbinding viaMicrosoft.Extensions.Configuration- Logging via
Microsoft.Extensions.Logging - Hosted services (
IHostedService) for background work - Scope validation in development builds
WPF and Windows Forms must integrate the host lifetime with the app lifetime — see Use the .NET Generic Host in a WPF app.
Without Generic Host
When you only need a service container and want zero extra dependencies:
var services = new ServiceCollection();
services.AddSingleton<IFilesService, FilesService>();
services.AddTransient<ContactViewModel>();
ServiceProvider provider = services.BuildServiceProvider();
Constructor injection
Inject services and child ViewModels through the constructor:
public sealed partial class ContactViewModel(
IFilesService files,
IMessenger messenger,
ILogger<ContactViewModel> logger)
: ObservableRecipient(messenger)
{
[ObservableProperty]
private string? name;
[RelayCommand]
private async Task SaveAsync()
{
logger.LogInformation("Saving {Name}", Name);
await files.SaveAsync(Name!);
}
}
Why constructor injection beats a service locator:
- Dependencies are explicit and visible at the call site
- Unit tests inject fakes/mocks directly
- The DI container validates the dependency graph at startup
- Missing registrations throw immediately, not at first use
Lifetimes
| Lifetime | Method | Typical use in XAML apps |
|---|---|---|
| Singleton | AddSingleton<T> |
Shell/main-window VM, settings, file/HTTP services, the shared IMessenger, app-wide caches |
| Transient | AddTransient<T> |
Per-page or per-document ViewModels (a fresh instance every resolve) |
| Scoped | AddScoped<T> |
Rarely needed in client apps; useful with explicit IServiceScope (e.g., per-window scopes) |
services.AddSingleton<ShellViewModel>(); // 1 instance for app lifetime
services.AddTransient<NoteViewModel>(); // new instance per resolve
services.AddScoped<DialogService>(); // 1 per scope (rare)
Resolving in a View
Resolve the page's root ViewModel in code-behind, then let it pull its own dependencies:
public sealed partial class ContactPage : Page
{
public ContactViewModel ViewModel { get; }
…
अक्सर पूछे जाने वाले सवाल
'Dependency Injection' क्या है?
यह कोड को व्यवस्थित करने का एक तरीका है, जिसमें कोई क्लास अपनी dependencies (यानी वे दूसरी क्लासें जिनकी उसे ज़रूरत है) खुद नहीं बनाती, बल्कि वे बाहर से 'inject' यानी दी जाती हैं। इससे कोड ज़्यादा लचीला और टेस्ट करने में आसान बनता है।
Singleton, Transient और Scoped में क्या अंतर है?
Singleton: पूरे ऐप की ज़िंदगी में सिर्फ़ एक ही instance बनता है। Transient: हर बार मांगे जाने पर नया instance बनता है। Scoped: हर खास 'scope' के लिए एक instance बनता है, जैसे कोई window या request (डेस्कटॉप ऐप में यह कम इस्तेमाल होता है)।
मुझे अपने ViewModels खुद क्यों नहीं बनाने चाहिए?
हाथ से बनाने पर कोड को संभालना और टेस्ट करना मुश्किल हो सकता है। 'Dependency Injection' का इस्तेमाल करने से dependencies सही तरीके से मिलती हैं, और आपको हर बार यह सोचना नहीं पड़ता कि उन्हें कैसे बनाना है।
जानकारी
लेखक का मूल विवरण
Wire CommunityToolkit.Mvvm ViewModels into Microsoft.Extensions.DependencyInjection. Covers the .NET Generic Host composition root, constructor injection, service lifetimes (Singleton / Transient / Scoped), IMessenger registration, resolving ViewModels in Views, keyed services, testing seams, and the legacy Ioc.Default escape hatch. Use across WPF, WinUI 3, .NET MAUI, Uno, and Avalonia.
repo: github/awesome-copilot
लोकप्रियता
- GitHub स्टार
- 40k
- रैंकिंग में स्थान
- #7302
- लाइसेंस
- MIT
- अपडेट की गई
- 15/10/2026
कमेंट
- …
इस repository की और skills
acquire-codebase-knowledge
40kआपके एजेंट को किसी मौजूदा code प्रोजेक्ट को समझने और उसका documentation बनाने में मदद करता है। यह नए लोगों को प्रोजेक्ट से जोड़ने या architecture का सार जानने में उपयोगी है।
acreadiness-assess
40kआपके एजेंट को किसी प्रोजेक्ट की AI के लिए तैयारी जाँचने और रिपोर्ट बनाने देता है। यह समझने में उपयोगी है कि प्रोजेक्ट AI tools इस्तेमाल करने के लिए कितना तैयार है।
acreadiness-generate-instructions
40kआपके एजेंट को assessment के आधार पर AI tools के लिए निर्देश बनाने में मदद करता है। यह बताता है कि AI कोड के साथ बेहतर कैसे काम करे और पहचानी गई कमियाँ कैसे दूर हों।
acreadiness-policy
40kआपके एजेंट को AI प्रोजेक्ट assessment के लिए नीतियाँ तय करने या लागू करने में मदद करता है। यह प्रोजेक्ट्स के मूल्यांकन को मानक बनाने और compliance सुनिश्चित करने में उपयोगी है।
मिलती-जुलती skills
api-connector-builder
280kयह आपके एजेंट को मौजूदा pattern के हिसाब से नए API connections बनाने देता है। जब बिना कोई नया तरीका बनाए एक और integration जोड़ना हो, तब इसका इस्तेमाल करें।
api-design
280kयह आपके एजेंट को best practices के हिसाब से REST APIs डिज़ाइन करने में मदद करता है। यह तय करने में काम आता है कि APIs आपस में कैसे बात करें, errors कैसे दिखें, और data को कैसे व्यवस्थित करें।
backend-patterns
280kआपके एजेंट को server-side architecture patterns और database optimization में मदद करता है। server पर चलने वाला code बनाने या review करने के लिए उपयोगी है।