Demo: Injecting AccountRepository

In the Conduit.UserController Project
  • Housekeeping
    • Add project reference to Repositories
  • Add IAccountRepository as private member
  • Inject via constructor
  • Fix using statement for AccountRepository
  • Modify RegisterUser method to call AccountRepo.RegisterUser

The result of the C# code:

using Conduit.Models.Requests;
using Conduit.Models.Responses;
using Conduit.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Conduit.Api.Controllers
    public class UsersController : ControllerBase
        private IAccountRepository AccountRepo;
        private ILogger<UsersController> Logger;

        public IActionResult RegisterUser([FromBody]UserRequest<Register> req)
            User user = AccountRepo.RegisterUser(req.User);
            return Ok(new { user });

        public UsersController(IAccountRepository accountRepo, ILogger<UsersController> logger)
            AccountRepo = accountRepo;
            Logger = logger;

Clean, Rebuid & run to ensure everything is still working correctly.

Resolve 500 Exception

I wish ASP.NET was magical and new what we wanted. Unfortunately, it doesn't. We need to tell it where IAccountRepository comes from.

In the next video we'll discuss more about Dependency Injection.