Created
November 1, 2013 11:05
-
-
Save davidwhitney/7263945 to your computer and use it in GitHub Desktop.
Fluent builder example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace FleuntExample | |
{ | |
public class BootstrappingYourLibrariesWithFluentApis | |
{ | |
public BootstrappingYourLibrariesWithFluentApis() | |
{ | |
// To make my library, I can instantiate everything independantly | |
var firstD1 = new Dependency(); | |
var firstD2 = new Dependency2 {SomeConfigurationProperty = "aa", SomeOptionalConfigurationProperty = "bb"}; | |
var firstLibraryInstantiation = new MyLibraryEntryPoint(firstD1, firstD2); | |
// Or, you can offer a discoverable entry point and build a fluent interface | |
var configured = | |
MyLibrary.SetUp().With( | |
x => | |
{ | |
x.SomeConfigurationProperty = "my setting"; | |
}) | |
.AndSomeOptionalThing(x => { }) | |
.AndSomeOptionalThing(x => { }) | |
.AndSomeRequiredFinalThing(x => { }); | |
var secondInstance = configured.Create(); | |
} | |
} | |
public static class MyLibrary | |
{ | |
private static MyLibraryBuilder _builder; | |
public static MyLibraryBuilder SetUp() | |
{ | |
_builder = new MyLibraryBuilder(); | |
return _builder; | |
} | |
public class MyLibraryBuilder : IRequireFurtherConfiguration, IAllowCreation | |
{ | |
public string SomeConfigurationProperty { get; set; } | |
public string SomeOptionalConfigurationProperty { get; set; } | |
public IRequireFurtherConfiguration With(Action<MyLibraryBuilder> withConfiguration) | |
{ | |
withConfiguration(this); | |
return this; | |
} | |
public IRequireFurtherConfiguration AndSomeOptionalThing(Action<MyLibraryBuilder> withConfiguration) | |
{ | |
withConfiguration(this); | |
return this; | |
} | |
public IAllowCreation AndSomeRequiredFinalThing(Action<MyLibraryBuilder> withConfiguration) | |
{ | |
withConfiguration(this); | |
return this; | |
} | |
public MyLibraryEntryPoint Create() | |
{ | |
return new MyLibraryEntryPoint(new Dependency(), | |
new Dependency2 | |
{ | |
SomeConfigurationProperty = SomeConfigurationProperty, | |
SomeOptionalConfigurationProperty = SomeOptionalConfigurationProperty | |
}); | |
} | |
} | |
public interface IRequireFurtherConfiguration | |
{ | |
IRequireFurtherConfiguration AndSomeOptionalThing(Action<MyLibraryBuilder> withConfiguration); | |
IAllowCreation AndSomeRequiredFinalThing(Action<MyLibraryBuilder> withConfiguration); | |
} | |
public interface IAllowCreation | |
{ | |
MyLibraryEntryPoint Create(); | |
} | |
} | |
public class MyLibraryEntryPoint | |
{ | |
private readonly Dependency _d1; | |
private readonly Dependency2 _d2; | |
public MyLibraryEntryPoint(Dependency d1, Dependency2 d2) | |
{ | |
_d1 = d1; | |
_d2 = d2; | |
} | |
} | |
public class Dependency2 | |
{ | |
public string SomeConfigurationProperty { get; set; } | |
public string SomeOptionalConfigurationProperty { get; set; } | |
} | |
public class Dependency | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment