-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Hey, currently we are creating the builder right inside the source code of the class that should be built. This pollutes the actual class and often times we don't have full control over the class that should be built, so it would be nice if the library would support building an other object from the builder. This is currently already possible using extension methods:
[FluentApi]
public class StudentBuilder {
[FluentMember(0)]
public string Model {get;private set;}
}
public static class StudentBuilderExtensions {
public static ThirdPartyStudent Build(this StudentBuilder builder) {
return new ThirdPartyStudent(builder.Model, ...);
}
}but this could be made somewhat easier, here is an example how it could work:
[FluentApi]
public class StudentBuilder { // cherry on top: define this as partial and generate the methods as part of this class, because naming is hard already and having two classes with somewhat similar names confuses users
[FluentMember(0)]
public string Model {get;private set;}
// ...
[FluentBuilder]
private ThirdPartyStudent Build() {
return new ThirdPartyStudent(Model, ...);
}
}This might seem somewhat unnecessary at first, but I think it could be a bridge to a much greater feature, which is Generics. I am aware that Generics are hard to implement, because they might not map exactly to the type parameters of the built class and then there are type constraints. The approach that I am suggesting here would make it easier for you as the maintainer to support generics, but would also reveal much more possibilities to use the library:
[FluentApi]
public class StudentBuilder<TBuilderModel> { // cherry on top: define this as partial and generate the methods as part of this class, because naming is hard already
[FluentMember(0)]
private TBuilderModel Model {get; set;}
// ...
[FluentBuilder]
private ThirdPartyStudent<TResultModel> Build() {
return new ThirdPartyStudent<TResultModel>(Model); // .NET compiler will ensure that the types are compatible and constraints are satisfied, library user has the responsibility to fix any issues
}
}
// usage:
ThirdPartyStudent<string> student = CreateStudentBuilder<string>.WithModel("test");