Chapter 2. Understanding the Fluent Configuration API

In this chapter, we will cover the following topics:

  • Improving property maps
  • Creating one-to-one maps
  • Creating one-to-many maps
  • Creating many-to-many maps
  • Mapping one table to many objects
  • Mapping many tables to one object
  • Handling inheritance based on database values
  • Handling complex key maps

Introduction

Have you ever tried to explain the unified field theory to someone that has no understanding of physics? We would have to tell them about the ideas of Einstein on how the universe was put together, and how the basic physic particles interact. If they have not fallen asleep by this point, we then have to explain even more things that are the final details. These details are required for the explanation of theory, but not everyone is interested in them. This is much like what the storage schema in a database means to object-oriented programmers. They have to worry about it at some point, but it will never be their focal point in the way that it is for a database administrator. When we say that the two objects are related, we think something completely different from a database administrator who hears the same phrase. When we try to convey our ideas to someone, it often helps to put them into terms that the other person can understand and relate to. This is not so different from storing objects in a database.

Objects at their most basic level are behavior and state. Objects can be either some kinds of data or the ability to act upon that data, but are often a combination. Storing data is something that our industry has become quite good at over the years, but most of us do not truly understand that it is only half the story.

While the data is within an object, it is hard to store it, as the object is closely tied to the data but not closely tied to the best storage schema. As such, we must recognize that the representation in the database and the representation in the objects should differ sometimes. Converting an object to a record in a table or multiple tables requires separating the data from the object and flattening it out. This process has been accomplished by many Object Relational Mappers (ORMs) through a variety of different approaches. Entity Framework approaches the problem of objects that differ greatly from the relational tables by allowing you to configure the mapping manually where it differs from the standard convention. This will reduce the amount of code that you have to write for the configuration, but it will require the learning of a new and fluent Application Programming Interface (API), which shows the database layer how to translate the code into SQL statements. This allows us, as the programmers, to completely separate the way data is stored in the database from the way our application packages the same data, allowing us to adhere to both object-oriented principles and best database practices.

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

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