Entity Framework (Linq to Entities) to IDataReader Adapter

More details about this project may be found on my blog.

Description

Given an Entity Data Model XML (EDMX), this utility generates valid IDataReaders across all entity types, allowing Linq to Entities (L2E) to be used in legacy systems that require this interface to be implemented.

The generated IDataReaders are strongly-typed and performant.

IDataReaderAdapter.Model.png

Goals

  • Provide a per-entity adapter for each entity in a model that implements the IDataReader & IDataRecord interfaces for use in legacy systems.
  • Resultant IDataReaders should be internally strongly-typed and performant (and thereby not use reflection).
  • Model adaptation should be straightforward and easy-to-update as the model itself changes.
  • Expose all entity properties, including "flattened" properties established through entity relationships

Background

Microsoft's Entity Framework introduces a new data design paradigm within .NET. This framework, however, can be difficult to implement in projects that internally rely heavily on the exchange of IDataReader-implementing objects between layers (or within interface contracts). This is a shortcoming that is not easily overcome. In many ways, migrating these projects to the Entity Framework is an all-or-nothing proposition.

It would be advantageous to provide adaptation for entities within a Linq to Entity model that was usable in such a system. New code could rely directly entity classes, while legacy interfaces could function as before. Such a system could be incrementally converted to the full L2E implementation. This incremental approach has many practical advantages.

This project allows such an approach with legacy systems. It includes a command-line code generator which produces strongly-typed and performant IDataReader adapters on a per-entity basis. The resultant adapted IDataReaders may be used in any way, include across layers, so long as the underlying entities expose those properties expected by a consumer (include exposure through relationship).

Usage

  1. Download the latest release
  2. Execute the command-line IDataReader code generation tool to generate the data readers for your model. For example:
IDataReaderAdapterGenerator /entities:Example.edmx /output:BrandonHaynes.Data.DataReaders.cs
  1. Include the generated code file in your project (BrandonHaynes.Data.DataReaders.cs in the example above)
  2. Add a reference to the assembly BrandonHaynes.DataReaderAdapter.dll

Example

This code sample assumes that the code generation utility has been executed on the Example.edmx file included in the release package. The command line use for generation is:

IDataReaderAdapterGenerator /entities:Example.edmx /output:BrandonHaynes.Data.DataReaders.cs

The Example.edmx file contains an entity named "Sample." Here we demonstrate a function that returns an IDataReader over an enumerable set of Sample entities:

public IDataReader GetSamples()
	{
	using (var context = new SampleContext())
		return new DataReaders.SampleDataReader(context.Samples);
	}

Feedback Appreciated!

Feedback about your experiences is needed, and greatly appreciated!

Last edited Sep 27, 2010 at 12:37 PM by BrandonHaynes, version 10