The fluent API

Image

Data annotations are simple to use, but once you start adding information about how your classes map to the database they can no longer really claim to be persistence-ignorant. They can also get a little unwieldy—I’ve seen classes that had more metadata than code. The second method Entity Framework provides for customizing the model, the Fluent API, provides an alternative that is completely code-based and lets you confine all your database-related code to the context class.

The Entity Framework Fluent API is an example of a technique known as a FLUENT INTERFACE. (Neither has any relationship to the Fluent Learning series of which this book is a member. The etymology of Fluent Learning is spoken languages, not programming techniques.)

One of the hallmarks of a fluent interface is that it uses METHOD CHAINING, a technique whereby the return type of a method call determines the valid methods for the next call. Here’s an abstract example. (It’s not valid .NET code.)

Image

Image In more detail

The Fluent interface was first proposed by Eric Evans and Martin Fowler. If you’re interested in the technique, a good starting point would be Martin Fowler’s first discussion of the topic, which you can find here: http://www.martinfowler.com/bliki/FluentInterface.html


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset