Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
c42fa9c1
Commit
c42fa9c1
authored
Aug 15, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use option pattern for database context in program.cs
parent
12ac1d8a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
1 deletion
+76
-1
DatabaseOptions.cs
Clinics.Backend/API/Options/Database/DatabaseOptions.cs
+10
-0
DatabaseOptionsSetup.cs
Clinics.Backend/API/Options/Database/DatabaseOptionsSetup.cs
+27
-0
Program.cs
Clinics.Backend/API/Program.cs
+28
-0
appsettings.json
Clinics.Backend/API/appsettings.json
+11
-1
No files found.
Clinics.Backend/API/Options/Database/DatabaseOptions.cs
0 → 100644
View file @
c42fa9c1
namespace
API.Options.Database
;
public
class
DatabaseOptions
{
// Must match keys in appsettings.json
public
string
ConnectionString
{
get
;
set
;
}
=
string
.
Empty
;
public
int
MaxRetryCount
{
get
;
set
;
}
public
int
CommandTimeout
{
get
;
set
;
}
public
bool
EnableDetailedErrors
{
get
;
set
;
}
}
Clinics.Backend/API/Options/Database/DatabaseOptionsSetup.cs
0 → 100644
View file @
c42fa9c1
using
Microsoft.Extensions.Options
;
namespace
API.Options.Database
;
public
class
DatabaseOptionsSetup
:
IConfigureOptions
<
DatabaseOptions
>
{
private
const
string
_cofigurationSectionName
=
"DatabaseOptions"
;
// From appsettings.json
#
region
Using
ctor
DI
to
access
configuration
private
readonly
IConfiguration
_configuration
;
public
DatabaseOptionsSetup
(
IConfiguration
configuration
)
{
_configuration
=
configuration
;
}
#
endregion
public
void
Configure
(
DatabaseOptions
options
)
{
var
connectionString
=
_configuration
.
GetConnectionString
(
"DefaultConnection"
);
options
.
ConnectionString
=
connectionString
!;
// Not null
// Get string values from section, and parsing them
_configuration
.
GetSection
(
_cofigurationSectionName
).
Bind
(
options
);
}
}
Clinics.Backend/API/Program.cs
View file @
c42fa9c1
using
API.Options.Database
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.Options
;
using
Persistence.Context
;
var
builder
=
WebApplication
.
CreateBuilder
(
args
);
#region Add Database context
// First, get database options
builder
.
Services
.
ConfigureOptions
<
DatabaseOptionsSetup
>();
builder
.
Services
.
AddDbContext
<
ClinicsDbContext
>(
(
serviceProvider
,
dbContectOptionsBuilder
)
=>
{
// Now, get options just like any other service
var
databaseOptions
=
serviceProvider
.
GetService
<
IOptions
<
DatabaseOptions
>>()!.
Value
;
dbContectOptionsBuilder
.
UseSqlServer
(
databaseOptions
.
ConnectionString
,
sqlServerAction
=>
{
sqlServerAction
.
EnableRetryOnFailure
(
databaseOptions
.
MaxRetryCount
);
sqlServerAction
.
CommandTimeout
(
databaseOptions
.
CommandTimeout
);
});
// Be careful with this option, true only in development process!
dbContectOptionsBuilder
.
EnableDetailedErrors
(
databaseOptions
.
EnableDetailedErrors
);
});
#endregion
// Add services to the container.
#region Link controllers with presentation layer
...
...
Clinics.Backend/API/appsettings.json
View file @
c42fa9c1
{
"ConnectionStrings"
:
{
"DefaultConnection"
:
"server=.
\\
;database=Clinics;Trusted_Connection=True; Encrypt=False;MultipleActiveResultSets=true"
},
"Logging"
:
{
"LogLevel"
:
{
"Default"
:
"Information"
,
"Microsoft.AspNetCore"
:
"Warning"
}
},
"AllowedHosts"
:
"*"
"AllowedHosts"
:
"*"
,
"DatabaseOptions"
:
{
"MaxRetryCount"
:
3
,
"CommandTimeout"
:
30
,
"EnableDetailedErrors"
:
true
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment