149 lines
3.7 KiB
Markdown
149 lines
3.7 KiB
Markdown
# xCommons
|
|
|
|
it is a Part of xDashboard on SaherElm IT Center which provides:
|
|
|
|
- Common Configuration Models.
|
|
- Common Usefull extra Extensions.
|
|
- Data Validation Provider.
|
|
- Provided features such as Enums, Models, Attributes, Filters, Authorization Policies, Middlewares and Helpers.
|
|
- etc.
|
|
|
|
this module has no dependency and itself uses as a base dependency for all XProject modules/
|
|
|
|
for configure and use this Module refer to DI.XDIHelperExtension.cs file.
|
|
|
|
## Implementing
|
|
|
|
after adding this package as a dependency to your project you can do following:
|
|
|
|
### 1. Register it on DI
|
|
|
|
```c#
|
|
public void ConfigureServices (IServiceCollection services) {
|
|
...
|
|
//
|
|
// Register Validation Provider and all XCommons Module Services ...
|
|
services.AddXCommons ();
|
|
...
|
|
}
|
|
```
|
|
|
|
this will provide XValidationHelper service which accessible through DI.
|
|
|
|
### 2. Register Application Configuration on DI
|
|
|
|
since many of features in this Framework works by accessing the configuration of class application. you had to register it on DI.
|
|
|
|
configure you app in **appSettings.json**:
|
|
|
|
```javascript
|
|
{
|
|
...
|
|
"Version": "0.1",
|
|
"Name": "SaherElm Sample App",
|
|
"DefaultLanguage": "fa-IR",
|
|
"XPoweredValue": "SaherElmITCenter",
|
|
"WelcomeMessage": "Welcome to Application",
|
|
"AllowedOrigins": [
|
|
"http://localhost:4200",
|
|
"https://localhost:4200"
|
|
],
|
|
...
|
|
},
|
|
```
|
|
|
|
then register your configuration on DI in **Startup** file:
|
|
|
|
```c#
|
|
public void ConfigureServices (IServiceCollection services) {
|
|
...
|
|
//
|
|
// Register App Configuration ...
|
|
services.AddXAppConfiguration (Configuration);
|
|
var appConfiguration = services.GetRegisteredService<XAppConfiguration> ();
|
|
...
|
|
}
|
|
```
|
|
|
|
### 3. Register Available Cross Origin Resource sharing (Cors)
|
|
|
|
for determining which addresses can access your application and enabling core mechanism you can simply do it by follow this structure ...
|
|
|
|
**NOTE**: notice that you have configured allowed origins in **appSettings.json** before on AppConfiguration section.
|
|
|
|
```c#
|
|
public void ConfigureServices (IServiceCollection services) {
|
|
...
|
|
//
|
|
// Register Allowed Origins ...
|
|
services.AddXCors (appConfiguration.AllowedOrigins);
|
|
...
|
|
}
|
|
```
|
|
|
|
the you can enable cors handling like this:
|
|
|
|
```c#
|
|
public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {
|
|
...
|
|
//
|
|
// Using Cors ...
|
|
app.UseXCors ();
|
|
...
|
|
}
|
|
```
|
|
|
|
**Note**: remember **AllowedOrigins** can provided using **appSettings.json** file dynamically;
|
|
|
|
### 4. Enable Swagger
|
|
|
|
also you can add swagger documentation to your project:
|
|
|
|
```c#
|
|
public void ConfigureServices (IServiceCollection services) {
|
|
...
|
|
//
|
|
// Register Api Versioning ...
|
|
services.AddApiVersioning (opt => {
|
|
//
|
|
// Set Default Api Version ...
|
|
opt.DefaultApiVersion = new ApiVersion (1, 0);
|
|
|
|
//
|
|
// Set Routing to Default API Version, if Version unspecified ...
|
|
opt.AssumeDefaultVersionWhenUnspecified = true;
|
|
|
|
//
|
|
// Report All Available Api Versions on Response ...
|
|
opt.ReportApiVersions = true;
|
|
});
|
|
|
|
//
|
|
// Register Swagger ...
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine (AppContext.BaseDirectory, xmlFile);
|
|
services.AddXSwagger (Configuration, xmlFilePath : xmlPath);
|
|
...
|
|
}
|
|
```
|
|
|
|
the you can enable swagger middleware like this:
|
|
|
|
```c#
|
|
public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {
|
|
...
|
|
//
|
|
// Use Swagger Middleware ...
|
|
app.UseXSwagger ();
|
|
...
|
|
}
|
|
```
|
|
|
|
## Maintainer
|
|
|
|
Hadi Khazaee asl
|
|
|
|
[https://www.saherelm.ir](https://www.saherelm.ir)
|
|
|
|
[hadi_khazaee_asl@yahoo.com](mailto:hadi_khazaee_asl@yahoo.com)
|