Roslyn: Ability to create generic types with default containing type?

Created on 24 Jun 2016  路  3Comments  路  Source: dotnet/roslyn

Take a look at the following piece of code:

class HomeController
{
    public Test(ILogger<HomeController> logger, IStringLocalizer<HomeController> localizer)
    {
    }
}

This usage of injecting generic interface with containing type is so common specially in asp.net core.

Is there any way to simplify this injection, like ILogger, IStringLocalizer or other forms.

This maybe oversimplification (since refactoring will correct usages of class names anyway), but it's overwhelming to type long class names over and over again.

This maybe a great addition for C# language. Please let me know what you think!

Thanks

Area-Language Design Discussion

Most helpful comment

I'm going to assume that the only reason you've made ILogger and IStringLocalizer generic is so that you can specify the injectee as the generic type argument. Why not utilize the facilities of whatever dependency injection library that you're using in order to capture said injectee during injection? Then those interfaces wouldn't need to be generic at all and you wouldn't need some weird new syntax to "alias" the declaring class as a generic type argument.

All 3 comments

I'm going to assume that the only reason you've made ILogger and IStringLocalizer generic is so that you can specify the injectee as the generic type argument. Why not utilize the facilities of whatever dependency injection library that you're using in order to capture said injectee during injection? Then those interfaces wouldn't need to be generic at all and you wouldn't need some weird new syntax to "alias" the declaring class as a generic type argument.

Problem solved (unless of course you meant "infer" rather than "default"):

public interface IHomeControllerLogger : ILogger<HomeController> {}

Alternatively #7451.

I believe Joshua meant "infer". I'd like to do exactly what HaloFour suggested but I cannot find how to do it.
Please post a link to a resource explaining how to capture injectee using standard asp.net core di.

Was this page helpful?
0 / 5 - 0 ratings