agoda-com / agoda-com/Agoda.IoC

Startup init for singletons

Aberta
#15 7 comentários 0 reações 0 responsáveis Ver no GitHub
enhancement
Linguagem predominante
C#
Estrelas
38
Forks
10
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

Autofac has this

```csharp

///
/// When implemented by a component, an instance of the component will be resolved
/// and started as soon as the container is built. Autofac will not call the Start()
/// method when subsequent instances are resolved. If this behavior is required, use
/// an OnActivated() event handler instead.
///
///
/// For equivalent "Stop" functionality, implement . Autofac
/// will always dispose a component before any of its dependencies (except in the presence
/// of circular dependencies, in which case the components in the cycle are disposed in
/// reverse-construction order.)
///
public interface IStartable
{
///
/// Perform once-off startup processing.
///
void Start();
}
```

anything that inherits from it will init on startup.

It's a cool idea some something we use in Supply extranets at Agoda. There this IHostedService now as well for background workers too

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio

Idea would be to do something like this if we follow the autofac approach

```csharp
[RegisterSingleton]
public class MyBackgroundWorker : IMyBackgroundWorker, IStartupable
{
// ..
}
```

And then call a GetService after app init, this would also require use to add a new extension method though something like "UseAgodaIoC", or more specific "StartupAgodaIoCSingletons".

Alternatively we could simply leverage the IHostedService and register them with a new attribute

```csharp
[RegisterSingletonStartup]
public class MyBackgroundWorker : IHostedService
{
// ..
}

// in registartion
services.AddHostedService();
//..
```

The problem here though is that AddHostedService has no support for registering an interface.

In most of our use cases the startup method is prewarming cache in singletons that are later used for data fetchnig (i.e. Repositories, etc). So i think this wont work for us as we need interfaced use in teh regestration to properly mock for unit testing, so we need something simple like the autofac example imo.

Thoughts?

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.