This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class ScrutorExtensions | |
| { | |
| public static IImplementationTypeSelector InjectableAttributes(this IImplementationTypeSelector selector) | |
| { | |
| var lifeTimes = Enum.GetValues(typeof(ServiceLifetime)).Cast<ServiceLifetime>(); | |
| foreach (var item in lifeTimes) | |
| selector = selector.InjectableAttribute(item); | |
| return selector; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class InjectableAttribute : Attribute | |
| { | |
| public ServiceLifetime Lifetime { get; } | |
| public InjectableAttribute(ServiceLifetime lifeTime = ServiceLifetime.Transient) | |
| { | |
| Lifetime = lifeTime; | |
| } | |
| } | |
| [Injectable(ServiceLifetime.Transient)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| ... | |
| services.Scan(i => | |
| i.FromCallingAssembly() | |
| .AddClasses(c => c.WithAttribute<TransientAttribute>()) | |
| .AsImplementedInterfaces() | |
| .WithTransientLifetime() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TransientAttribute : Attribute | |
| { | |
| public TransientAttribute() | |
| { | |
| } | |
| } | |
| public class ScopedAttribute : Attribute | |
| { | |
| public ScopedAttribute() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| ... | |
| services.Scan(i => | |
| i.FromCallingAssembly() | |
| .AddClasses(c => c.AssignableTo<ITransient>()) | |
| .AsImplementedInterfaces() | |
| .WithTransientLifetime() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface ITransient { } | |
| public interface IScoped { } | |
| public interface ISingleton { } | |
| public interface ITransientService | |
| { | |
| string GetValue(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ApiController] | |
| [Route("[controller]")] | |
| public class ExampleServiceController : ControllerBase | |
| { | |
| private readonly ITestTransientService _transientService; | |
| private readonly ITestScopedService _scopedService; | |
| private readonly ITestSingletonService _singletonService; | |
| public ExampleServiceController(ITestTransientService transientService, ITestScopedService scopedService, ITestSingletonService singletonService) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| ... | |
| services.AddTransient<ITestTransientService, TestTransientService>(); | |
| services.AddScoped<ITestScopedService, TestScopedService>(); | |
| services.AddSingleton<ITestSingletonService, TestSingletonService>(); | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class HomePageComponent extends BasePageComponent implements OnInit { | |
| { | |
| ... | |
| goAbout() { | |
| this.changePage('about'); | |
| return false; | |
| } | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class HomePageComponent extends BasePageComponent implements OnInit { | |
| { | |
| ... | |
| goAbout() { | |
| this.changePage('about'); | |
| return false; | |
| } | |
| ... | |
| } |