Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid explicit type casting when overriding inherited methods

I have a base abstract class that also implements a particular interface.

public interface IMovable<TEntity, T>
    where TEntity: class
    where T: struct
{
    TEntity Move(IMover<T> moverProvider);
}

public abstract class Animal : IMovable<Animal, int>
{
    ...

    public virtual Animal Move(IMover<int> moverProvider)
    {
        // performs movement using provided mover
    }
}

Then I have inherited classes some of which have to override interface implementation methods of the base class.

public class Snake : Animal
{
    ...

    public override Animal Move(IMover<int> moverProvider)
    {
        // perform different movement
    }
}

My interface methods return the same object instance after it's moved so I can use chaining or do something directly in return statement without using additional variables.

// I don't want this if methods would be void typed
var s = GetMySnake();
s.Move(provider);
return s;

// I don't want this either if at all possible
return (Snake)GetMySnake().Move(provider);

// I simply want this
return GetMySnake().Move(provider);

Question

As you can see in my example my overrides in child class returns base class type instead of running class. This may require me to cast results, which I'd like to avoid.

How can I define my interface and implementations so that my overrides will return the actual type of the executing instance?

public Snake Move(IMover<int> moverProvider) {}
like image 588
Robert Koritnik Avatar asked May 28 '14 11:05

Robert Koritnik


2 Answers

I suggest changing the return type of the interface method to void and moving the chaining behaviour to an extension method where you can get the real type of the target e.g.

public interface IMovable<TEntity, T>
    where TEntity : class
    where T : struct
{
    void MoveTo(IMover<T> moverProvider);
}

public abstract class Animal : IMovable<Animal, int>
{
    public virtual void MoveTo(IMover<int> mover) { }
}

public static class AnimalExtensions
{
    public static TAnimal Move<TAnimal>(this TAnimal animal, IMover<int> mover) where TAnimal : Animal, IMovable<TAnimal, int>
    {
        animal.MoveTo(mover);
        return animal;
    }
}

Note you can make the Move extension more generic if you need it to apply more generally:

public static TEntity Move<TEntity, T>(this TEntity entity, IMover<T> mover) where TEntity : IMovable<TEntity, T> where T : struct
{
    entity.MoveTo(mover);
    return entity;
}
like image 154
Lee Avatar answered Nov 12 '22 05:11

Lee


You can convert Animal to a generic type that accepts the concrete type as a type parameter:

public abstract class Animal<T> : IMovable<T, int> where T:Animal<T>        
{


    public virtual T Move(IMover<int> moverProvider)
    {
    ...
    }
}

public class Snake : Animal<Snake>
{


    public override Snake Move(IMover<int> moverProvider)
    {
    ...
    }
}
like image 2
Panagiotis Kanavos Avatar answered Nov 12 '22 04:11

Panagiotis Kanavos