Back to results for “Dependency Injection”
.NET MAUI › Native Interop & Libraries

How do you expose native SDK features cleanly through dependency injection?

Advanced

Answer

Define a shared interface for the capability, implement it per platform (calling the bound native SDK) under Platforms/, and register it in DI. Consumers depend only on the interface, which isolates the binding, allows mocking in tests, and lets you swap SDKs without touching app logic.
public interface IAnalytics { void Track(string evt); }
// Platforms/Android + Platforms/iOS implement via native SDKs
builder.Services.AddSingleton<IAnalytics, Analytics>();
Real-world example Firebase Analytics (native on each platform) sits behind one IAnalytics so view models call a single Track method.

Common follow-ups: Why hide the SDK behind an interface? | How does this aid testing?