using System;
using System.Collections.Generic;
using Chernobyl.Collections.Generic;
namespace Chernobyl.Creation
{
///
/// An type that can construct and/or configure instances of type
/// .
///
/// The type that is being constructed and/or configured.
public interface IFactory : IBuilder
{
///
/// Constructs and/or configures an instance of type .
///
TResult Create();
}
///
/// An type that can construct and/or configure instances of type
/// using a single argument. If you wish to have multiple arguments,
/// it is recommended you use an argument that can hold multiple instances such as a
/// or .
///
public interface IFactory : IBuilder
{
///
/// Constructs and/or configures an instance of type .
///
TResult Create(T1 arg1);
}
///
/// Utility methods for working with the factory types such as
/// .
///
public static class FactoryExtensions
{
///
/// An event handler style method that can be assigned to an event so that the
/// method is invoked.
///
public static void Create(this IFactory factory, object sender, EventArgs e)
=> factory.Create();
///
/// Returns a that creates an instance from the specified factory.
///
public static Lazy LazyCreate(this IFactory factory) =>
new Lazy(factory.Create);
///
/// Returns a that creates an instance from the specified factory.
///
public static Lazy LazyCreate(this IFactory factory, Arg arg1) =>
new Lazy(() => factory.Create(arg1.Func()));
///
/// Returns a that creates an instance from the specified factory.
///
public static Lazy LazyCreate(this IFactory factory, T1 arg1) =>
factory.LazyCreate(arg1.AsArg());
///
/// Returns a that creates an instance from the specified factory.
///
public static Lazy Create(this Lazy> factory) =>
factory.Select(f => f.Create());
///
/// Returns a that creates an instance from the specified factory.
///
public static Lazy Create(this Lazy> factory, T1 arg1) =>
factory.Select(f => f.Create(arg1));
}
}