Composition with fluent interfaces

Asked

Viewed 163 times

5

I’m trying to make a class using composition, fluent methods and I’m having some problems.

I would like to access a property of a compositional method through fluent interfaces but I cannot implement the abstract method.

The case is as follows:

// Classe base para composição
public abstract class Foo
{
    // Propriedade base
    public string Param1 { get; set; }

    // Método base para retorno da classe (usado com interfaces fluentes)
    public abstract Foo GetData();
}

// Classe que compoe Foo
public class Bar : Foo
{
    // Propriedade particular de Bar
    public string Param2 { get; set; }

    // Método para retorno da própria classe (usado com interfaces fluentes)
    // Aqui eu não consigo criar o método
    public override Bar GetData()
    {
        return this;
    }
}

// Classe que utiliza
public class Test
{
    public Test()
    {
        // Acesso à propriedade particular da classe compositiva
        var newBar = new Bar().GetData().Param2;
    }
}

Does anyone have any idea how to do that?

  • I think that the return type of the compositional class method should be the parent object (public override Foo GetData()).

  • You cannot declare this method as override: public override Bar GetData() because it does not exist in any class above the hierarchy (does not exist in the parent class). The method in the parent class returns Foo and not Bar.

  • The problem of me returning the same object of the father (Foo) in Getdata() is that with it I will not have access to property Param2.

1 answer

3


You must use Generic in the base class, use "T" as a return of "Getdata", and when inheriting this class pass the type you want to use in the "Getdata" method":

// Classe base para composição
public abstract class Foo<T>
{
    // Propriedade base
    public string Param1 { get; set; }

    // Método base para retorno da classe (usado com interfaces fluentes)
    public abstract T GetData();
}

// Classe que compoe Foo
public class Bar : Foo<Bar>
{
    // Propriedade particular de Bar
    public string Param2 { get; set; }

    // Método para retorno da própria classe (usado com interfaces fluentes)
    // Aqui eu não consigo criar o método
    public override Bar GetData()
    {
        return this;
    }
}

// Classe que utiliza
public class Test
{
    public Test()
    {
        // Acesso à propriedade particular da classe compositiva
        var newBar = new Bar().GetData().Param2;
    }
}
  • 1

    This code will not compile. It has the same problem as the question formulator’s code: public override Bar GetData() - no corresponding method to be overwritten.

  • I tested, compiled and worked. As you define the type T that will be used in class inheritance, there will be a method to overrite, since in the base class you define public abstract T GetData();

  • It worked, thanks to Michel!

  • Hmm Ok. I would declare the method in the daughter class with the same signature. But I don’t think it would make this code any more interesting.

  • I would also use T. I have my doubts if it would work in any situation. But I don’t have time to test this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.