Object oriented programming languages encapsulate data in objects.
Relational databases are based on the mathematical concept of relational algebra.
This conceptual discrepancy is called relational impedance mismatch.
# Object Relational Mapping (2)
Object Relational Mapping is a programming technique that converts between data available in relational databases and objects in object-oriented programming languages.
Entity Framework Core is an implementation of an ORM for dotnet.
Dapper also an ORM for dotnet.
# Object Relational Mapping (3)
# Basic Techniques
ORM maps classes to tables.
One object corresponds to one row in a database table.
Properties correspond to columns of a table.
The Identity of one object is defined by the primary key of a table.
# Basic Techniques (2)
Data entries and foreign keys will be transformed to objects and references.
When writing to the database, this conversion will be performed in the reversed direction.
# Entity
An Entity is an object that can be stored into a database.
To use an object as an entity, the object’s class must be registered within the database context of an application.
We have to register it in DbContext
# Entity - Default Mapping
When no further information is given, Entity Framework will try to guess how to map an entity to the database (does mapping per default).
# Entity - Define Mapping
Entity Framework uses 2 techniques to define the mapping between entities and the database.
Annotations (C# Attributes)
Fluent API (C# Method calls)
# Annotation - Table
By using the Table Annotation, you can specify the name of the table in the database.
# Annotation - Required
Properties that are marked with the Required attribute will get a not null constraint in the database.
# Annotation - NotMapped
Properties marked with the NotMapped attribute, won’t be mapped to the database.
# Annotation - Column
The Column attribute defines the column name that should be used for a property.
# Annotation - Key
The Key attribute marks a column as primary key.
By default a property that is named Id or Id prefixed by the Classname (e.g. DishId) will be used as primary key.
# DbContext
The DbContext is the central interface to communicate with the database when using Entity Framework.
The DbContext class is used to resolve the Relational impedance mismatch.
# DbContext (2)
# DbContext - Registering Entities
Classes that should be mapped to the database must be registered first.
Registering happens by specifying a property of type DbSet for each class that should be mapped.
# DbContext - Configuring Entities
The DbContext can also be used to configure entities. Configuring means to define the mapping.
Entities can be defined completely in the DbContext without using Annotations at all.
# DbContext - Fluent API
FluentAPI - uses Method chaining
# DbContext - Fluent API (2)
# DbContext - Fluent API (3)
# ORM - Advanced Concepts
ORM bridges the gap between relational databases and object-oriented programming.
Some concepts from object-oriented design must be considered specifically for the mapping.
Inheritance
Object References
Composite keys
# ORM - Inheritance
Class inheritance is used in object-oriented design to define entities with similar properties in a hierarchical structure.
The relational design defines several different forms of inheritance.
# ORM - Single Table Inheritance
Single Table Inheritance saves all values of all (base and derived) entities into a single table.
This is also called Table-per-hierarchy configuration and is the default mapping strategy for inheritance when using Entity Framework Core.
# ORM - Joined Table Inheritance
The object data will be stored in a distributed way in multiple tables.
This is also called Table-per-type configuration.
Each class has a Table Annotation - so, EF knows, that every Type should be an own Entity.
# ORM - Table-per-concrete-Type Inheritance
In the TPC mapping pattern, all the types are mapped to individual tables. Each table contains columns for all properties on the corresponding entity type. This addresses some common performance issues with the TPT strategy.
# ORM - 1:1 Relation
A 1:1 relation exists between 2 entities, if one of the entities has a reference to the other entity.
In a 1:1 Relation - the foreign key is unique.
# ORM - Inheritance Summary
In summary, TPH is usually fine for most applications, and is a good default for a wide range of scenarios, so don’t add the complexity of TPC if you don’t need it. Specifically, if your code will mostly query for entities of many types, such as writing queries against the base type, then lean towards TPH over TPC.
That being said, TPC is also a good mapping strategy to use when your code will mostly query for entities of a single leaf type and your benchmarks show an improvement compared with TPH.
Use TPT only if constrained to do so by external factors.
# ORM - 1:n Relation
on the n side we need the foreign key.
# ORM - m:n Relation
# Layers / Tiers
We typically build up our applications in layers. (Tier is just a synonym for layer.)
Each layer is responsible for one specific purpose. Splitting up responsibilities is also often referred to as separation of concerns.
One layer only communicates with the layers right next to itself, but never skips a layer.
# 3 Tier Architecture
The 3 tier architecture is one of the most widely used approaches in how to structure software.
The three layers used in this architecture are as follows.
# Data Access Layer
Synonyms:
DAL
Model
Data Layer
Responsible for accessing (reading/writing) data. Very often this means accessing a database, but how to persist data is not standardized. You could have a data access layer that stores data to files just as well. When using C# the data access layer often is implemented using an ORM like Entity Framework.
# Business Logic Layer
Synonyms:
BL
Logic
Domain
This layer is responsible for processing information. We work on a higher level than accessing individual database tables. Additionally, to using the DAL for accessing single tables, the BL can contain more complex operations including multiple datasets and other systems such as an e-mail service.
Example:
a method for registering the user in a DB
then, the user should receive a verification Mail
# Presentation Layer
Synonyms:
View
Service Layer
The presentation layer presents the information to the user and offers possibilities to modify it. Very often, the presentation layer will be a (web-) application. In a way, a simple API, such as a REST-API, can also be considered a presentation layer in the context of a three-tier architecture.
# Domain Layer - Repository
The repository pattern is a widely used way to structure your code to get reusable functionality to access data.
The IRepository interface connects the presentation layer and the domain layer.
# IRepository Pattern
The IRepository is a generic interface. This means it can be implemented for different entities. Users of the IRepository might want to use a IRepository<Teacher> or a IRepository<Pupil> to access either teacher data or pupil data.
# IRepository - Create
!! Note: all classess here are sync over async !!
# IRepository - Update
# IRepository - Read
# IRepository - Delete
# ARepository
Instead of implementing all the interface methods for each entity, we use an abstract base class where we implement all these functionalities once, that doesn’t differ between different entities.