DEV Community

Cover image for Simplifying Dependency Injection in .NET
Guillaume Faas for Vonage

Posted on • Originally published at developer.vonage.com

Simplifying Dependency Injection in .NET

Hey there, fellow developers!

We've got some exciting news to share about our .NET SDK!
We've just rolled out a fantastic new feature that will make your lives much easier.
You know how initializing the client used to be a bit of a pain, right?
Well, fear no more!

Since version v6.4.0, we integrated a solution that takes the hassle out of the process.
Now, you can use a cool extension method to register our client in the ASP.NET default Dependency Injection container.
Intrigued? Keep reading to find out more!

Standard Client Initialization

Imagine we'd like to use Meetings API features.
We would have to initialize an instance of the main client (VonageClient) to fetch the specific MeetingsClient.

Credentials credentials = ...
VonageClient vonageClient = new VonageClient(credentials);
IMeetingsClient meetingsClient = vonageClient.MeetingsClient;
Enter fullscreen mode Exit fullscreen mode

It's indeed quite straightforward as the client only expects a Credentials object to be created.
So far, nothing new; you've been there.

Even if it's relatively easy, it has a few downsides:

  • You must create a VonageClient while you only need a IMeetingsClient
  • You create unnecessary coupling to the VonageClient implementation, which makes your code harder to test

And there's more to it: if you want to inject any of our clients, you must register all of them in your container. Manually.

There's definitely room for some improvement.

Enters our DI Registration!

Okay, let's get to the good stuff!
With our new extension method, you no longer have to do that.

Check out how easy it is to use:

using Microsoft.Extensions.DependencyInjection;
using Vonage;
using Vonage.Extensions;

// ...

public void ConfigureServices(IServiceCollection services)
{
    //...

    // Initialize credentials
    Credentials credentials = ...
    // Register our clients with a 'Transient' lifetime...
    services.AddVonageClientTransient(credentials);
    // Or a 'Scoped' lifetime
    services.AddVonageClientScoped(credentials);

    // ...
}
Enter fullscreen mode Exit fullscreen mode

That's it!

It relies on ASP.NET DI Container from Microsoft.Extensions.DependencyInjection, which means it's compatible as long as you're handling an IServiceCollection instance.
This is provided by default on ASP.NET WebApi and WebApp, but you can also integrate it with ConsoleApps and Services.

You can choose between two ServiceLifetime options:

  • Transient: objects are always different
  • Scoped: objects are the same for a given request but differ across each new request

By calling services.AddVonageClient{lifetime}, you're telling the DI container set up the VonageClient for you.

You can now enjoy dependency resolution with the main client or any subclient:

[ApiController]
[Route("[controller]")]
public class VonageController : ControllerBase
{
    ...

    public VonageController(VonageClient client) => this.client = client;
}

[ApiController]
[Route("[controller]")]
public class VonageController : ControllerBase
{
    ...

    public VonageController(IMeetingsClient client) => this.client = client;
}
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

We're proud to make your developer journey smoother with new quality-of-life improvements.
It's never been easier to integrate our SDK into your .NET applications.

And guess what?
Your feedback and contributions matter to us!

So, feel free to hit up our GitHub repository to report issues, suggest improvements, or even contribute your pull requests.
If you have questions, join us on the Vonage Developer Slack, and we will get back to you.

Happy coding, friends!

Top comments (0)