04.07.2013 Views

Programming Entity Framework - Cdn.oreilly.com

Programming Entity Framework - Cdn.oreilly.com

Programming Entity Framework - Cdn.oreilly.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13<br />

Creating and Using POCO Classes<br />

When it was first released, <strong>Entity</strong> <strong>Framework</strong> was roundly criticized by agile developers.<br />

These developers hold the tenets of domain-driven development and testability very high.<br />

The classes generated from the <strong>Entity</strong> Data Model (EDM) are very tightly bound to the<br />

<strong>Entity</strong> <strong>Framework</strong> APIs by either inheriting from the <strong>Entity</strong>Object or implementing<br />

interfaces that allow the classes to participate in change tracking and relationship<br />

management.<br />

The problem with this is that it is extremely difficult to write unit tests with these classes.<br />

Unit tests need to emulate persistence. In other words, during a test, instead of literally<br />

querying the database, a test might just supply some fake data to a class, or instead of<br />

sending data to the data store, a test might say “OK, let’s pretend that part just happened<br />

and we’ll move on now.”<br />

In addition to the problem the dependent classes create for testing, it also makes it<br />

difficult for developers to change their backend infrastructure if needed. For example, an<br />

application might be written using another object relational mapping tool, such as<br />

NHibernate. If a developer wanted to switch to the <strong>Entity</strong> <strong>Framework</strong>, version 1 made it<br />

very difficult to just take the existing classes and put <strong>Entity</strong> <strong>Framework</strong> behind. The<br />

developer would be required to make some major changes to the classes, binding them<br />

tightly to <strong>Entity</strong> <strong>Framework</strong>.<br />

The <strong>Entity</strong> <strong>Framework</strong> team listened to and learned from the agile <strong>com</strong>munity and added<br />

a number of new mechanisms for supporting agile development.<br />

One of these is support for Plain Old CLR Objects (POCO). POCOs are classes that<br />

remain free from a backing infrastructure, such as <strong>Entity</strong> <strong>Framework</strong>. A POCO class<br />

would not inherit from <strong>Entity</strong> <strong>Framework</strong>’s <strong>Entity</strong>Object. But as you’ve seen, the<br />

<strong>Entity</strong>Object performs all of the important work for providing relationship and<br />

change tracking information to the context. In order to remove the dependency on<br />

<strong>Entity</strong>Object, the <strong>Entity</strong> <strong>Framework</strong> gained some new functionality that allows the<br />

ObjectContext to acquire relationship and change tracking information from classes<br />

that do not inherit from <strong>Entity</strong>Object. In fact, as you’ll see, it can do so with classes<br />

that have no knowledge at all about the <strong>Entity</strong> <strong>Framework</strong>.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!