Merge branch 'main' into NET-6

This commit is contained in:
Marcello 2021-07-07 19:04:43 +02:00
commit 9fa261b590
49 changed files with 770 additions and 198 deletions

View file

@ -9,8 +9,8 @@ Components are .NET C# classes built into .NET assemblies that:
- Can be nested and reused.
- Can be shared and distributed as Razor class libraries or NuGet packages.
![Blazor Server Architecture](https://docs.microsoft.com/en-us/aspnet/core/blazor/index/_static/blazor-server.png)
![Blazor WASM Architecture](https://docs.microsoft.com/en-us/aspnet/core/blazor/index/_static/blazor-webassembly.png)
![Blazor Server Architecture](../../.images/dotnet_blazor-server.png)
![Blazor WASM Architecture](../../.images/dotnet_blazor-webassembly.png)
The component class is usually written in the form of a Razor markup page with a `.razor` file extension. Components in Blazor are formally referred to as *Razor components*.
@ -32,8 +32,6 @@ Project
|
|-Pages
| |- _Host.cshtml --> fallback page
| |- Page.cshtml
| |- Page.cshtml.cs
| |- Component.razor
| |- Index.razor
| |- ...
@ -83,6 +81,69 @@ Project
|- Program.cs --> App entrypoint
```
### Blazor PWA Project Structure
```txt
Project
|-Properties
| |- launchSettings.json
|
|-wwwroot --> static files
| |-css
| | |- site.css
| | |- bootstrap
| |
| |- index.html
| |- favicon.ico
| |- manifest.json
| |- service-worker.js
| |- icon-512.png
|
|-Pages
| |- Component.razor
| |- Index.razor
| |- ...
|
|-Shared
| |- MainLayout.razor
| |- MainLayout.razor.css
| |- ...
|
|- _Imports.razor --> @using imports
|- App.razor --> component root of the app
|
|- appsettings.json --> application settings
|- Program.cs --> App entrypoint
```
### `manifest.json`, `service-worker.js` (Blazor PWA)
[PWA](https://web.dev/progressive-web-apps/)
[PWA MDN Docs](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps)
[PWA Manifest](https://developer.mozilla.org/en-US/docs/Web/Manifest)
[Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API)
```json
// manifest.json
{
"name": "<App Name>",
"short_name": "<Short App Name>",
"start_url": "./",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#03173d",
"icons": [
{
"src": "icon-512.png",
"type": "image/png",
"sizes": "512x512"
}
]
}
```
## Common Blazor Files
### `App.razor`
```cs

View file

@ -13,10 +13,8 @@ Custom filters can be created to handle cross-cutting concerns. Examples of cros
Filters run within the *ASP.NET Core action invocation pipeline*, sometimes referred to as the *filter pipeline*. The filter pipeline runs after ASP.NET Core selects the action to execute.
![1][filter-pipeline-1] ![2][filter-pipeline-2]
[filter-pipeline-1]: https://docs.microsoft.com/it-it/aspnet/core/mvc/controllers/filters/_static/filter-pipeline-1.png
[filter-pipeline-2]: https://docs.microsoft.com/en-gb/aspnet/core/mvc/controllers/filters/_static/filter-pipeline-2.png
![filter-pipeline-1](../../.images/dotnet_filter-pipeline-1.png)
![filter-pipeline-2](../../.images/dotnet_filter-pipeline-2.png)
## **Filter types**

View file

@ -23,7 +23,7 @@ When a middleware short-circuits, it's called a *terminal middleware* because
The ASP.NET Core request pipeline consists of a sequence of request delegates, called one after the other.
![request-delegate-pipeline](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/index/_static/request-delegate-pipeline.png)
![request-delegate-pipeline](../../.images/dotnet_request-delegate-pipeline.png)
Each delegate can perform operations before and after the next delegate. Exception-handling delegates should be called early in the pipeline, so they can catch exceptions that occur in later stages of the pipeline. It's possible to chain multiple request delegates together with `Use`.
@ -74,8 +74,8 @@ public class Startup
## Middleware Order
![middleware-pipeline](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/index/_static/middleware-pipeline.svg)
![mvc-endpoint](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/index/_static/mvc-endpoint.svg)
![middleware-pipeline](../../.images/dotnet_middleware-pipeline.png)
![mvc-endpoint](../../.images/dotnet_mvc-endpoint.png)
The Endpoint middleware executes the filter pipeline for the corresponding app type.

View file

@ -1314,7 +1314,7 @@ public static System.Collections.IEnumerable<int> IterateRange(int start = 0, in
## Structs (Custom Value Types) & Classes (Custom Reference Types)
![reference-vs-value](https://blog.penjee.com/wp-content/uploads/2015/02/pass-by-reference-vs-pass-by-value-animation.gif)
![reference-vs-value](../../.images/dotnet_pass-by-reference-vs-pass-by-value-animation.gif)
**Structure** types have _value semantics_. That is, a variable of a structure type contains an _instance_ of the type.

View file

@ -30,7 +30,8 @@ namespace <Project>.Model
NuGet Packages to install:
- `Microsoft.EntityFrameworkCore`
- `Microsoft.EntityFrameworkCore.Tools` to use migrations
- `Microsoft.EntityFrameworkCore.Tools` to use migrations in Visual Studio
- `Microsoft.EntityFrameworkCore.Tools.DotNet` to use migrations in `dotnet` cli (`dotnet-ef`)
- `Microsoft.EntityFrameworkCore.Design` *or* `Microsoft.EntityFrameworkCore.<db_provider>.Design` needed for tools to work (bundled w\ tools)
- `Microsoft.EntityFrameworkCore.<db_provider>`
@ -39,15 +40,6 @@ using Microsoft.EntityFrameworkCore;
namespace <Project>.Model
{
class Context : DbContext
{
public Context(DbContextOptions options) : base(options)
{
}
}
// or
class Context : DbContext
{
private const string _connectionString = "Server=<server_name>;Database=<database>;UID=<user>;Pwd=<password>";
@ -58,8 +50,15 @@ namespace <Project>.Model
optionsBuilder.UseSqlServer(_connectionString); // specify connection
}
// or
public Context(DbContextOptions options) : base(options)
{
}
//DBSet<TEntity> represents the collection of all entities in the context (or that can be queried from the database) of a given type
public DbSet<Entity> Entities { get; set; }
public DbSet<Entity> Entities => Set<Entity>(); // with nullable reference types
}
}
```
@ -89,31 +88,10 @@ dotnet ef database update
### Create
```cs
public static bool InsertOne(Entity entity)
{
int rows = 0;
context.Add(entity);
context.AddRange(entities);
using(var context = new Context())
{
context.Add(entity);
context.SaveChanges();
}
return rows == 1;
}
public static bool InsertMany(IEnumerable<Entity> entities)
{
int rows = 0;
using(var context = new Context())
{
context.AddRange(entities);
context.SaveChanges();
}
return rows == entities.Count();
}
context.SaveChanges();
```
### Read
@ -121,83 +99,27 @@ public static bool InsertMany(IEnumerable<Entity> entities)
[Referenced Object Not Loading Fix](https://stackoverflow.com/a/5385288)
```cs
public static List<Entity> SelectAll()
{
using(var context = new Context())
{
return context.Entities.ToList();
}
}
context.Entities.ToList();
context.Entities.Find(id);
static Entity SelectOneById(int id)
{
using(var context = new Context())
{
return context.Entities.Find(id);
// force read of foreign key identifying referenced obj
return context.Entities.Include(c => c.ForeignObject).Find(id);
}
}
// force read of foreign key identifying referenced obj
context.Entities.Include(c => c.ForeignObject).Find(id);
```
### Update
```cs
public static bool UpdateOne(Entity entity)
{
int rows = 0;
context.Entities.Update(entity);
context.UpdateRange(entities);
using(var context = new Context())
{
context.Entities.Update(entity);
context.SaveChanges();
}
return rows == 1;
}
public static bool UpdateMany(IEnumerable<Entity> entities)
{
int rows = 0;
using(var context = new Context())
{
context.UpdateRange(entities);
context.SaveChanges();
}
return rows == entities.Count();
}
context.SaveChanges();
```
### Delete
```cs
public static bool DeleteOne(Entity entity)
{
int rows = 0;
context.Entities.Remove(entity);
context.RemoveRange(entities);
using(var context = new Context())
{
context.Entities.Remove(entity);
context.SaveChanges();
}
return rows == 1;
}
public static bool DeleteMany(IEnumerable<Entity> entities)
{
int rows = 0;
using(var context = new Context())
{
context.RemoveRange(entities);
context.SaveChanges();
}
return rows == entities.Count();
}
context.SaveChanges();
```

View file

@ -22,7 +22,7 @@ The term *view* in Xamarin.Forms denotes familiar types of presentation and inte
## Pages
![pages](https://d585tldpucybw.cloudfront.net/sfimages/default-source/blogs/2020/2020-11/pages.png)
![pages](../../.images/dotnet_xamarin-pages.png)
```xml
<?xml version="1.0" encoding="utf-8" ?>
@ -48,7 +48,7 @@ The term *view* in Xamarin.Forms denotes familiar types of presentation and inte
## Layouts
![layouts](https://d585tldpucybw.cloudfront.net/sfimages/default-source/blogs/2020/2020-11/layouts.png)
![layouts](../../.images/dotnet_xamarin-layouts.png)
- StackLayout: Organizes views linearly, either horizontally or vertically.
- AbsoluteLayout: Organizes views by setting coordinates & size in terms of absolute values or ratios.