this is what I’m trying to achieve:
config.Name("Foo")
.Elements(() => {
Element.Name("element1").Height(23);
Element.Name("element2").Height(31);
})
.Foo(23);
or like this:
.Elements(e => {
e.Name("element1").Height(23);
e.Name("element2").Height(31);
})
.Foo(3232);
this is what I have for the moment:
public class Config
{
private string name;
private int foo;
private IList<Element> elements = new List<Element>();
public Config Name(string name)
{
this.name = name;
return this;
}
public Config Foo(int x)
{
this.foo = x;
}
... //add method for adding elements
class Element
{
public string Name { get; set; }
public int Height { get; set; }
}
}
anybody knows how to do this ?
Any reason you don’t want to use object and collection initializers?
Then:
If you don’t want to expose the list of elements directly, you could always turn that into a builder, and copy it into a more private data structure on
Build:This has the additional advantage that you can make
Configitself immutable.If you always need
Nameto be present, just take that as a constructor parameter instead.While there are times where it’s good to have a fluent interface with mutating (or copy-and-change) method calls, in this case I think collection/object initializers are more idiomatic C#.
Note that if you’re using C# 4 and you want to make your
Elementconstructor calls, you can always use named arguments: