googleads / google-ads-dotnet

This project hosts the .NET client library for the Google Ads API.

Home Page:https://developers.google.com/google-ads/api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Configure using the options pattern

jeffdahl opened this issue · comments

The options pattern simplifies configuration. When using a settings.json file, instead of configuring GoogleAds with the following three lines:

IConfigurationSection section = Configuration.GetSection("GoogleAdsApi");
GoogleAdsConfig config = new GoogleAdsConfig(section);
GoogleAdsClient client = new GoogleAdsClient(config);

configuration can be done in a single line:

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContext, services) =>
        services.AddGoogleAdsClient(hostContext.Configuration))
    .Build();

None of GoogleAdsConfig, GoogleAdsClient and services are designed to be singletons, because that's not how the product works in the general case.

I'll give it some thought, but perhaps it is simpler to have a SingletonGoogleAdsClientProvider helper class specifically for this use case. @Raibaz wdyt?

Not entirely sure if registering GoogleAdsConfig and GoogleAdsClient as transient would be feasible; I'd rather keep them as they are and instead add a Singleton to wrap them for dependency injection usage.

@jeffdahl I think @Raibaz and I need some more time thinking through this. How about we accept this bug for ourselves, and send you a PR when we are ready? I'd be happy to credit you for the work even if we end up with a different implementation.

Any consideration of adding the two interfaces in PR #540? As explained in the PR, adding these two interfaces will allow a configured IGoogleAdsClient to be injected via the DI container.

Thanks for considering.