The purpose of this document is to question how the .NET 4.0 Entity Framework will handle POCOs and Persistent-Ignorant objects. These concepts are not new to enterprise and domain-driven developers. In fact, these concepts precede that of .NET and C#. I will not provide a discussion on these topics because there are many excellent guides, books, and other resources available.
- · Technology: Microsoft .NET 4.0 Entity Framework
- · Tools: Visual Studio 2010
- · Database: SQL Server 2008 or SQL Server 2008 Express Edition
In this discussion, I will raise some concerns about the Entity Framework; and implementation details that the Entity Framework should allow. The Entity Framework is really a dual-edge sword. First, it provides entity classes to have extended base functionality (e.g. change tracking, unit of work, deferred or “lazy” loading). The other edge is the ability to persist and to retrieve entity items from the data store. We should also throw in some domain-driven design patterns like repository, adaptor – and even add the provider-model pattern. How can we make all of this work together?
Just a thought, if you are a home builder or contractor – there are always new tools and products released promising to make the job easier. When tools are being selected, they are usually selected for the types of jobs or tasks that the builder routinely does. Most home builders rarely need a crane to perform day-to-day tasks. Nor do they use a simple hand shovel to dig a foundation for a new home. Typically, the builder owns and purchases the tools that he uses most. The other tools (e.g. cranes and bull-dozers) are rented or contracted out to another company. I wonder what category the new Entity Framework fits in? This is an important question for developers.
We cannot just choose to the Entity Framework because it is new and part of .NET 4.0; or that Visual Studio 2010 provides nice visual tools to work with. Really, what kind of applications is the Entity Framework really suited for? Is it for simple utility applications or small web applications with a limited number of users? Can it be adapted or incorporated into enterprise-level applications and ease the development effort? What about performance? If the application requires a high-level of performance and/or scalability – would the new Entity Framework be a good choice. There are a lot of questions here. The answers will come in the next few months and years.
This is really where it begins. The domain model provides the items that will be used within the application. The names, types, and relationships between the domain items are very important and should be well thought out. This process requires a combination of right and left brain activity. It is not just a science but also an art that can be crafted and improved upon over time. The domain models you create now would probably be different from ones you would create years from now. Depending on your skill-level, creativity, years of experience – the models you create today will only be as good as you are today. Years from now, we would hope that the model for the domain currently at hand would be improved or better based on your heightened skills and creativity.
The domain model is a set of classes that map to the domain of the application. It appears that with the current Entity Framework that there is a clear dependency on the model and the schema of the database. For example, with POCO and persistent ignorance models – the entity classes are simple. However, in order for it to work with the Entity Framework, the property names have to match to the column names in the database tables. Should the model look like the database schema? In fact with the new Entity Framework, you can generate your database schema/script and create a database from the model. So, another question, are the classes created for the domain a relational model or an object model? Inheritance rules. Design by interface. The common techniques should still be used by domain-driven developers who are creating class hierarchies that are used in the application domain.
What we need is for the Entity Framework to provide mapping capabilities so that the impedance mismatch discussed in domain-driven design and POCO development can be addressed. Does the Entity Framework and the tools (e.g. Visual Studio 2010) allow for management and development of the mapping. Should the names of the properties in a domain object use the same names in the database schema? By default, this is a requirement when using POCOs with the Entity Framework. In most real-world and enterprise applications this will certainly not work. I do not want to use the names that the DBA used to create the database schema as the names for me to use with my entity classes. I image that you can decorate properties in domain classes with attributes that allow mapping to the correct item in the model. However, is the POCO still a plain-old-CLR-object? It now contains mapping information. Our next item of discussion please.
At the core of making all of this work is the mapping between an entity and the data store. Something magical happens in the middle that allows you to create, modify and save changes, and to retrieve single or collections of entity items from the data store. This leads to another question/requirement about the Entity Framework. It should allow you to connect to and to retrieve information from different kinds of data stores.
The solution is in how the information contained in an entity is mapped to a database schema. This really has been at the heart of ORM solutions for almost a decade now. There are many commercial and home-grown solutions that have been developed. How they perform this mapping varies. Configuration files, code generation based on database schemas, data adaptor patterns and other technical solutions provide the mechanism to make the magic happen.
There is a saying, “that the best developers are lazy.” I don’t know who came up with this phrase – over the years I am beginning to doubt this explanation. Programmers or developers that don’t like to write code. Maybe we just don’t like to write boring code – we want to write the sexy code that performs some great logic. I guess sending information or retrieving it from a database is not very sexy or exciting code to write. However, it is probably one of the more important features of an application. It appears that some delegate this task to code generators or other tools and processes to lighten the load. I hope that the Entity Framework is one of those tools. Some may take the approach of code generation for some of the code (e.g. scaffolding or infrastructure) that provides a jump-start in the development process. Some tools provide a round-trip approach and everything is code-generated and cannot be modified because regeneration will overwrite the modified code.
This raises another good question about the effect of changes to the schema and/or the entity model has on the Entity Framework. Is there a strong enough dependency that when one changes that the other must be modified also? Should there be an abstraction of the persistence layer and the mapping between the data schema and the domain model? I think so. If the domain model entities look like tables in the database and are tightly coupled to the mapping/persistence layer – is this a good thing.
I have found that many times when I develop entity models that the items sometimes look very similar to the database tables. It is an interesting development approach to have the developers with (2) different skill-sets to develop each layer. I think the relational and object-oriented approaches that each developer takes will provide two very distinct solutions. Thus the need to map. The mapping has to occur somewhere in the application. Also, depending on the performance and scalability requirements of the application – how this is done and what technologies used may be very important. I would suggest that prototypes are developed to load test each solution to determine which provides the best performance and/or allows for the greatest scalability. You really need to understand how the application will be used early to be able to make such decisions. This goes back to the principle that we just don’t use a tool because it is new and there is a lot of attention given to it. Think of the tools and technologies that have stood the test of time. Many of these are simple and provide elegant solutions. There is even a bigger list of tools, frameworks, and other technology rejects that are no longer used or just simply forgotten. They may have served a purpose for a time – but the test of longevity lies in the simplicity and elegance of what the tools provides. For example, you do not hear Microsoft talking about BizTalk too much these days? This tool served its purpose – but does it provide the same value as it did back in the day. I haven’t heard of the new BizTalk 2010 with Visual Studio integration!
The new tools may come and go. However, there is still an art to hand-crafted software using proven and tested techniques, patterns, and tools. We could spend all of our time learning the new tools and how the work – or even better what we need to do to create workarounds for things that they cannot do yet, but are promised in an upcoming release.