jeudi 29 août 2019

Inheritance: Unable to access method from base class

I am implementing CQRS pattern. To use CQRS pattern in my project I wrote three commands, they are

public class DogCommand : PetCommand
{
    public DogCommand(){}
    public override string Name{get; set;}
}

public class CatCommand : PetCommand
{
    public CatCommand(){}
    public override string Name{get; set;}
}

public abstract class PetCommand : ICommand
{
    public PetCommand(){}
    public virtual string Name{get; set;}
}

public interface ICommand
{
   //Bussiness logic
 }

Here I have interface called ICommand. PetCommand is base class which is implementing this interface. Derived class DogCommand and CatCommand are inheriting PetCommand.

I also wrote base command handler, as below

public abstract class BaseCommandHandler<T> : CommandHandler<T> where T : ICommand
{
    protected BaseCommandHandler(string type, string name): base(type, name)
    {

    }
}
public abstract class CommandHandler<T> : ICommandHandler<T> where T : ICommand
{
   protected CommandHandler(string type, string name)
   {
    //Business logic
   }

   protected void LogWrite(string msg)
   {
      //Writing log 
   }
}


public interface ICommandHandler<in T> where T : ICommand
{
    void Run(T command));
}

All functions present in BaseCommandHandler, I will use in each derived command handler

Now problem is in derived class command handler

public class PetCommandHandler : BaseCommandHandler<DogCommand>, ICommandHandler<CatCommand> 
{

    public void Run(DogCommand dCommand)
    {
        this.LogWrite(dCommand)      
    }

    public void Run(CatCommand cCommand)
    {
       **//Want to access this.LogWrite() with cCommand. How can I do that?**          
    }
}

Here I am unable to access this.LogWrite() function for cCommand, because PetCommandHandler is inheriting first BaseCommandHandler and then implementing ICommandHandle.

How to access this.LogWrite() function for cCommand?

Here is compile time error:

cannot convert from ‘Command.DogCommand’ to ‘Command.CatCommand’

Aucun commentaire:

Enregistrer un commentaire