using System.Collections;
using System.Collections.Generic;
using Chernobyl.Event;
using Chernobyl.Utility;
namespace Chernobyl.Creation.Collections
{
///
/// An and that stores
/// other instances and acts as a composite
/// (http://en.wikipedia.org/wiki/Composite_pattern) for those instances.
///
/// The type that is to
/// be held by this .
/// The type that is created by the
/// instances that will be contained within this instance.
public class BuilderEnumerable : Builder, IEnumerable
where TFactory : IBuilder
{
///
/// Initializes a new instance of the
/// class.
///
/// The instance whose
/// event is to be forwarded through
/// this instance.
public BuilderEnumerable(IEnumerable builders)
{
builders.ThrowIfNull("builders");
_builders = builders;
foreach (TFactory builder in _builders)
builder.Created += OnItemCreated;
}
///
/// Initializes a new instance of the
/// class. If you invoke
/// this constructor, you must override to provide
/// the appropriate .
///
protected BuilderEnumerable()
{}
///
/// Returns an enumerator that iterates through the collection.
///
/// A that can be used to iterate
/// through the collection.
public IEnumerator GetEnumerator()
{
return Builders.GetEnumerator();
}
///
/// Returns an enumerator that iterates through a collection.
///
/// An object that can be used to
/// iterate through the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// An event handler that can be assigned to any event which notifies
/// client code of created items. This method will raise this instance's
/// event.
///
/// The sender of the event.
/// The arguments containing the event data.
protected void OnItemCreated(object sender, CreationEventArgs e)
{
if (CreatedMethod != null)
CreatedMethod(this, e);
}
///
/// The of
/// instances whose event is being
/// listened to by this instance.
///
protected virtual IEnumerable Builders
{
get { return _builders; }
}
///
/// The backing field to .
///
readonly IEnumerable _builders;
}
}