Alternative to new use when implementing methods that return this in daughter classes

2

Taking the following code as an example:

public class Pai
{
    protected string PropriedadeA { get; set; }
    public Pai Metodo1(int valor)
    {
        //Vários procedimentos feitos aqui
        PropriedadeA = "Resultado do tratamento";
        return this;
    }
}

public class Filha : Pai
{
    public new Filha Metodo1(int valor)
    {
        base.Metodo1(valor);
        return this;
    }
}

Is there an alternative that avoids rewriting the method in class Filha ?

This arose from the need to implement the Fluent Interface feature in various classes. These classes will inherit the features of the others and add new ones.

In order for the Fluent Interface to work the methods have to return the class Filha and not the Pai class.

public interface IDataConfiguration<TResponse>
{
    TResponse GetData();
    Task<TResponse> GetDataAsync();
}

public interface IDataVideoConfiguration<TResponse> : IDataConfiguration<TResponse>
{
    IDataVideoConfiguration<TResponse> VideoName(string videoName);
    IDataVideoConfiguration<TResponse> CosmoId(string cosmoId);
    IDataVideoConfiguration<TResponse> I_GuideId(string iGuideId);
}

public interface IDataVideoImagesConfiguration<TResponse>
{
    IDataVideoImagesConfiguration<TResponse> VideoName(string videoName);
    IDataVideoImagesConfiguration<TResponse> CosmoId(string cosmoId);
    IDataVideoImagesConfiguration<TResponse> I_GuideId(string iGuideId);

    IDataVideoImagesConfiguration<TResponse> ImageFormatId(string formatId);
    IDataVideoImagesConfiguration<TResponse> ImageSize(string imageSize);
    IDataVideoImagesConfiguration<TResponse> ImageSort(string imageSort);
    TResponse GetData(int count = 0, int offset = 0);
    Task<TResponse> GetDataAsync(int count = 0, int offset = 0);
}  

Class Pai implements IDataVideoConfiguration<TResponse> problem arises when implementing class Filha inheriting from Pai :

public class Filha<TResponse> : Pai<TResponse>, IDataVideoImagesConfiguration<TResponse>
    
asked by anonymous 22.01.2015 / 22:15

2 answers

3

I will answer that I do not know if it solves the problem but at least it helps to have ideas:

using System.Console;

public class Program {
    public static void Main() {
        var pai = new Pai();
        WriteLine(pai.Metodo1(1).GetType());
        var filha = new Filha();
        WriteLine(filha.Metodo1(1).GetType());
        filha.PropriedadeB = "xxx";
        WriteLine(filha.Metodo1(1).GetType());
        var temp = filha.Metodo1(1);
        WriteLine(temp.PropriedadeB);
    }
}

public class Pai : Pai<Pai> { }

public class Pai<T> where T : Pai<T> {
    protected string PropriedadeA { get; set; }
    public T Metodo1(int valor) {
        //Vários procedimentos feitos aqui
        PropriedadeA = "Resultado do tratamento";
        return (T)this;
    }
}

public class Filha : Pai<Filha> {
    public string PropriedadeB { get; set; }
}

See working on dotNetFiddle .

It certainly has limitations, but it already solves something.

But be prepared also to have to take the trouble of reproducing methods unnecessarily. Sometimes to simplify the life of the consumer greatly increases the work of the supplier.

    
22.01.2015 / 23:38
1

I did the following test :

using System;

public class Pai
{
    protected string PropriedadeA { get; set; }
    public Pai Metodo1(int valor)
    {
        //Vários procedimentos feitos aqui
        PropriedadeA = "Resultado do tratamento";
        return this;
    }
}

public class Filha : Pai
{

}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        var filha = new Filha().Metodo1(1);
        Console.WriteLine(filha.GetType());
    }
}

The returned type was Filha . There is no need to reintroduce the method.

    
22.01.2015 / 22:20