C#C
C#2y ago
Kyede

Circular Dependency

Heyo :D,

So I have started getting into somewhat bigger projects again and needed to use Dependency Injections to loose coupling and overall tightness between classes. Since 2 of my classes (and likely in the future, more class depend on each other), it's a good approach to create abstractions to expose only a fraction of the class that other classes need to access. This way, I am not exposing it concretely. However, for some reason, I am running into a circular dependency exception which I don't seem to... really understand? I get where the issue is but I can't figure out a more plausible approach. I would appreciate some feedback on what potentially can be done.

To give the issue.. ILoggingManager depends on IYukkaifyEnvironment, which in turn depends on ILoggingManager..

namespace Yukkaify
{
  public interface IYukkaifyEnvironment
  {
    bool IsHooked { get; set; }
    string Root { get; set; }
  }
      
  public interface IYukkaify
  {
    DiscordSocketClient Client { get; set; }
    CommandService Command { get; set; }
    IConfiguration Configuration { get; set; }
    ILoggingManager LogManager { get; set; }
    IGuildVerificationManager GuildManager { get; set; }
    IEventsManager EventManager { get; set; }
  
    Task InitializeYukkaify();
    Task YukkaifyReadyHandler();
  }

  public class Yukkaify(DiscordSocketClient client, CommandService command, IConfiguration configuration, ILoggingManager logManager, IGuildVerificationManager guildManager, IEventsManager eventManager) : IYukkaify, IYukkaifyEnvironment
  {
     // Here I do whatever. Since nothing is being used atm besides ILoggingManager (im still working on it)
  }
}


namespace Yukkaify.Utilities
{
  public interface ILoggingManager
  {
      IYukkaifyEnvironment Yukkaify { get; }
      IQueueManager QueueManager { get; }
      IConfiguration Configuration { get; }
      List<string> ContentCache { get; set; }
      
      Task ContentLogger(LoggingManager.DebugType type, string message, Exception ex = null);
      Task LogReporter();
  }
  
  public class LoggingManager(IYukkaifyEnvironment yukkaify, IQueueManager queueManager, IConfiguration configuration) : ILoggingManager
  {
      public IYukkaifyEnvironment Yukkaify { get; } = yukkaify;
  
      public IQueueManager QueueManager { get; } = queueManager;
  
      public IConfiguration Configuration { get; } = configuration;
  
      public List<string> ContentCache { get; set; } = []; 
  }
}
Was this page helpful?