Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to create generic types with default containing type? #6626

Closed
joshua-mng opened this issue Jun 24, 2016 · 3 comments
Closed

Ability to create generic types with default containing type? #6626

joshua-mng opened this issue Jun 24, 2016 · 3 comments

Comments

@joshua-mng
Copy link

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

@HaloFour
Copy link
Contributor

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.

@dsaf
Copy link

dsaf commented Jun 27, 2016

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

public interface IHomeControllerLogger : ILogger<HomeController> {}

Alternatively dotnet/roslyn#7451.

@idilov
Copy link

idilov commented Feb 23, 2017

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.

@CyrusNajmabadi CyrusNajmabadi transferred this issue from dotnet/roslyn Oct 28, 2022
@dotnet dotnet locked and limited conversation to collaborators Oct 28, 2022
@CyrusNajmabadi CyrusNajmabadi converted this issue into discussion #6627 Oct 28, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

5 participants