was successfully added to your cart.

C# Dependency Injection – Video and Resources

The What, How, and Why

Dependency Injection in C# can be a really confusing topic. Yet, when done right, Dependency Injection can be one of the best things you do for your application. It allows you to disconnect pieces of your application from each other easily and it allows you to test the various parts of your application independently. Today, I am going to get you started using Dependency Injection using a free tool called Autofac. We will take a small sample application and first apply the Dependency Inversion Principle (the D in SOLID). Then, we will wire up Autofac to handle connecting the various dependencies together.

Resources mentioned in the video

Join the discussion 8 Comments

  • Norm says:

    Hi, Tim. Thanks for the great content on Dependency Injection.

    I have one question about registering types. If my application uses two objects that both implement the same interface, can you register both of them? And, if so, how does AutoFac know how to distinguish between the two objects and their different implementations in the application?

    Thanks in advance for your reply.

  • Burrows says:

    Hi Tim,
    great video as usual, really look forward to my Mondays a watching your tutorials your SOLID and DI tutorials have help me alot.
    just a quick note when Assembly scanning was looking at the documentation on the Autofac site and found an in my opinion easier approach to the one you showed. this is how I have implemented it in my own example at home .

    using System.Reflection;
    using Autofac;
    using Context.Policy;
    using UnitOfWork.Policy;
    using ViewModels;

    namespace CreationFactory
    public static class AutofacInjection
    #region Private Fields

    private static readonly IContainer resolver;

    #endregion Private Fields

    #region Public Constructors

    static AutofacInjection()
    var reg = new ContainerBuilder();
    var asm = Assembly.Load(nameof(ViewModels));


    resolver = reg.Build();

    #endregion Public Constructors

    #region Public Properties

    public static IFinancialTotalsViewModel GetFinancialTotalsViewModel => resolver.Resolve();
    public static IPolicyTabViewModel GetPolicyTabViewModel => resolver.Resolve();
    public static IPolicyViewModel GetPolicyViewModel => resolver.Resolve();

    #endregion Public Properties

    • Tim Corey says:

      I’ll be covering the AsImplementedInterfaces in my next DI video since it seems easier at first but it can cause issues for you down the road.

  • Joe Lin says:

    Tim, I just discovered your site, and this was one of the best tutorials I’ve ever seen, on any topic. I’m looking forward to checking out your other videos.

    Could you show how to use Autofac with WPF and MVVM in a future DI video? I’ve been doing some research, but I’m still very lost. Connecting up views with view models seems quite complicated. I feel you would break it down so the difficulty would be manageable and understandable. (Also, I’m trying to use Prism, but that might be too much off-topic.)

Leave a Reply