Outline

Demo: Extracting Interfaces

In the Conduit.Repositories Project

House Cleaning
  • Delete Class1.cs
  • Right+Click, Install Package Microsoft.Extensions.Logging.Abstractions
  • Add reference to Conduit.Models
Create AccountRepository
  • Make it public
  • Add private member Ilogger
  • Add CTOR & inject Ilogger
  • Add public method RegisterUser
    • Move code from UsersController to AccountRepository
  • Extract Interface from AccountRepository

The result of the C# code for the AccountRepository:

using Conduit.Models.Requests;
using Conduit.Models.Responses;
using Microsoft.Extensions.Logging;

namespace Conduit.Repositories
{
    public class AccountRepository : IAccountRepository
    {
        private ILogger<AccountRepository> Logger;

        public User RegisterUser(Register register)
        {
            User user = new User
            {
                Bio = "no bio yet",
                Email = register.Email,
                Image = "no image yet",
                Token = "no token yet",
                UserName = register.UserName
            };
            return user;
        }

        public AccountRepository(ILogger<AccountRepository> logger)
        {
            Logger = logger;
        }
    }
}

The result of the C# code for IAccountRepository:

using Conduit.Models.Requests;
using Conduit.Models.Responses;

namespace Conduit.Repositories
{
    public interface IAccountRepository
    {
        User RegisterUser(Register register);
    }
}