Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What about generics?</p> <pre><code>public class BaseFoo&lt;TDerived&gt; where TDerived : BaseFoo&lt;TDerived&gt; { private bool _myProp = false; public TDerived SetMyProperty() { this._myProp = true; return (TDerived)this; } } public class DerivedFoo : BaseFoo&lt;DerivedFoo&gt; { public bool _derivedProp = false; public DerivedFoo SetDerivedPropery() { this._derivedProp = true; return this; } } </code></pre> <p><strong>But I really think that the coupling between the different levels of the inheritance hierarchy is a big design problem.</strong><br> I suggest you have a look at other fluent interfaces like Fluent NHibernate, Rhino Mocks etc, to see how the "big" guys do it ;-)</p> <p>You could also use extension methods like this, sort of an <em>fluent upgrade</em>. This seems cleaner to me.</p> <pre><code>public static class FooFluentExtensions { public static T SetMyProperty&lt;T&gt;(this T foo) where T : BaseFoo { foo.MyProp = true; return foo; } public static T SetDerivedPropery&lt;T&gt;(this T foo) where T : DerivedFoo { foo.DerivedProp = true; return foo; } } static void Main() { DerivedFoo foo = new DerivedFoo(); // works, because SetMyProperty returns DerivedFoo foo.SetMyProperty().SetDerivedPropery(); BaseFoo baseFoo = new BaseFoo(); baseFoo.SetMyProperty(); // doesn't work (and that's correct), because SetMyProperty returns BaseFoo // baseFoo.SetMyProperty().SetDerivedPropery(); } </code></pre> <p>You could encapsulate every method that you want to be fluent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload