通用“TThis”为了流畅的课堂

发布于 2024-08-10 06:33:14 字数 971 浏览 6 评论 0原文

我正在构建一个流畅的接口,其中有一个包含大量流畅逻辑的基类,以及一个添加一些专门行为的派生类。我面临的问题是从派生类型的实例调用时基类中的流畅方法的返回类型。调用基类的方法后,只有基类的方法仍然可用于进一步的流畅调用。

改变调用方法的顺序将有助于它的编译,但它会降低它的可读性,这对于流畅的接口来说有点重要。有没有办法为基类定义某种“This”类型,以便所有方法返回相同的类型。

示例

public class Field<T>
{
    public Field<T> Name( string name )
    {
        _name = name;
        return this;
    }
}

public SpecialField<T> : Field<T>
{
    public SpecialField<T> Special(){ return this; }
}


// !!! Arrgh. Special is not a member of the Field<T> class.
var specialField = new SpecialField()
    .Name( "bing" )
    .Special();

损坏的解决方案

我尝试通过执行类似以下操作来解决该问题,但它不是有效的 C# :( 但至少表达了我希望如何编写接口代码。

public class Field<T,TThis> : TThis
    where TThis : Field<T,TThis>
{
    public TThis Name( string name ){...}
}

public SpecialField<T> : Field<T,SpecialField<T>>
{
    public TThis Special(){ return this; }
}

I'm constructing a fluent interface where I have a base class that contains the bulk of the fluent logic, and a derived class that add some specialized behavior. The problem I'm facing is the return type of the fluent methods in the base class when called from an instance of the derived type. After invoking a method of the base class, only methods of the base class remain available for further fluent invocations.

Changing the order in which the methods are invoked will help it to compile, but it makes it less readable which is kinda the point for fluent interfaces. Is there a way to define some sort of "This" type for the the base class so that all methods return the same type.

Example

public class Field<T>
{
    public Field<T> Name( string name )
    {
        _name = name;
        return this;
    }
}

public SpecialField<T> : Field<T>
{
    public SpecialField<T> Special(){ return this; }
}


// !!! Arrgh. Special is not a member of the Field<T> class.
var specialField = new SpecialField()
    .Name( "bing" )
    .Special();

Broken Solution

I've tried solving it by doing something like the following but it's not valid C# :( but at least expresses how I'd like to code the interface.

public class Field<T,TThis> : TThis
    where TThis : Field<T,TThis>
{
    public TThis Name( string name ){...}
}

public SpecialField<T> : Field<T,SpecialField<T>>
{
    public TThis Special(){ return this; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-08-17 06:33:14

在研究了其他一些流畅的 API 之后,我找到了如何做到这一点。它不是很干净,但效果很好。基本上,您为要使用的每个派生类型引入一个中间基类,并将“TThis”类型传递给实际实现。

样本

public class FieldBase<T,TThis> 
    where TThis : FieldBase<T,TThis>
{
    private string _name;
    public TThis Name( string name ) 
    {
        _name = name;
        return (TThis)this;
    }
}

public class Field<T> : FieldBase<T,Field<T>>{}

public class SpecialFieldBase<T,TThis> : FieldBase<T,TThis>
    where TThis : SpecialFieldBase<T,TThis>
{
    public TThis Special(){ return (TThis)this; }
}

public class SpecialField<T> : SpecialFieldBase<T,SpecialField<T>>{}


// Yeah it works!
var specialField = new SpecialField<string>()
    .Name( "bing" )
    .Special();

After poking around some other fluent APIs I found how to do it. It's not quite as clean, but it works well. Basically you introduce an intermediary base class for each derived type that you want to use and it passes the "TThis" type to the actual implementation.

Sample

public class FieldBase<T,TThis> 
    where TThis : FieldBase<T,TThis>
{
    private string _name;
    public TThis Name( string name ) 
    {
        _name = name;
        return (TThis)this;
    }
}

public class Field<T> : FieldBase<T,Field<T>>{}

public class SpecialFieldBase<T,TThis> : FieldBase<T,TThis>
    where TThis : SpecialFieldBase<T,TThis>
{
    public TThis Special(){ return (TThis)this; }
}

public class SpecialField<T> : SpecialFieldBase<T,SpecialField<T>>{}


// Yeah it works!
var specialField = new SpecialField<string>()
    .Name( "bing" )
    .Special();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文