using System;
using System.ComponentModel;
namespace Chernobyl.Creation
{
///
/// An that uses a for creation.
///
public class FuncFactory : FuncBuilder, TResult>, IFactory
{
///
/// Initializes a new instance of the class.
///
/// The method whose return value becomes the
/// item that is returned by this. If this method throws an
/// then it is used as the
/// value.
public FuncFactory(Func function)
: base(function)
{}
///
/// Performs an implicit conversion from to
/// .
///
/// The method whose return value becomes the
/// item that is returned by this. If this method throws an
/// then it is used as the
/// value.
/// The result of the conversion.
public static implicit operator FuncFactory(Func function) => new FuncFactory(function);
///
/// Performs an implicit conversion from to
/// .
///
/// The whose
/// is to be returned.
///
/// The result of the conversion.
public static implicit operator Func(FuncFactory factory) => factory.Function;
///
public TResult Create() => Create(Function, null);
}
///
/// An that uses a for creation.
///
public class FuncFactory : FuncBuilder, TResult>, IFactory
{
/// The method whose return value becomes the
/// item that is returned by this. If this method throws an
/// then it is used as the
/// value.
public FuncFactory(Func function)
: base(function)
{}
///
/// Performs an implicit conversion from to
/// .
///
/// The method whose return value becomes the
/// item that is returned by this. If this method throws an
/// then it is used as the
/// value.
/// The result of the conversion.
public static implicit operator FuncFactory(Func function) => new FuncFactory(function);
///
/// Performs an implicit conversion from to
/// .
///
/// The whose
/// is to be returned.
///
/// The result of the conversion.
public static implicit operator Func(FuncFactory factory) => factory.Function;
///
public TResult Create(T1 arg1) => Create(() => Function(arg1), arg1);
}
///
/// A type used to help create new types
/// that build objects from methods.
///
/// The type of the method whose return value
/// becomes the item that is returned by this.
/// The type that is being constructed and/or configured.
public abstract class FuncBuilder : Builder
{
///
/// Initializes a new instance of the class.
///
/// The method whose return value becomes the
/// item that is returned by this. If this method throws an
/// then it is used as the
/// value.
protected FuncBuilder(TFunc function)
{
Function = function;
}
///
/// Invokes the creation method provided and then reports the item
/// created using .
///
protected TResult Create(Func creator, object userState)
{
TResult result;
try
{
result = creator();
ReportCreation(new CreationEventArgs(result, userState));
}
catch (Exception e)
{
ReportCreation(new CreationEventArgs(e, userState));
throw;
}
return result;
}
///
/// The method whose return value becomes the item that is returned by
/// this. If this method throws an then
/// it is used as the value.
///
public TFunc Function { get; set; }
}
}