Database Reference
In-Depth Information
Chapter 8
Plain Old CLR Objects
Objects should not know how to save themselves, load themselves, or filter themselves. That's a familiar mantra in
software development, and especially in Domain Driven Development. There is a good bit of wisdom in this mantra.
Having persistence knowledge bound too tightly to domain objects complicates testing, refactoring, and reuse. In
ObjectContext, the classes generated by Entity Framework for model entities are heavily dependent on the plumbing
of Entity Framework. For some developers, these classes know too much about the persistence mechanism, and they
are too closely tied to the concerns of models and mapping. There is another option, however.
Entity Framework also supports using your own classes for the entities in the model. The term Plain Old CLR
Object, often simply referred to as POCO, isn't meant to imply that your classes are either plain or old. It merely means
that they don't contain any reference at all to specialized frameworks, they don't need to derive from third-party code,
they don't need to implement any special interface, and they don't need to live in any special assembly or namespace.
You may implement your domain objects however you see fit and tie them to the model with a custom object
context. With that being said, you are ready to leverage all of the power of Entity Framework and follow just about any
architectural pattern you choose. You can also use DbContext to generate the POCO classes for you.
This chapter covers a wide variety of recipes specific to POCO. The first recipe shows you the basics of using POCO.
The remaining recipes focus on loading entities and keeping Entity Framework in sync with the state of your objects.
In this chapter, we've intentionally focused on writing most of the POCO-related code by hand to demonstrate
how things work. All of the work involved in building the POCO plumbing goes away if you use the POCO T4 template
available from the ADO.NET development team at Microsoft.
8-1. Using POCO
Problem
You want to use Plain Old CLR Objects (POCO) in your application.
Solution
Let's say that you have a data model like the one shown in Figure 8-1 .
 
Search WWH ::




Custom Search