Skip to content

Instantly share code, notes, and snippets.

@takahirohonda
Last active December 16, 2019 09:38
Show Gist options
  • Save takahirohonda/eb72d4a325b310b986e24b1723c23a3f to your computer and use it in GitHub Desktop.
Save takahirohonda/eb72d4a325b310b986e24b1723c23a3f to your computer and use it in GitHub Desktop.
Using Attribute - ASP.NET Core DI
// Add attribute
// Interface for the class & Lifetime
// (1) Scoped
[Service(typeof(IAppsRepository), Lifetime.Scoped)]
public class AppsRepository : IAppsRepository
{
...
}
// (2) Transient
[Service(typeof(IAppsRepository), Lifetime.Transient)]
public class AppsRepository : IAppsRepository
{
...
}
// (3) Singleton
[Service(typeof(IAppsRepository), Lifetime.Singleton)]
public class AppsRepository : IAppsRepository
{
...
}
// (4) Generics
[Service(typeof(IAppLogger<>), Lifetime.Scoped)]
public class LoggerAdapter<T> : IAppLogger<T>
{
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment