namespace Chernobyl.Creation
{
///
/// A implementation of to help in the creation
/// of types.
///
public abstract class Builder : IBuilder
{
///
/// An event that is raised when this creates
/// an instance.
///
public event CreationCompletedEventHandler Created
{
add { CreatedMethod += value; }
// ReSharper disable DelegateSubtraction
// Delegate subtraction is fine here since we are simply forwarding
// the operation to the CreatedMethod. Client must be aware of their
// actions in this case.
remove { CreatedMethod -= value; }
// ReSharper restore DelegateSubtraction
}
///
/// Raises the event with as its args.
///
protected void ReportCreation(CreationEventArgs e) => CreatedMethod?.Invoke(this, e);
///
/// Raises the event with the creation to
/// report.
///
protected void ReportCreation(object created) => ReportCreation(new CreationEventArgs(created));
///
/// The event in the form of a method.
///
protected CreationCompletedEventHandler CreatedMethod { get; private set; }
}
///
/// A implementation of to help in the creation
/// of types.
///
/// The type that is being constructed and/or
/// configured.
public abstract class Builder : Builder, IBuilder
{
///
/// Initializes a new instance of the class.
///
protected Builder()
{
// Any creation events raised from this type should also be raised
// from the base class.
Created += (sender, args) =>
{
base.CreatedMethod?.Invoke(this, args);
};
}
///
/// An event that is raised when this creates
/// an instance.
///
public new event CreationCompletedEventHandler Created
{
add { CreatedMethod += value; }
// ReSharper disable DelegateSubtraction
// Delegate subtraction is fine here since we are simply forwarding
// the operation to the CreatedMethod. Client must be aware of their
// actions in this case.
remove { CreatedMethod -= value; }
// ReSharper restore DelegateSubtraction
}
///
/// Raises the event with as its args.
///
protected void ReportCreation(CreationEventArgs e) => CreatedMethod?.Invoke(this, e);
///
/// Raises the event with the creation to
/// report.
///
protected void ReportCreation(TResult created) => ReportCreation(new CreationEventArgs(created));
///
/// The event in the form of a method.
///
protected new CreationCompletedEventHandler CreatedMethod { get; private set; }
}
}