Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit e064216

Browse files
committed
Aggiornata documentazione
1 parent c1d9600 commit e064216

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# DBContext generic methods
2+
3+
## Example entity
4+
```csharp
5+
public class MyEntity : IEntity<int>
6+
{
7+
public int Id { get; set; }
8+
public string Name { get; set; }
9+
public string Description { get; set; }
10+
}
11+
```
12+
13+
## Example interface
14+
```csharp
15+
public interface IMyService
16+
{
17+
Task<List<MyEntity>> GetListItemAsync();
18+
Task<MyEntity> GetItemAsync(int id);
19+
Task CreateItemAsync(MyEntity item);
20+
Task UpdateItemAsync(MyEntity item);
21+
Task DeleteItemAsync(MyEntity item);
22+
}
23+
```
24+
25+
## Example class
26+
```csharp
27+
public class MyService : IMyService
28+
{
29+
private readonly IUnitOfWork<MyEntity, int> unitOfWork;
30+
31+
public MyService(IUnitOfWork<MyEntity, int> unitOfWork)
32+
{
33+
this.unitOfWork = unitOfWork;
34+
}
35+
36+
public async Task<List<MyEntity>> GetListItemAsync()
37+
{
38+
var listItem = await unitOfWork.ReadOnly.GetAllAsync();
39+
return listItem;
40+
}
41+
42+
public async Task<MyEntity> GetItemAsync(int id)
43+
{
44+
var item = await unitOfWork.ReadOnly.GetByIdAsync(id);
45+
return item;
46+
}
47+
48+
public async Task CreateItemAsync(MyEntity item)
49+
{
50+
await unitOfWork.Command.CreateAsync(item);
51+
}
52+
53+
public async Task UpdateItemAsync(MyEntity item)
54+
{
55+
await unitOfWork.Command.UpdateAsync(item);
56+
}
57+
58+
public async Task DeleteItemAsync(MyEntity item)
59+
{
60+
await unitOfWork.Command.DeleteAsync(item);
61+
}
62+
}
63+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# DBContext Pool registration for different databases
2+
3+
## Registering services at Startup
4+
```csharp
5+
public void ConfigureServices(IServiceCollection services)
6+
{
7+
//numbers of retries to connect to the database
8+
int retryOnFailure = 3; // or zero if you don't want to retry
9+
10+
//default assembly migrations (in this case the migrations will be created in the DbContext assembly)
11+
string migrationsAssembly = string.Empty;
12+
13+
//or you can customize the assembly for migrations using the syntax
14+
string migrationsAssembly = "MyAssembly.Migrations.MySQLDatabase";
15+
16+
services.AddDbContextServicesGenerics<MyDbContext>();
17+
18+
//if you use MySQL database
19+
services.AddDbContextForMySql(connectionString, retryOnFailure, migrationsAssembly);
20+
21+
//if you use PostgreSQL database
22+
services.AddDbContextForPostgres(connectionString, retryOnFailure, migrationsAssembly);
23+
24+
//if you use SQLServer database
25+
services.AddDbContextForSQLServer(connectionString, retryOnFailure, migrationsAssembly);
26+
27+
//if you use SQLite database but in this case the retryOnFailure is not necessary as SQLite is not subject to transient errors
28+
services.AddDbContextForSQLite(connectionString, migrationsAssembly);
29+
}
30+
```
31+
32+
## Connection strings of the databases
33+
```json
34+
// for database MySQL / MariaDB
35+
"ConnectionStrings": {
36+
"Default": "Server=[SERVER];Database=[DATABASE];Uid=[USERNAME];Pwd=[PASSWORD];Port=3306"
37+
}
38+
39+
//for database PostgreSQL
40+
"ConnectionStrings": {
41+
"Default": "Host=[SERVER];Port=5432;Database=[DATABASE];Username=[USERNAME];Password=[PASSWORD]"
42+
}
43+
44+
//for database SQLServer
45+
"ConnectionStrings": {
46+
"Default": "Data Source=[SERVER];Initial Catalog=[DATABASE];User ID=[USERNAME];Password=[PASSWORD]"
47+
//or "Default": "Data Source=[SERVER];Initial Catalog=[DATABASE];User ID=[USERNAME];Password=[PASSWORD];Encrypt=False"
48+
}
49+
50+
//for database SQLite
51+
"ConnectionStrings": {
52+
"Default": "Data Source=Data/MyDatabase.db"
53+
}
54+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# DBContext registration for databases SQL server
2+
3+
## Registering services at Startup
4+
```csharp
5+
public void ConfigureServices(IServiceCollection services)
6+
{
7+
//default assembly migrations (in this case the migrations will be created in the DbContext assembly)
8+
string migrationsAssembly = string.Empty;
9+
10+
//or you can customize the assembly for migrations using the syntax
11+
string migrationsAssembly = "MyAssembly.Migrations.MySQLDatabase";
12+
13+
services.AddDbContextServicesGenerics<MyDbContext>();
14+
services.AddDbContextNoPoolSQLServer(connectionString, migrationsAssembly);
15+
}
16+
```
17+
18+
## Connection strings of the databases
19+
```json
20+
//for database SQLServer
21+
"ConnectionStrings": {
22+
"Default": "Data Source=[SERVER];Initial Catalog=[DATABASE];User ID=[USERNAME];Password=[PASSWORD]"
23+
//or "Default": "Data Source=[SERVER];Initial Catalog=[DATABASE];User ID=[USERNAME];Password=[PASSWORD];Encrypt=False"
24+
}
25+
```

0 commit comments

Comments
 (0)