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>