ENTITY FRAMEWORK CORE

WHAT IS THE PURPOSE OF AN ORM AS AN ENTITY FRAMEWORK CORE

NET CORENew technologies such as ORMs have been introduced over the years, and Entity Framework Core is one of them. They are not alternative technologies to ADO.NET but are based on ADO.NET, operating at a slightly higher level. They allow us facilities, such as working with an object model, querying our data with strongly typed queries. ORMs since they work at a higher level introduce a small performance cost, if our site has thousands of users and we do not want to introduce performance cost, working with ADO.NET is a more than viable option.

ORM

For the time being, we say nothing about the object represented by the question marks, unlike ADO.NET in which we enter a string to be sent as a command to the database, and therefore subject to typing errors, with an ORM as you can see it is virtually impossible to make typing errors, because if we mistype something the editor reports it to us immediately and not at Runtime since these are C# methods and properties.

Errore di digitazione

All this is passed from the application service to the infrastructure service.

Servizio infrastrutturale

The ORM is able to convert all this to an SQL query, because after all, we still have a relational database on the other side.

Query sql

The query is then sent to the database, which returns the data to us in table format, transformed by the ORM into a graph of objects.

Grafo oggetti

Then all this object graph passes it back to the application service. An ORM succeeds in mapping between the relational and object-oriented worlds hence the definition of Object Relational Mapper (ORM).

Prestazioni

Let’s see what the advantages and disadvantages are.

Vantaggi

Before unraveling the question marks, let’s do some practice.

Linq

UNDERSTAND AND USE LAMBDA EXPRESSIONS

C# is an object-oriented language, yet the language itself has traits that are purely functional, such as lambda expressions. They are essentially functions that we can assign to a variable just as we would assign any other type of data, an integer a string etc. A lambda like any other object can be passed as an argument in invoking another function.

Espressioni

The third expression contains logic, and to facilitate its reuse we can encapsulate it in a function/method.

Funzione

Let’s look at the use of a lambda expression.

lambda

The expression is an assignment, everything to the right of the equal symbol must be traced back to a Func<DateTime, bool>. This means that the function accepts a DateTime as a parameter (dob = date of birth) and returns a boolean value. To the left of the lambda => operator we have the list of parameters that the function accepts, to the right the body of the function. It is also called an anonymous function because as you see there is no trace of its name. We can also trace it back to the following expression.

Lambda_2

Brackets are optional with one parameter but become mandatory if we have zero or more than one parameter. Let’s look at another feature that introduces the legal age to drive and that can vary from state to state.

lambda_3

This lambda function accepts two parameters the first of type DateTime the second an int and returns a boolean value.

lambda_4

Up to eight parameters can be had. What about functions that do not return void? There are Actions that are lambdas that return void.

Action

To remember:

lambda_5

EXERCISES WITH LAMBDA EXPRESSIONS

Lambda operations