APICore
APICore
Mohamed ELshafei
Prerequisite - Dependency injection (DI)
• Dependency injection (DI) is a technique for achieving loose coupling
between objects and their collaborators, or dependencies. Rather
than directly instantiating collaborators, or using static references, the
objects a class needs in order to perform its actions are provided to
the class in some fashion.
• Most often, classes will declare their dependencies via their
constructor, allowing them to follow the Explicit Dependencies
Principle. This approach is known as "constructor injection"
Prerequisite - Dependency Inversion Principle
(DIP)
• The Dependency Inversion Principle (DIP) states that high level
modules should not depend on low level modules; both should
depend on abstractions (typically interfaces). Abstractions should not
depend on details.
• Details should depend upon abstractions which are provided to them
when the class is constructed.
Prerequisite - containers
• A container is essentially a factory that's responsible for providing
instances of types that are requested from it.
• No route Prefix.
Multiple Routes
Specifying attribute route optional
parameters, default values, and constraints
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;“
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Working with EF Core’s AppDbContext
• Never directly instantiate your application’s DbContext type(s)
• Instead, request from DI using constructor parameters
Recommendation
• Follow the Dependency Inversion Principle and avoid having UI types
depend on details
• Using Entity Framework Core is an implementation detail
• Instead depend on an abstraction over your persistence method
• Common pattern for this: Repository
Loading Related Data
Entity Framework Core allows you to use the navigation properties in
your model to load related entities. There are three common O/RM
patterns used to load related data.
• Eager loading means that the related data is loaded from the
database as part of the initial query.
• Explicit loading means that the related data is explicitly loaded from
the database at a later time.
• Lazy loading means that the related data is transparently loaded from
the database when the navigation property is accessed.
Eager loading
• You can use the Include method to specify related data to be included
in query results.
EF Core will then enable lazy-loading for any navigation property that
can be overridden--that is, it must be virtual and on a class that can be
inherited from.
Performance considerations
• If you know you need related data for every entity retrieved, eager
loading often offers the best performance, because a single query
sent to the database is typically more efficient than separate queries
for each entity retrieved.
• In some scenarios separate queries is more efficient. Eager loading of
all related data in one query might cause a very complex join to be
generated, which SQL Server can't process efficiently.
Help Doc Using NSwag
• NSwag, third-party APIs that incorporate Swagger and generate a client
implementation.
• NSwag allows you to expedite the development cycle and easily adapt to
API changes. Install-Package NSwag.AspNetCore
• The problem was occurring because in .NET Core 3 they change little bit
the JSON politics. Json.Net is not longer supported and if you want to
used all Json options, you have to download this Nuget:
Microsoft.AspNetCore.Mvc.NewtonsoftJson.