Home > Back-end >  Base and Inheritance class names/prefixes
Base and Inheritance class names/prefixes

Time:11-13

I have a main class such as the following ( with extending class called Keys )

public class Engine : Keys, IEngine, IDisposable
{
   private readonly Network _network;
   public Engine(bool useMainNet = false) : base(useMainNet)
   {
      _network = useMainNet ? Network.Main : Network.TestNet;
   }

   public Network GetCurrentNetwork() => _network;

Where the Keys class has functions like GenerateKeys() as following

public class Keys : IDisposable, IKeys
{
  public Keys(bool useMainNet = false)
  {
      _network = useMainNet ? Network.Main : Network.TestNet;
  }
  public string GenerateKeys()
  {
      return "KEY_GEN";
  }
}

The problem is I can call the method as follows

 using (Engine engine = new(true))
 {     
      engine.GenerateKeys();
 }

It works fine, but what I want is the following

using (Engine engine = new(true))
{     
    engine.Keys.GenerateKeys();
    // OR engine.Keys().GenerateKeys();
}

So I want the Main_Method.Sub_Method.Function, It makes more sense this way, Is it possible?

CodePudding user response:

As mentioned in the comments, you are probably mixing the principles of compositions and inheritance

Try this:

public class Engine : IEngine, IDisposable
{
   private readonly Network _network;
   public Engine(bool useMainNet = false) : base(useMainNet)
   {
      _network = useMainNet ? Network.Main : Network.TestNet;
   }

   public Network GetCurrentNetwork() => _network;

   // by property
   public Keys Keys => new Keys();

   // by method (with parameters)
   public Keys Keys(bool useMainNet = false) => new Keys(useMainNet);

   ...
}
  • Related