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 4 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.

    • Tim Corey says:

      I’ll be covering this in the next DI video. You have a couple options and it will be easiest to show rather than try to type out.

  • 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.

Leave a Reply