20070612

IEnumerable<T>, Fluent Interfaces, Specification Pattern = Ménage à trois

Since I haven't blogged for months, might as well bust a Ménage à trois post.

In this post you will learn the following:

- An implementation of IEnumerable<T>
- Fluent interfaces
- Simpe implementation of the Specification Pattern

To begin, here's the test...


using System.Collections;

using System.Collections.Generic;

using MbUnit.Framework;

using specification;

 

namespace test

{

    [TestFixture]

    public class InventoryTest

    {

        [Test]

        public void Add_ShouldBuildInventory_UsingAdd_ButItIsNotAsCool()

        {

            Inventory musicalInstrumentInventory = new Inventory();

            musicalInstrumentInventory.Add(new MusicalInstrument("Guitar"));

            musicalInstrumentInventory.Add(new MusicalInstrument("Turntables"));

            musicalInstrumentInventory.Add(new MusicalInstrument("Drums"));

            musicalInstrumentInventory.Add(new MusicalInstrument("Banjo"));

 

            int totalMusicalInstruments = 0;

            IEnumerator enumerator = musicalInstrumentInventory.GetEnumerator();

            while (enumerator.MoveNext())

            {

                totalMusicalInstruments++;

            }

 

            Assert.AreEqual(4, totalMusicalInstruments);

        }

 

        [Test]

        public void With_ShouldBuildInventory_UsingFluentInterface()

        {

            Inventory musicalInstrumentInventory = new Inventory()

                .With(new MusicalInstrument("Guitar"))

                .With(new MusicalInstrument("Turntables"))

                .With(new MusicalInstrument("Drums"))

                .With(new MusicalInstrument("Banjo"));

 

            int totalMusicalInstruments = 0;

            IEnumerator enumerator = musicalInstrumentInventory.GetEnumerator();

            while (enumerator.MoveNext())

            {

                totalMusicalInstruments++;

            }

 

            Assert.AreEqual(4, totalMusicalInstruments);

        }

 

        [Test]

        public void FindAll_ShouldFindGuitar()

        {

            MusicalInstrument guitar = new MusicalInstrument("Guitar");

            Inventory musicalInstrumentInventory = new Inventory()

                .With(guitar)

                .With(new MusicalInstrument("Turntables"))

                .With(new MusicalInstrument("Drums"))

                .With(new MusicalInstrument("Banjo"));

 

            MusicalInstrument resultGuitar =

                    new List<MusicalInstrument>

                        (

                            musicalInstrumentInventory.FindAll(new GetGuitarSpecification())

                        )[0];

 

            Assert.IsTrue(guitar.Equals(resultGuitar));

        }

 

        private class GetGuitarSpecification : ISpecification<MusicalInstrument>

        {

            public bool IsSatisfiedBy(MusicalInstrument item)

            {

                return item.Name.Equals("Guitar");

            }

        }

    }

}





Now the code...


using System.Collections;

using System.Collections.Generic;

 

namespace specification

{

    public class Inventory : IEnumerable<MusicalInstrument>

    {

        private List<MusicalInstrument> instruments;

 

        public Inventory()

        {

            instruments = new List<MusicalInstrument>();

        }

 

        public void Add(MusicalInstrument musicalInstrument)

        {

            instruments.Add(musicalInstrument);

        }

 

        public Inventory With(MusicalInstrument musicalInstrument)

        {

            Add(musicalInstrument);

            return this;

        }

 

        public IEnumerable<MusicalInstrument> FindAll(ISpecification<MusicalInstrument> specification)

        {

            return instruments.FindAll(delegate(MusicalInstrument instrument)

                                          {

                                              return specification.IsSatisfiedBy(instrument);

                                          });

        }

 

        IEnumerator<MusicalInstrument> IEnumerable<MusicalInstrument>.GetEnumerator()

        {

            return instruments.GetEnumerator();

        }

 

        public IEnumerator GetEnumerator()

        {

            return instruments.GetEnumerator();

        }

    }

}



And finally the ISpecification Interface


namespace specification

{

    public interface ISpecification<PersistentItem>

    {

        bool IsSatisfiedBy(PersistentItem item);

    }

}

0 comments: