Permalink
Browse files

FG-13 Finders: Cambiar Listas por IEnumerables

  • Loading branch information...
emiliotorrens committed Oct 1, 2012
1 parent 17da904 commit 8c127e06ebcca4f3f07caa4ce4e5948974935196
@@ -0,0 +1,105 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace EtoolTech.MongoDB.Mapper.Test.NUnit
+{
+ [TestFixture]
+ public class Cursors
+ {
+
+ [Test]
+ public void Test()
+ {
+ Helper.DropAllCollections();
+
+ for (int i = 0; i < 100; i++)
+ {
+ var c = new Country { Code = "ES_" + i.ToString(), Name = "España" };
+ c.Save<Country>();
+ Assert.AreEqual(c.MongoMapper_Id, i + 1);
+ }
+
+ IEnumerable<Country> countries = MongoMapper.FindAsCursor<Country>();
+
+ CountryEnumerable countryEnumerable = new CountryEnumerable(countries);
+
+ foreach (Country c in countryEnumerable)
+ {
+ string s = c.Code;
+ }
+
+ List<Country> c2 = countryEnumerable.ToList();
+
+ }
+ }
+
+ public class CountryEnumerable : IEnumerable<Country>
+ {
+ private IEnumerable<Country> _cursor;
+
+ public CountryEnumerable(IEnumerable<Country> cursor)
+ {
+ _cursor = cursor;
+ }
+
+ public IEnumerator<Country> GetEnumerator()
+ {
+ return new CountryEnumerator(_cursor);
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ }
+
+ public class CountryEnumerator : IEnumerator<Country>
+ {
+ private readonly IEnumerable<Country> _cursor;
+ private readonly IEnumerator<Country> _enumerator;
+ private Country _current;
+
+ public CountryEnumerator(IEnumerable<Country> cursor)
+ {
+ _cursor = cursor;
+ _enumerator = _cursor.GetEnumerator();
+ }
+
+ public void Dispose()
+ {
+
+ }
+
+ public bool MoveNext()
+ {
+ while (true)
+ {
+ bool hasNext = _enumerator.MoveNext();
+ if (!hasNext) return false;
+ _current = _enumerator.Current;
+ bool isValid = _current.Code.Contains("1");
+ if (isValid) break;
+ }
+ return true;
+ }
+
+ public void Reset()
+ {
+ _enumerator.Reset();
+ }
+
+ public Country Current
+ {
+ get { return _current; }
+ }
+
+ object IEnumerator.Current
+ {
+ get { return Current; }
+ }
+ }
+}
@@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AmbiguousDiscriminatorTest.cs" />
+ <Compile Include="Cursors.cs" />
<Compile Include="InsertModifyDeleteTest.cs" />
<Compile Include="ConfigTest.cs" />
<Compile Include="FindTest.cs" />

0 comments on commit 8c127e0

Please sign in to comment.