Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Alex Thissen "It depends: loving .NET Core dependency injection or not"

89 views

Published on

.NET Core comes with its own dependency injection system, that you probably know from ASP.NET Core. In this session, we will have a detailed look at all of the specifics of Microsoft's default DI system for .NET Core applications. You will learn how to properly use the Inversion of Control features, such as registering services, scopes, lifetimes, and how to resolve instances.

Armed with this DI knowledge, we will revisit ASP.NET Core and investigate bootstrapping and typical scenarios for middleware, background processes and resolving objects from MVC and Razor.

At the end there will be a deep dive into topics with service descriptors, implementation factories, do's and don'ts and pitfalls to avoid memory leaks in your implementation.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Alex Thissen "It depends: loving .NET Core dependency injection or not"

  1. 1. 1 It depends: Loving .NET Core dependency injection or not Alex Thissen Cloud architect at Xpirit, The Netherlands @alexthissen
  2. 2. 2 1. Unit testing 2. SOLID: Dependency Inversion Principle 3. Loose coupling 4. Clean architecture Domain Logic Presentation
  3. 3. 3 Container HomeController FloppyDiskRepository IRepository ILogger ConsoleLogger
  4. 4. 4 Demo: ASP.NET Core DI Quick look at (not so) obvious injection
  5. 5. 5 Register How • Add… and TryAdd… of type mappings • Add… extension methods Where • Application root • Startup class (ASP.NET) Resolve Implicit • Constructor injection • ASP.NET Core specifics Explicit • GetService<T> • GetRequiredService<T> • Also for enumerables Release Automatic • Resolved instances and dependencies Scopes • End of scope • Might need to create child scopes IServiceCollection IServiceProvider IDisposable 1 2 3
  6. 6. 6 ServiceType ImplementationType
  7. 7. 7 Singleton • Easy way to implement singleton pattern • One instance per DI container • Beware of concurrency and threading issues Scoped • Duration of scope • ASP.NET uses web request as scope • Useful for UnitOfWork objects, e.g. DbContext • See also ServiceScopeFactory Transient • Single use • Most common and safest
  8. 8. 8 public class Startup { public Startup(IConfiguration configuration, IHostEnvironment env) public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } builder.UseStartup<Startup>(); 1 2 3
  9. 9. 9 IServiceProvider ActivatorUtilities IServiceProvider Register using Configure(Services) injection in Startup class Constructor injection [FromServices] attribute @inject IApplicationBuilder. ApplicationServices HttpContext. RequestServices Middleware Constructor or Invoke method
  10. 10. 10 Demo: Resolving services in ASP.NET Core Controller constructors @inject FromServicesAttribute Application- and RequestServices
  11. 11. 11 IHostedService BackgroundService Host IHostBuilder WebHostBuilder HostBuilder Configure Microsoft.Extensions.Hosting Microsoft.Extensions.DependencyInjection
  12. 12. 12 Demo: Generic Host New .NET Core 3.0 hosting
  13. 13. 13 ServiceDescriptor
  14. 14. 14 IDisposable Dispose ServiceScopeFactory AddHostedService<T> using (var scope = serviceScopeFactory.CreateScope()) { ISomeRepository scoped = scope.ServiceProvider.GetRequiredService<ISomeRepository>(); await DoMyWork(scoped); }
  15. 15. 15 UseServiceProviderFactory ConfigureContainer IServiceProvider Some alternative DI Frameworks Autofac Castle Windsor Lamar LightInject Ninject SimpleInjector Spring.NET Unity
  16. 16. 16 Demo: Advanced DI Scopes Replacing DI container Custom DI containers
  17. 17. 17 Configure{Environment}Services Configure{Environment} Startup{Environment} StartupProduction UseStartup<Startup> Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName; webBuilder.UseStartup(assemblyName); });
  18. 18. 18 Host.CreateDefaultBuilder(args) .UseDefaultServiceProvider((context, options) => { options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); options.ValidateOnBuild = true; })
  19. 19. 20 Demo: Tips and tricks Convention based startup Scope validation TestServer services override
  20. 20. 21 IServiceProvider IServiceProvider.CreateScope IHttpContextAccessor accessor.HttpContext.RequestServices
  21. 21. 22
  22. 22. 23
  23. 23. 24 Workshop Going container for .NET Core in the cloud Alex Thissen Cloud architect at Xpirit, The Netherlands @alexthissen

×