Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Last active August 21, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiannagel/b2c77869d3f492dc583c77467560dc3c to your computer and use it in GitHub Desktop.
Save christiannagel/b2c77869d3f492dc583c77467560dc3c to your computer and use it in GitHub Desktop.
Register services in the Microsoft.Extensions.DependencyInjection container
public class AppServices
{
private AppServices()
{
var services = new ServiceCollection();
// view-models
services.AddTransient<BooksViewModel>();
// services
services.AddTransient<IItemsService<Book>, BooksService>();
services.AddTransient<IShowProgressInfo, ShowProgressInfo>();
// stateful services
services.AddScoped<IItemToViewModelMap<Book, BookItemViewModel>, BookToBookItemViewModelMap>();
services.AddScoped<ISharedItems<Book>, SharedItems<Book>>();
services.AddScoped<IRepository<Book, int>, BooksSampleRepository>();
// logging configuration
services.AddLogging(builder =>
{
#if DEBUG
builder.AddDebug().SetMinimumLevel(LogLevel.Trace);
#endif
});
ServiceProvider = services.BuildServiceProvider();
}
public IServiceProvider ServiceProvider { get; }
private static AppServices _instance;
private static readonly object _instanceLock = new object();
private static AppServices GetInstance()
{
lock (_instanceLock)
{
return _instance ?? (_instance = new AppServices());
}
}
public static AppServices Instance => _instance ?? GetInstance();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment