Click or drag to resize
BuilderT Class
An abstract base class for Builder classes.
Inheritance Hierarchy
SystemObject
  Capgemini.CommonObjectUtilsBuilderT

Namespace: Capgemini.CommonObjectUtils
Assembly: Capgemini.CommonObjectUtils (in Capgemini.CommonObjectUtils.dll) Version: 1.3.0.0
Syntax
public abstract class Builder<T>

Type Parameters

T
The type that the builder builds.

The BuilderT type exposes the following members.

Constructors
  NameDescription
Protected methodBuilderT
Initializes a new instance of the BuilderT class
Top
Methods
  NameDescription
Public methodBuild
Builds the target object.
Protected methodBuildImplementation
The internal implementation that builds the target object.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodExpect
Tells the builder that an argument is expected.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodGot
Checks if an argument has received.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodReceiveTArg
Tells the builder that an argument was received.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

You can inherit from this class when writing your own builders.

Call the Expect(String, BuilderTNecessity) method to tell the class what arguments your builder needs and which ones are mandatory.

Call the ReceiveTArg(String, TArg) method as your builder gets each argument to tell the builder which arguments you have received.

When client code calls the Build method the Builder class will check that all mandatory arguments have beens supplied and if they haven't it will throw a MissingBuilderArgumentException. If all the required arguments have been supplied the builder calls the BuildImplementation method that the sub-class supplies and building finishes.

If you need to know if a particular argument got supplied you can call the Got(String) method instead of tracking that yourself.

Examples
Here is an example that builds integers with a mandatory starting value and an optional multiplier.
private class IntBuilder : Builder<int>
{
    private int value;
    private int multiplier;

    public IntBuilder()
    {
        Expect("value", Necessity.Mandatory);
        Expect("multiplier", Necessity.Optional);
    }

    public IntBuilder SetValue(int value)
    {
        this.value = Receive("value", value);
        return this;
    }

    public IntBuilder SetMultiplier(int multiplier)
    {
        this.multiplier = Receive("multiplier", multiplier);
        return this;
    }

    protected override int BuildImplementation()
    {
        return value * (Got("multiplier") ? multiplier : 1);
    }
}
See Also