mardi 4 septembre 2018

What is the pattern you should follow for "dependencies within dependencies" in .NET Core?

Consider the following:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddSingleton<ILogger, Logger>();
        ...
    }

So I obviously want a logging class within my controller methods, as shown, but I also have many other components that will be common to them - and all of those components will want to have references to my logger. It is a singleton after all (which might be an anti-pattern here, I guess, but anyway).

How should I properly be passing "dependencies within dependencies" within a ConfigureServices method? I could do something like this, I guess:

    public void ConfigureServices(IServiceCollection services)
    {
        Logger l = new Logger();
        services.AddSingleton<ILogger, Logger>((provider) =>
        {
            return l;
        });
        services.AddSingleton<IFoo, Foo>((provider) =>
        {
            return new Foo(l);
        });
        ...
    }

I could do something like that! But it looks and feels like a hack. I'm certain I shouldn't be resolving / making actual concrete constructions in my ConfigureServices method like this, but I don't know of a way to tell my Foo constructor to (like my controller methods) be dependent on ILogger.

What is the intended mechanism for doing this -- or is there a completely different pattern I should consider following?

Aucun commentaire:

Enregistrer un commentaire