I wrote my own custom data layer to save it in a specific file, and I rendered it using the DataContext template.
All this is based on the .NET 2.0 Framework (predefined restrictions for the target server), so although some of them may look like LINQ-to-SQL, it doesn’t! I just implemented a similar data pattern.
See the example below, for example, for a situation that I still cannot explain.
To get all instances of Animal - I do this and it works great
public static IEnumerable<Animal> GetAllAnimals() { AnimalDataContext dataContext = new AnimalDataContext(); return dataContext.GetAllAnimals(); }
And the implementation of the GetAllAnimals () method in AnimalDataContext () below
public IEnumerable<Animal> GetAllAnimals() { foreach (var animalName in AnimalXmlReader.GetNames()) { yield return GetAnimal(animalName); } }
AnimalDataContext () implements IDisposable, because I have an XmlTextReader, and I want to make sure that it clears quickly.
Now, if I end the first call inside the using statement like this
public static IEnumerable<Animal> GetAllAnimals() { using(AnimalDataContext dataContext = new AnimalDataContext()) { return dataContext.GetAllAnimals(); } }
and put a breakpoint on the first line of the AnimalDataContext.GetAllAnimals () method and another breakpoint on the first line of the AnimalDataContext.Dispose () method and do ...
The Dispose () method is called FIRST, so AnimalXmlReader.GetNames () provides an “object reference not set to an object instance” exception because AnimalXmlReader is null in Dispose () ???
Any ideas? I have a hunch that its connection with yield return cannot be called inside the try-catch block, which with the help of effectively represents, after compilation ...
Neil 08 Oct '09 at 16:53 2009-10-08 16:53
source share