Skip to content

Instantly share code, notes, and snippets.

View Startup
services.AddSingleton<ISingletonOperation, SingletonOperation>();
services.AddTransient<ITransientOperation, TransientOperation>();
services.AddScoped<IScopedOperation, ScopedOperation>();
services.AddTransient<IMyService, MyService>();
View HomeController
private readonly ISingletonOperation _singletonOperation;
private readonly IScopedOperation _scopedOperation;
private readonly ITransientOperation _transientOperation;
private readonly IMyService _myService;
private readonly ILogger<HomeController> _logger;
public HomeController
(
ILogger<HomeController> logger,
View MyService
public interface IMyService
{
}
public class MyService : IMyService
{
private readonly ISingletonOperation _singletonOperation;
private readonly ITransientOperation _transientOperation;
private readonly IScopedOperation _scopedOperation;
View ScopedOperation
public interface IScopedOperation : IOperation
{
}
public class ScopedOperation : IScopedOperation
{
public Guid Id { get; }
public ScopedOperation()
{
View ScopedOperation
public interface IScopedOperation : IOperation
{
}
public class ScopedOperation : IScopedOperation
{
public Guid Id { get; }
public ScopedOperation()
{
View TransientOperation
public interface ITransientOperation : IOperation
{
}
public class TransientOperation : ITransientOperation
{
public Guid Id { get; }
public TransientOperation()
{
View singleton
public interface ISingletonOperation : IOperation
{
}
public class SingletonOperation : ISingletonOperation
{
public Guid Id { get; }
public SingletonOperation()
{
View iOperation
public interface IOperation
{
Guid Id { get; }
}
View foo example
var bar = new Bar();
var foo = new Foo(bar);
View foo
class Bar : IBar {
// ...
}class Foo {
private readonly IBar _bar;
public Foo(IBar bar) {
_bar = bar;
}
}