using Chernobyl.Event; using Chernobyl.Readiness; using Chernobyl.Values; namespace Chernobyl.Creation { /// /// Represents a value that is produced by an . This /// type can be implicitly converted to a /// and so can be consumed by factory create methods. /// /// The type created by the public class Product : HeldValue { /// /// Converts a to an /// for consumption by a /// factory create method. /// /// The product to be converted. /// The converted value. public static implicit operator CreationCompletedEventHandler(Product product) { return product.OnCreation; } /// /// Invoked when an item is created. This method keeps this instance up /// to date. /// /// The sender of the event. /// The argument containing the created instance or /// details on the creation. void OnCreation(object sender, CreationEventArgs e) { if (e.Cancelled == false && e.Error == null) Value = e.CreatedInstance; } } /// /// Utility and extension code for use with . /// public static class ProductExtensions { /// /// Creates an item from an and returns it as /// an . /// /// The type created by the . /// The instance to create the item from. /// The item created represented as an . public static IValue Create(this IFactory factory) { IValue> value = new BasicContainer>(factory); return value.Create(); } /// /// Creates an item from an and returns it as /// an . /// /// The type created by the . /// The instance to create the item from. /// The item created represented as an . public static IValue Create(this IValue> factory) { Product product = new Product(); factory.NotNull().Provide += (sender, e) => ((CreationCompletedEventHandler)product)(sender, new CreationEventArgs(e.NewValue.Create())); return product; } } }