2
Taking the example of the following code:
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 any alternative to avoid rewriting the method in the class Filha
?
This arose from the need to implement the Fluent Interface in several classes. These classes inherit the functionalities of the others and add new.
So that the Fluent Interface work the methods have to return the class Filha
and not the class Pai
.
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);
}
The class Pai
implements IDataVideoConfiguration<TResponse>
the problem arises when implementing the class Filha
inheriting from Pai
:
public class Filha<TResponse> : Pai<TResponse>, IDataVideoImagesConfiguration<TResponse>
I don’t know if I understand what you really want. In the form you’re in, you just don’t have to have the method in your daughter. If you really need to have this method to do other things in it, then you should declare the method in
Pai
asvirtual
. Other than that, I don’t see any alternatives.– Maniero
Got it. The problem is having to keep declaring the method in the daughter class then? The excessive work? I have the impression that this problem is difficult to solve if there are any. That is, it is probably easier to do that same gambit. I thought about extension methods, but it would be limited to them only being able to access public members.
– Maniero
Yes the reason is the work. This situation has arisen quite a while ago. I tried several things and ended up giving up. It was the question about interfaces that reminded me of this issue. Note that at the level of interfaces this is solved using another generic type Tout.
– ramaral