C# - polymorphism question
I have some classes that i'd like chain m开发者_StackOverflow社区ethods of to provide a fluent style of config.
e.g.
public class BaseFoo
{
private bool _myProp = false;
public [[[BaseFoo?]]] SetMyProperty()
{
this._myProp = true;
return this;
}
}
public class DerivedFoo : BaseFoo
{
public bool _derivedProp = false;
public DerivedFoo SetDerivedPropery()
{
this._derivedProp = true;
return this;
}
}
The problem is clearly when trying to chain these together, as soon as I use the base method the returned type of BaseFoo. Obviously I could cast this back to DerivedFoo, but is there a simply way of returning an object of the derived class type.
The only way I can think of doing it is chaining constructors together and passing in the parent type to the initial constructor but usure of syntax.
Another way would be to have like proxy methods for each of the subclass', but returning the derived type.
DerivedFoo foo = new DerivedFoo();
foo.SetMyProperty().SetDerivedPropery(); // wont work because the SetMyProperty returns type of BaseFoo
foo.SetDerivedPropery().SetMyProperty(); // works because I know i'm calling the method of the derived class first
(foo.SetMyProperty() as DerivedFoo).SetDerivedPropery(); // works as i'm casting result of base method to derived type
any ideas?
Thanks in advance,
Sam
What about generics?
public class BaseFoo<TDerived> where TDerived : BaseFoo<TDerived>
{
private bool _myProp = false;
public TDerived SetMyProperty()
{
this._myProp = true;
return (TDerived)this;
}
}
public class DerivedFoo : BaseFoo<DerivedFoo>
{
public bool _derivedProp = false;
public DerivedFoo SetDerivedPropery()
{
this._derivedProp = true;
return this;
}
}
But I really think that the coupling between the different levels of the inheritance hierarchy is a big design problem.
I suggest you have a look at other fluent interfaces like Fluent NHibernate, Rhino Mocks etc, to see how the "big" guys do it ;-)
You could also use extension methods like this, sort of an fluent upgrade. This seems cleaner to me.
public static class FooFluentExtensions
{
public static T SetMyProperty<T>(this T foo) where T : BaseFoo
{
foo.MyProp = true;
return foo;
}
public static T SetDerivedPropery<T>(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();
}
You could encapsulate every method that you want to be fluent.
Personally, I loathe this style of coding.
Can't you make them properties? Then you can do
DerivedFoo foo = new DerivedFoo
{
MyProperty = true,
DerivedProperty = true
};
It's much cleaner and more readable.
One option is to do something like this:
public class BaseFoo<T> where T : class
{
private bool _myProp = false;
public T SetMyProperty()
{
this._myProp = true;
return this as T;
}
}
public class DerivedFoo : BaseFoo<DerivedFoo> { ... }
But it depends on your scenario.
精彩评论