mirror of https://github.com/abpframework/abp.git
committed by
GitHub
4229 changed files with 982155 additions and 275737 deletions
@ -0,0 +1,4 @@ |
|||
ui-angular: |
|||
- npm/ng-packs/**/* |
|||
- templates/app/angular/**/* |
|||
- templates/module/angular/**/* |
|||
@ -0,0 +1,24 @@ |
|||
name: 'Angular' |
|||
on: |
|||
pull_request: |
|||
paths: |
|||
- 'npm/ng-packs/**' |
|||
branches: |
|||
#- master |
|||
- dev |
|||
push: |
|||
paths: |
|||
- 'npm/ng-packs/**' |
|||
branches: |
|||
#- master |
|||
- dev |
|||
jobs: |
|||
build-test-lint: |
|||
runs-on: ubuntu-18.04 |
|||
steps: |
|||
- uses: actions/checkout@v1 |
|||
- uses: actions/setup-node@v1 |
|||
with: |
|||
node-version: '10.x' |
|||
- run: yarn && yarn ci |
|||
working-directory: npm/ng-packs |
|||
@ -0,0 +1,17 @@ |
|||
name: "Pull Request Labeler" |
|||
on: |
|||
pull_request: |
|||
paths: |
|||
- npm/ng-packs/**/* |
|||
- templates/app/angular/**/* |
|||
- templates/module/angular/**/* |
|||
branches: |
|||
- master |
|||
- dev |
|||
jobs: |
|||
labeler: |
|||
runs-on: ubuntu-18.04 |
|||
steps: |
|||
- uses: actions/labeler@v2 |
|||
with: |
|||
repo-token: "${{ secrets.GITHUB_TOKEN }}" |
|||
@ -1,60 +0,0 @@ |
|||
language: csharp |
|||
mono: none |
|||
dotnet: 2.1.300 |
|||
script: |
|||
- dotnet build framework |
|||
- dotnet build modules/users |
|||
- dotnet build modules/permission-management |
|||
- dotnet build modules/setting-management |
|||
- dotnet build modules/identity |
|||
- dotnet build modules/tenant-management |
|||
- dotnet build modules/account |
|||
- dotnet build modules/docs |
|||
- dotnet build modules/blogging |
|||
- dotnet test framework/test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests |
|||
- dotnet test framework/test/Volo.Abp.AspNetCore.MultiTenancy.Tests |
|||
- dotnet test framework/test/Volo.Abp.AspNetCore.Mvc.Tests |
|||
- dotnet test framework/test/Volo.Abp.AspNetCore.Mvc.UI.Tests |
|||
- dotnet test framework/test/Volo.Abp.AspNetCore.Mvc.Versioning.Tests |
|||
- dotnet test framework/test/Volo.Abp.AspNetCore.Tests |
|||
- dotnet test framework/test/Volo.Abp.Auditing.Tests |
|||
- dotnet test framework/test/Volo.Abp.Authorization.Tests |
|||
- dotnet test framework/test/Volo.Abp.Autofac.Tests |
|||
- dotnet test framework/test/Volo.Abp.AutoMapper.Tests |
|||
- dotnet test framework/test/Volo.Abp.Caching.Tests |
|||
- dotnet test framework/test/Volo.Abp.Castle.Core.Tests |
|||
- dotnet test framework/test/Volo.Abp.Core.Tests |
|||
- dotnet test framework/test/Volo.Abp.Data.Tests |
|||
- dotnet test framework/test/Volo.Abp.Ddd.Tests |
|||
- dotnet test framework/test/Volo.Abp.EntityFrameworkCore.Tests |
|||
- dotnet test framework/test/Volo.Abp.EventBus.Distributed.Tests |
|||
- dotnet test framework/test/Volo.Abp.EventBus.Tests |
|||
- dotnet test framework/test/Volo.Abp.Http.Client.Tests |
|||
- dotnet test framework/test/Volo.Abp.Localization.Tests |
|||
- dotnet test framework/test/Volo.Abp.MemoryDb.Tests |
|||
- dotnet test framework/test/Volo.Abp.MongoDB.Tests |
|||
- dotnet test framework/test/Volo.Abp.MultiTenancy.Tests |
|||
- dotnet test framework/test/Volo.Abp.Serialization.Tests |
|||
- dotnet test framework/test/Volo.Abp.TestApp.Tests |
|||
- dotnet test framework/test/Volo.Abp.UI.Navigation.Tests |
|||
- dotnet test framework/test/Volo.Abp.Uow.Tests |
|||
- dotnet test framework/test/Volo.Abp.Validation.Tests |
|||
- dotnet test framework/test/Volo.Abp.VirtualFileSystem.Tests |
|||
- dotnet test modules/blogging/test/Volo.Blogging.Application.Tests |
|||
- dotnet test modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests |
|||
- dotnet test modules/identity/test/Volo.Abp.Identity.Domain.Tests |
|||
- dotnet test modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests |
|||
- dotnet test modules/identity/test/Volo.Abp.Identity.MongoDB.Tests |
|||
- dotnet test modules/identity/test/Volo.Abp.Identity.Application.Tests |
|||
- dotnet test modules/permission-management/test/Volo.Abp.PermissionManagement.Tests |
|||
- dotnet test modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests |
|||
- dotnet test modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests |
|||
- dotnet test modules/setting-management/test/Volo.Abp.SettingManagement.EntityFrameworkCore.Tests |
|||
- dotnet test modules/setting-management/test/Volo.Abp.SettingManagement.MongoDB.Tests |
|||
- dotnet test modules/setting-management/test/Volo.Abp.SettingManagement.Tests |
|||
- dotnet test modules/tenant-management/test/Volo.Abp.TenantManagement.EntityFrameworkCore.Tests |
|||
- dotnet test modules/tenant-management/test/Volo.Abp.TenantManagement.MongoDB.Tests |
|||
- dotnet test modules/tenant-management/test/Volo.Abp.TenantManagement.Application.Tests |
|||
- dotnet test modules/users/test/Volo.Abp.Users.MongoDB.Tests |
|||
- dotnet test modules/users/test/Volo.Abp.Users.EntityFrameworkCore.Tests |
|||
|
|||
@ -0,0 +1,40 @@ |
|||
# COMMON PATHS |
|||
|
|||
$rootFolder = (Get-Item -Path "./" -Verbose).FullName |
|||
|
|||
# List of solutions |
|||
|
|||
$solutionPaths = ( |
|||
"framework", |
|||
"modules/users", |
|||
"modules/permission-management", |
|||
"modules/setting-management", |
|||
"modules/feature-management", |
|||
"modules/identity", |
|||
"modules/identityserver", |
|||
"modules/tenant-management", |
|||
"modules/account", |
|||
"modules/docs", |
|||
"modules/blogging", |
|||
"modules/audit-logging", |
|||
"modules/background-jobs", |
|||
"modules/client-simulation", |
|||
"templates/module/aspnet-core", |
|||
"templates/app/aspnet-core", |
|||
"abp_io/AbpIoLocalization" |
|||
) |
|||
|
|||
# Build all solutions |
|||
|
|||
foreach ($solutionPath in $solutionPaths) { |
|||
$solutionAbsPath = (Join-Path $rootFolder $solutionPath) |
|||
Set-Location $solutionAbsPath |
|||
dotnet build --configuration Release |
|||
if (-Not $?) { |
|||
Write-Host ("Build failed for the solution: " + $solutionPath) |
|||
Set-Location $rootFolder |
|||
exit $LASTEXITCODE |
|||
} |
|||
} |
|||
|
|||
Set-Location $rootFolder |
|||
@ -0,0 +1,3 @@ |
|||
# Configuration |
|||
|
|||
ASP.NET Core has an flexible and extensible key-value based configuration system. In fact, the configuration system is a part of Microsoft.Extensions libraries and it is independent from ASP.NET Core. That means it can be used in any type of application. See [Microsoft's documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/) to learn the configuration infrastructure. ABP framework is 100% compatible with the configuration system. |
|||
@ -0,0 +1,3 @@ |
|||
# Current User |
|||
|
|||
TODO! |
|||
@ -0,0 +1,11 @@ |
|||
# Data Access |
|||
|
|||
ABP framework was designed as database agnostic, it can work any type of data source by the help of the [repository](Repositories.md) and [unit of work](Unit-Of-Work.md) abstractions. |
|||
|
|||
However, currently the following providers are implements: |
|||
|
|||
* [Entity Framework Core](Entity-Framework-Core.md) (works with [various DBMS and providers](https://docs.microsoft.com/en-us/ef/core/providers/?tabs=dotnet-core-cli).) |
|||
* [MongoDB](MongoDB.md) |
|||
* [Dapper](Dapper.md) |
|||
|
|||
More providers might be added in the next releases. |
|||
@ -1,3 +1,222 @@ |
|||
# Data Filtering |
|||
|
|||
TODO |
|||
[Volo.Abp.Data](https://www.nuget.org/packages/Volo.Abp.Data) package defines services to automatically filter data on querying from a database. |
|||
|
|||
## Pre-Defined Filters |
|||
|
|||
ABP defines some filters out of the box. |
|||
|
|||
### ISoftDelete |
|||
|
|||
Used to mark an [entity](Entities.md) as deleted instead of actually deleting it. Implement the `ISoftDelete` interface to make your entity "soft delete". |
|||
|
|||
Example: |
|||
|
|||
````csharp |
|||
using System; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Acme.BookStore |
|||
{ |
|||
public class Book : AggregateRoot<Guid>, ISoftDelete |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsDeleted { get; set; } //Defined by ISoftDelete |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`ISoftDelete` defines the `IsDeleted` property. When you delete a book using [repositories](Repositories.md), ABP automatically sets `IsDeleted` to true and protects it from actual deletion (you can also manually set the `IsDeleted` property to true if you need). In addition, it **automatically filters deleted entities** when you query the database. |
|||
|
|||
> `ISoftDelete` filter is enabled by default and you can not get deleted entities from database unless you explicitly disable it. See the `IDataFilter` service below. |
|||
|
|||
### IMultiTenant |
|||
|
|||
[Multi-tenancy](Multi-Tenancy.md) is an efficient way of creating SaaS applications. Once you create a multi-tenant application, you typically want to isolate data between tenants. Implement `IMultiTenant` interface to make your entity "multi-tenant aware". |
|||
|
|||
Example: |
|||
|
|||
````csharp |
|||
using System; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Acme.BookStore |
|||
{ |
|||
public class Book : AggregateRoot<Guid>, ISoftDelete, IMultiTenant |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsDeleted { get; set; } //Defined by ISoftDelete |
|||
|
|||
public Guid? TenantId { get; set; } //Defined by IMultiTenant |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`IMultiTenant` interface defines the `TenantId` property which is then used to automatically filter the entities for the current tenant. See the [Multi-tenancy](Multi-Tenancy.md) document for more. |
|||
|
|||
## IDataFilter Service: Enable/Disable Data Filters |
|||
|
|||
You can control the filters using `IDataFilter` service. |
|||
|
|||
Example: |
|||
|
|||
````csharp |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Acme.BookStore |
|||
{ |
|||
public class MyBookService : ITransientDependency |
|||
{ |
|||
private readonly IDataFilter _dataFilter; |
|||
private readonly IRepository<Book, Guid> _bookRepository; |
|||
|
|||
public MyBookService( |
|||
IDataFilter dataFilter, |
|||
IRepository<Book, Guid> bookRepository) |
|||
{ |
|||
_dataFilter = dataFilter; |
|||
_bookRepository = bookRepository; |
|||
} |
|||
|
|||
public async Task<List<Book>> GetAllBooksIncludingDeletedAsync() |
|||
{ |
|||
//Temporary disable the ISoftDelete filter |
|||
using (_dataFilter.Disable<ISoftDelete>()) |
|||
{ |
|||
return await _bookRepository.GetListAsync(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
```` |
|||
|
|||
* [Inject](Dependency-Injection.md) the `IDataFilter` service to your class. |
|||
* Use the `Disable` method within a `using` statement to create a code block where the `ISoftDelete` filter is disabled inside it (Always use it inside a `using` block to guarantee that the filter is reset to its previous state). |
|||
|
|||
`IDataFilter.Enable` method can be used to enable a filter. `Enable` and `Disable` methods can be used in a nested way to define inner scopes. |
|||
|
|||
## AbpDataFilterOptions |
|||
|
|||
`AbpDataFilterOptions` can be used to [set options](Options.md) for the data filter system. |
|||
|
|||
The example code below disables the `ISoftDelete` filter by default which will cause to include deleted entities when you query the database unless you explicitly enable the filter: |
|||
|
|||
````csharp |
|||
Configure<AbpDataFilterOptions>(options => |
|||
{ |
|||
options.DefaultStates[typeof(ISoftDelete)] = new DataFilterState(isEnabled: false); |
|||
}); |
|||
```` |
|||
|
|||
> Carefully change defaults for global filters, especially if you are using a pre-built module which might be developed assuming the soft delete filter is turned on by default. But you can do it for your own defined filters safely. |
|||
|
|||
## Defining Custom Filters |
|||
|
|||
Defining and implementing a new filter highly depends on the database provider. ABP implements all pre-defined filters for all database providers. |
|||
|
|||
When you need it, start by defining an interface (like `ISoftDelete` and `IMultiTenant`) for your filter and implement it for your entities. |
|||
|
|||
Example: |
|||
|
|||
````csharp |
|||
public interface IIsActive |
|||
{ |
|||
bool IsActive { get; } |
|||
} |
|||
```` |
|||
|
|||
Such an `IIsActive` interface can be used to filter active/passive data and can be easily implemented by any [entity](Entities.md): |
|||
|
|||
````csharp |
|||
public class Book : AggregateRoot<Guid>, IIsActive |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsActive { get; set; } //Defined by IIsActive |
|||
} |
|||
```` |
|||
|
|||
### EntityFramework Core |
|||
|
|||
ABP uses [EF Core's Global Query Filters](https://docs.microsoft.com/en-us/ef/core/querying/filters) system for the [EF Core Integration](Entity-Framework-Core.md). So, it is well integrated to EF Core and works as expected even if you directly work with `DbContext`. |
|||
|
|||
Best way to implement a custom filter is to override `CreateFilterExpression` method for your `DbContext`. Example: |
|||
|
|||
````csharp |
|||
protected bool IsActiveFilterEnabled => DataFilter?.IsEnabled<IIsActive>() ?? false; |
|||
|
|||
protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>() |
|||
{ |
|||
var expression = base.CreateFilterExpression<TEntity>(); |
|||
|
|||
if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity))) |
|||
{ |
|||
Expression<Func<TEntity, bool>> isActiveFilter = |
|||
e => !IsActiveFilterEnabled || EF.Property<bool>(e, "IsActive"); |
|||
expression = expression == null |
|||
? isActiveFilter |
|||
: CombineExpressions(expression, isActiveFilter); |
|||
} |
|||
|
|||
return expression; |
|||
} |
|||
```` |
|||
|
|||
* Added a `IsActiveFilterEnabled` property to check if `IIsActive` is enabled or not. It internally uses the `IDataFilter` service introduced before. |
|||
* Overrided the `CreateFilterExpression` method, checked if given entity implements the `IIsActive` interface and combines the expressions if necessary. |
|||
|
|||
### MongoDB |
|||
|
|||
ABP implements data filters directly in the [repository](Repositories.md) level for the [MongoDB Integration](MongoDB.md). So, it works only if you use the repositories properly. Otherwise, you should manually filter the data. |
|||
|
|||
Currently, the best way to implement a data filter for the MongoDB integration is to override the `AddGlobalFilters` in the repository derived from the `MongoDbRepository` class. Example: |
|||
|
|||
````csharp |
|||
public class BookRepository : MongoDbRepository<BookStoreMongoDbContext, Book, Guid> |
|||
{ |
|||
protected override void AddGlobalFilters(List<FilterDefinition<Book>> filters) |
|||
{ |
|||
if (DataFilter.IsEnabled<IIsActive>()) |
|||
{ |
|||
filters.Add(Builders<Book>.Filter.Eq(e => ((IIsActive)e).IsActive, true)); |
|||
} |
|||
} |
|||
} |
|||
```` |
|||
|
|||
This example implements it only for the `Book` entity. If you want to implement for all entities (those implement the `IIsActive` interface), create your own custom MongoDB repository base class and override the `AddGlobalFilters` as shown below: |
|||
|
|||
````csharp |
|||
public abstract class MyMongoRepository<TMongoDbContext, TEntity, TKey> : MongoDbRepository<TMongoDbContext, TEntity, TKey> |
|||
where TMongoDbContext : IAbpMongoDbContext |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
protected MyMongoRepository(IMongoDbContextProvider<TMongoDbContext> dbContextProvider) |
|||
: base(dbContextProvider) |
|||
{ |
|||
|
|||
} |
|||
|
|||
protected override void AddGlobalFilters(List<FilterDefinition<TEntity>> filters) |
|||
{ |
|||
if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity)) |
|||
&& DataFilter.IsEnabled<IIsActive>()) |
|||
{ |
|||
filters.Add(Builders<TEntity>.Filter.Eq(e => ((IIsActive)e).IsActive, true)); |
|||
} |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> See "Set Default Repository Classes" section of the [MongoDb Integration document](MongoDB.md) to learn how to replace default repository base with your custom class. |
|||
@ -0,0 +1,84 @@ |
|||
# Setting Management Module |
|||
|
|||
Setting Management Module implements the `ISettingStore` (see [the setting system](../Settings.md)) to store the setting values in a database and provides the `ISettingManager` to manage (change) the setting values in the database. |
|||
|
|||
> Setting Management module is already installed and configured for [the startup templates](../Startup-Templates/Index.md). So, most of the times you don't need to manually add this module to your application. |
|||
|
|||
## ISettingManager |
|||
|
|||
`ISettingManager` is used to get and set the values for the settings. Examples: |
|||
|
|||
````csharp |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.SettingManagement; |
|||
|
|||
namespace Demo |
|||
{ |
|||
public class MyService : ITransientDependency |
|||
{ |
|||
private readonly ISettingManager _settingManager; |
|||
|
|||
//Inject ISettingManager service |
|||
public MyService(ISettingManager settingManager) |
|||
{ |
|||
_settingManager = settingManager; |
|||
} |
|||
|
|||
public async Task FooAsync() |
|||
{ |
|||
Guid user1Id = ...; |
|||
Guid tenant1Id = ...; |
|||
|
|||
//Get/set a setting value for the current user or the specified user |
|||
|
|||
string layoutType1 = |
|||
await _settingManager.GetOrNullForCurrentUserAsync("App.UI.LayoutType"); |
|||
string layoutType2 = |
|||
await _settingManager.GetOrNullForUserAsync("App.UI.LayoutType", user1Id); |
|||
|
|||
await _settingManager.SetForCurrentUserAsync("App.UI.LayoutType", "LeftMenu"); |
|||
await _settingManager.SetForUserAsync(user1Id, "App.UI.LayoutType", "LeftMenu"); |
|||
|
|||
//Get/set a setting value for the current tenant or the specified tenant |
|||
|
|||
string layoutType3 = |
|||
await _settingManager.GetOrNullForCurrentTenantAsync("App.UI.LayoutType"); |
|||
string layoutType4 = |
|||
await _settingManager.GetOrNullForTenantAsync("App.UI.LayoutType", tenant1Id); |
|||
|
|||
await _settingManager.SetForCurrentTenantAsync("App.UI.LayoutType", "LeftMenu"); |
|||
await _settingManager.SetForTenantAsync(tenant1Id, "App.UI.LayoutType", "LeftMenu"); |
|||
|
|||
//Get/set a global and default setting value |
|||
|
|||
string layoutType5 = |
|||
await _settingManager.GetOrNullGlobalAsync("App.UI.LayoutType"); |
|||
string layoutType6 = |
|||
await _settingManager.GetOrNullDefaultAsync("App.UI.LayoutType"); |
|||
|
|||
await _settingManager.SetGlobalAsync("App.UI.LayoutType", "TopMenu"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
```` |
|||
|
|||
So, you can get or set a setting value for different setting value providers (Default, Global, User, Tenant... etc). |
|||
|
|||
### Setting Cache |
|||
|
|||
Setting values are cached using the [distributed cache](../Caching.md) system. Always use the `ISettingManager` to change the setting values which manages the cache for you. |
|||
|
|||
## Setting Management Providers |
|||
|
|||
Setting Management module is extensible, just like the [setting system](../Settings.md). You can extend it by defining setting management providers. There are 5 pre-built setting management providers registered by the order below: |
|||
|
|||
* `DefaultValueSettingManagementProvider`: Gets the value from the default value of the setting definition. It can not set the default value since default values are hard-coded on the setting definition. |
|||
* `ConfigurationSettingManagementProvider`: Gets the value from the [IConfiguration service](Configuration.md). It can not set the configuration value because it is not possible to change the configuration values on runtime. |
|||
* `GlobalSettingManagementProvider`: Gets or sets the global (system-wide) value for a setting. |
|||
* `TenantSettingManagementProvider`: Gets or sets the setting value for a tenant. |
|||
* `UserSettingManagementProvider`: Gets the setting value for a user. |
|||
|
|||
`ISettingManager` uses the setting management providers on get/set methods. Typically, every setting management provider defines extension methods on the `ISettingManagement` service (like `SetForUserAsync` defined by the user setting management provider). |
|||
@ -0,0 +1,4 @@ |
|||
# Options |
|||
|
|||
TODO! |
|||
|
|||
@ -1,3 +1,180 @@ |
|||
# Settings |
|||
|
|||
TODO! |
|||
[Configuration system](Configuration.md) is a good way to configure the application on startup. In addition to the configurations, ABP provides another way to set and get some application settings. |
|||
|
|||
A setting is a name-value pair stored in a dynamic data source, generally in a database. Setting system is extensible and there are pre-built provides for a user, a tenant, global and default. |
|||
|
|||
## Defining Settings |
|||
|
|||
A setting must be defined before its use. ABP was designed to be [modular](Module-Development-Basics.md), so different modules can have different settings. A module must create a class derived from the `SettingDefinitionProvider` in order to define its settings. An example setting definition provider is shown below: |
|||
|
|||
````csharp |
|||
public class EmailSettingProvider : SettingDefinitionProvider |
|||
{ |
|||
public override void Define(ISettingDefinitionContext context) |
|||
{ |
|||
context.Add( |
|||
new SettingDefinition("Smtp.Host", "127.0.0.1"), |
|||
new SettingDefinition("Smtp.Port", "25"), |
|||
new SettingDefinition("Smtp.UserName"), |
|||
new SettingDefinition("Smtp.Password", isEncrypted: true), |
|||
new SettingDefinition("Smtp.EnableSsl", "false") |
|||
); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
ABP automatically discovers this class and registers the setting definitions. |
|||
|
|||
### SettingDefinition |
|||
|
|||
`SettingDefinition` class has the following properties: |
|||
|
|||
* **Name**: Unique name of the setting in the application. This is **the only mandatory property**. Used to get/set the value of this setting in the application code (It's a good idea to define a const string for a setting name instead of using a magic string). |
|||
* **DefaultValue**: A setting may have a default value. |
|||
* **DisplayName**: A localizable string that can be used to show the setting name on the UI. |
|||
* **Description**: A localizable string that can be used to show the setting description on the UI. |
|||
* **IsVisibleToClients**: A boolean value indicates that whether this setting value is available in the client side or not. Default value is false to prevent accidently publishing an internal critical setting value. |
|||
* **IsInherited**: A boolean value indicates that whether this setting value is inherited from other providers or not. Default value is true and fallbacks to the next provider if the setting value was not set for the requested provider (see the setting value providers section for more). |
|||
* **IsEncrypted**: A boolean value indicates that whether this setting value should be encrypted on save and decrypted on read. It makes possible to secure the setting value in the database. |
|||
* **Providers**: Can be used to restrict providers available for a particular setting (see the setting value providers section for more). |
|||
* **Properties**: A name/value collection to set custom properties about this setting those can be used later in the application code. |
|||
|
|||
## Reading Setting Values |
|||
|
|||
### ISettingProvider |
|||
|
|||
`ISettingProvider` is used to get the value of a setting or get the values of all the settings. Example usages: |
|||
|
|||
````csharp |
|||
public class MyService |
|||
{ |
|||
private readonly ISettingProvider _settingProvider; |
|||
|
|||
//Inject ISettingProvider in the constructor |
|||
public MyService(ISettingProvider settingProvider) |
|||
{ |
|||
_settingProvider = settingProvider; |
|||
} |
|||
|
|||
public async Task FooAsync() |
|||
{ |
|||
//Get a value as string. |
|||
string userName = await _settingProvider.GetOrNullAsync("Smtp.UserName"); |
|||
|
|||
//Get a bool value and fallback to the default value (false) if not set. |
|||
bool enableSsl = await _settingProvider.GetAsync<bool>("Smtp.EnableSsl"); |
|||
|
|||
//Get a bool value and fallback to the provided default value (true) if not set. |
|||
bool enableSsl = await _settingProvider.GetAsync<bool>( |
|||
"Smtp.EnableSsl", defaultValue: true); |
|||
|
|||
//Get a bool value with the IsTrueAsync shortcut extension method |
|||
bool enableSsl = await _settingProvider.IsTrueAsync("Smtp.EnableSsl"); |
|||
|
|||
//Get an int value or the default value (0) if not set |
|||
int port = (await _settingProvider.GetAsync<int>("Smtp.Port")); |
|||
|
|||
//Get an int value or null if not provided |
|||
int? port = (await _settingProvider.GetOrNullAsync("Smtp.Port"))?.To<int>(); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> `ISettingProvider` is a very common service and some base classes (like `IApplicationService`) already property-inject it. You can directly use the `SettingProvider` in such cases. |
|||
|
|||
### Reading Setting Values on the Client Side |
|||
|
|||
If a setting is allowed to be visible on the client side, current value of the setting can also be read from the JavaScript code. Examples: |
|||
|
|||
````js |
|||
//Gets a value as string. |
|||
var language = abp.setting.get('Abp.Localization.DefaultLanguage'); |
|||
|
|||
//Gets an integer value. |
|||
var requiredLength = abp.setting.getInt('Abp.Identity.Password.RequiredLength'); |
|||
|
|||
//Gets a boolean value. |
|||
var requireDigit = abp.setting.getBoolean('Abp.Identity.Password.RequireDigit'); |
|||
```` |
|||
|
|||
In addition, use `abp.setting.values` to get a dictionary of all the setting values. |
|||
|
|||
## Setting Value Providers |
|||
|
|||
Setting system is extensible, you can extend it by defining setting value providers to get setting values from any source and based on any condition. |
|||
|
|||
`ISettingProvider` uses the setting value providers to obtain a setting value. It fallbacks to the next value provider if a value provider can not get the setting value. |
|||
|
|||
There are 5 pre-built setting value providers registered by the order below: |
|||
|
|||
* `DefaultValueSettingValueProvider`: Gets the value from the default value of the setting definition, if set (see the SettingDefinition section above). |
|||
* `ConfigurationSettingValueProvider`: Gets the value from the [IConfiguration service](Configuration.md). |
|||
* `GlobalSettingValueProvider`: Gets the global (system-wide) value for a setting, if set. |
|||
* `TenantSettingValueProvider`: Gets the setting value for the current tenant, if set (see the [multi-tenancy](Multi-Tenancy.md) document). |
|||
* `UserSettingValueProvider`: Gets the setting value for the current user, if set (see the [current user](CurrentUser.md) document). |
|||
|
|||
> Setting fallback system works from bottom (user) to top (default). |
|||
|
|||
Global, Tenant and User setting value providers use the `ISettingStore` to read the value from the data source (see the section below). |
|||
|
|||
### Custom Setting Value Providers |
|||
|
|||
If you need to extend the setting system, you can define a class derived from the `SettingValueProvider` class. Example: |
|||
|
|||
````csharp |
|||
public class CustomSettingValueProvider : SettingValueProvider |
|||
{ |
|||
public override string Name => "Custom"; |
|||
|
|||
public CustomSettingValueProvider(ISettingStore settingStore) |
|||
: base(settingStore) |
|||
{ |
|||
} |
|||
|
|||
public override Task<string> GetOrNullAsync(SettingDefinition setting) |
|||
{ |
|||
/* Return the setting value or null |
|||
Use the SettingStore or another data source */ |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> Alternatively, you can implement the `ISettingValueProvider` interface. Remember to register it to the [dependency injection](Dependency-Injection.md) in this case. |
|||
|
|||
Every provider should have a unique Name (which is "Custom" here). Built-in providers use the given names: |
|||
|
|||
* `DefaultValueSettingValueProvider`: "**D**". |
|||
* `ConfigurationSettingValueProvider`: "**C**". |
|||
* `GlobalSettingValueProvider`: "**G**". |
|||
* `TenantSettingValueProvider`: "**T**". |
|||
* `UserSettingValueProvider`: "**U**". |
|||
|
|||
One-letter names were preferred to reduce the data size in the database (provider name is repeated in each row). |
|||
|
|||
Once you define a custom setting value provider, you need to explicitly register it to the `AbpSettingOptions`: |
|||
|
|||
````csharp |
|||
Configure<AbpSettingOptions>(options => |
|||
{ |
|||
options.ValueProviders.Add<CustomSettingValueProvider>(); |
|||
}); |
|||
```` |
|||
|
|||
This example adds it as the last item, so it will be the first value provider used by the `ISettingProvider`. You could add it to another index in the `options.ValueProviders` list. |
|||
|
|||
### ISettingStore |
|||
|
|||
While a setting value provider is free to use any source to get the setting value, the`ISettingStore` service is the default source of the setting values. Global, Tenant and User setting value providers use it. |
|||
|
|||
## ISettingEncryptionService |
|||
|
|||
`ISettingEncryptionService` is used to encrypt/decrypt setting values when `IsEncrypted` property of a setting definition was set to `true`. |
|||
|
|||
You can replace this service in the dependency injection system to customize the encryption/decryption process. Default implementation uses the `StringEncryptionService` which is implemented with the AES algorithm by default (see string [encryption document](String-Encryption.md) for more). |
|||
|
|||
## Setting Management Module |
|||
|
|||
The core setting system is pretty independent and doesn't make any assumption about how you manage (change) the setting values. Even the default `ISettingStore` implementation is the `NullSettingStore` which returns null for all setting values. |
|||
|
|||
The setting management module completes it (and implements `ISettingStore`) by managing setting values in a database. See the [Setting Management Module document](Modules/Setting-Management.md) for more. |
|||
|
|||
@ -0,0 +1,4 @@ |
|||
# String Encryption |
|||
|
|||
TODO! |
|||
|
|||
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 13 KiB |
@ -0,0 +1,3 @@ |
|||
# 配置 |
|||
|
|||
ASP.NET Core 提供了一个灵活可扩展,基于键值的配置系统. 但是配置系统独立于ASP.NET Core是`Microsoft.Extensions` 类库的部分. 它可以用于任何类型的应用程序 . 参见 [微软文档](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/) 学习配置的更多内容. ABP 框架与配置系统百分百兼容. |
|||
@ -0,0 +1,3 @@ |
|||
# Current User |
|||
|
|||
TODO! |
|||
@ -0,0 +1,222 @@ |
|||
# 数据过滤 |
|||
|
|||
[Volo.Abp.Data](https://www.nuget.org/packages/Volo.Abp.Data) 包定义了在查询数据库时自动过滤数据的服务. |
|||
|
|||
## 预定义的过滤 |
|||
|
|||
ABP定义了一些开箱即用的过滤. |
|||
|
|||
### ISoftDelete |
|||
|
|||
将[实体](Entities.md)标记为已删除,并不是物理删除. 实现 `ISoftDelete` 接口将你的实体"软删除". |
|||
|
|||
示例: |
|||
|
|||
````csharp |
|||
using System; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Acme.BookStore |
|||
{ |
|||
public class Book : AggregateRoot<Guid>, ISoftDelete |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsDeleted { get; set; } //Defined by ISoftDelete |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`ISoftDelete` 定义了 `IsDeleted` 属性. 当你使用[仓储](Repositories.md)删除一条记录时, ABP会自动将 `IsDeleted` 设置为true,并将删除操作替换为修改操作(如果需要,也可以手动将 `IsDeleted` 设置为true). 在查询数据库时会**自动过滤软删除的实体**. |
|||
|
|||
> `ISoftDelete` 过滤默认启用, 想要真正的从数据库删除实体需要显示的禁用过滤. 参见下面提到的 `IDataFilter` 服务. |
|||
|
|||
### IMultiTenant |
|||
|
|||
[多租户](Multi-Tenancy.md) 是创建 SaaS 应用程序的有效方法. 多租户应用程序通常需要在租户间隔离数据. 实现 `IMultiTenant` 接口使你的实体支持 "多租户". |
|||
|
|||
示例: |
|||
|
|||
````csharp |
|||
using System; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Acme.BookStore |
|||
{ |
|||
public class Book : AggregateRoot<Guid>, ISoftDelete, IMultiTenant |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsDeleted { get; set; } //Defined by ISoftDelete |
|||
|
|||
public Guid? TenantId { get; set; } //Defined by IMultiTenant |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`IMultiTenant` 接口定义了 `TenantId` 属性用于自动过滤当前租户实体. 更多信息参见[多租户](Multi-Tenancy.md)文档. |
|||
|
|||
## IDataFilter 服务: 启用/禁用 数据过滤 |
|||
|
|||
你可以使用 `IDataFilter` 服务控制数据过滤. |
|||
|
|||
示例: |
|||
|
|||
````csharp |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Acme.BookStore |
|||
{ |
|||
public class MyBookService : ITransientDependency |
|||
{ |
|||
private readonly IDataFilter _dataFilter; |
|||
private readonly IRepository<Book, Guid> _bookRepository; |
|||
|
|||
public MyBookService( |
|||
IDataFilter dataFilter, |
|||
IRepository<Book, Guid> bookRepository) |
|||
{ |
|||
_dataFilter = dataFilter; |
|||
_bookRepository = bookRepository; |
|||
} |
|||
|
|||
public async Task<List<Book>> GetAllBooksIncludingDeletedAsync() |
|||
{ |
|||
//Temporary disable the ISoftDelete filter |
|||
using (_dataFilter.Disable<ISoftDelete>()) |
|||
{ |
|||
return await _bookRepository.GetListAsync(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
```` |
|||
|
|||
* [注入](Dependency-Injection.md) `IDataFilter` 服务到你的类中. |
|||
* 在 `using` 语句中使用 `Disable` 方法创建一个代码块,其中禁用了 `ISoftDelete` 过滤器(始终与 `using` 搭配使用,确保代码块执行后将过滤重置为之前的状态). |
|||
|
|||
`IDataFilter.Enable` 方法可以启用过滤. 可以嵌套使用 `Enable` 和 `Disable` 方法定义内部作用域. |
|||
|
|||
## AbpDataFilterOptions |
|||
|
|||
`AbpDataFilterOptions` 用于设置数据过滤系统[选项](Options.md). |
|||
|
|||
下面的示例代码在默认情况下禁用了 `ISoftDelete` 过滤,除非显示启用,在查询数据库时会包含标记为已删除的实体: |
|||
|
|||
````csharp |
|||
Configure<AbpDataFilterOptions>(options => |
|||
{ |
|||
options.DefaultStates[typeof(ISoftDelete)] = new DataFilterState(isEnabled: false); |
|||
}); |
|||
```` |
|||
|
|||
> 更改全局过滤的默认值需要小心,特别是在你使用预构建的模块时该模块可能是在默认启用软删除过滤的情况下开发的. 但你可以安全的为自己定义的数据过滤执行此操作. |
|||
|
|||
## 自定义数据过滤 |
|||
|
|||
定义和实现新的过滤很大程序上取决与数据库提供者. ABP为所有的数据库提供者实现了预构建的过滤. |
|||
|
|||
首先为过滤定义一个接口 (如 `ISoftDelete` 和 `IMultiTenant`) 然后用实体实现它. |
|||
|
|||
示例: |
|||
|
|||
````csharp |
|||
public interface IIsActive |
|||
{ |
|||
bool IsActive { get; } |
|||
} |
|||
```` |
|||
|
|||
`IIsActive` 接口可以过滤活跃/消极数据,任何[实体](Entities.md)都可以实现它: |
|||
|
|||
````csharp |
|||
public class Book : AggregateRoot<Guid>, IIsActive |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsActive { get; set; } //Defined by IIsActive |
|||
} |
|||
```` |
|||
|
|||
### EntityFramework Core |
|||
|
|||
ABP使用[EF Core的全局过滤](https://docs.microsoft.com/en-us/ef/core/querying/filters)系统用于[EF Core 集成](Entity-Framework-Core.md). 所以它很好的集成到EF Core中,即使你直接使用 `DbContext` 它也可以正常工作. |
|||
|
|||
实现自定义过滤的最佳方法是为重写你的 `DbContext` 的 `CreateFilterExpression` 方法. 示例: |
|||
|
|||
````csharp |
|||
protected bool IsActiveFilterEnabled => DataFilter?.IsEnabled<IIsActive>() ?? false; |
|||
|
|||
protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>() |
|||
{ |
|||
var expression = base.CreateFilterExpression<TEntity>(); |
|||
|
|||
if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity))) |
|||
{ |
|||
Expression<Func<TEntity, bool>> isActiveFilter = |
|||
e => !IsActiveFilterEnabled || EF.Property<bool>(e, "IsActive"); |
|||
expression = expression == null |
|||
? isActiveFilter |
|||
: CombineExpressions(expression, isActiveFilter); |
|||
} |
|||
|
|||
return expression; |
|||
} |
|||
```` |
|||
|
|||
* 添加 `IsActiveFilterEnabled` 属性用于检查是否启用了 `IIsActive` . 内部使用了之前介绍到的 `IDataFilter` 服务. |
|||
* 重写 `CreateFilterExpression` 方法检查给定实体是否实现 `IIsActive` 接口,在必要时组合表达式. |
|||
|
|||
### MongoDB |
|||
|
|||
ABP直接在[仓储](Repositories.md)级别实现数据过滤用于[MongoDB 集成](MongoDB.md). 所以只有正确的使用仓储,它才会工作. 否则你需要手动过滤数据. |
|||
|
|||
目前为MongoDB集成实现数据过滤的最佳方法是重写派生自 `MongoDbRepository` 仓储的 `AddGlobalFilters` 方法: |
|||
|
|||
````csharp |
|||
public class BookRepository : MongoDbRepository<BookStoreMongoDbContext, Book, Guid> |
|||
{ |
|||
protected override void AddGlobalFilters(List<FilterDefinition<Book>> filters) |
|||
{ |
|||
if (DataFilter.IsEnabled<IIsActive>()) |
|||
{ |
|||
filters.Add(Builders<Book>.Filter.Eq(e => ((IIsActive)e).IsActive, true)); |
|||
} |
|||
} |
|||
} |
|||
```` |
|||
|
|||
示例中仅为 `Book` 实体实现了过滤. 如果你想要为所有的实体实现过滤 (实现了 `IIsActive` 接口的实体),可以创建自己的MongoDB仓储基类并重 写 `AddGlobalFilters` 方法. 如下所示: |
|||
|
|||
````csharp |
|||
public abstract class MyMongoRepository<TMongoDbContext, TEntity, TKey> : MongoDbRepository<TMongoDbContext, TEntity, TKey> |
|||
where TMongoDbContext : IAbpMongoDbContext |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
protected MyMongoRepository(IMongoDbContextProvider<TMongoDbContext> dbContextProvider) |
|||
: base(dbContextProvider) |
|||
{ |
|||
|
|||
} |
|||
|
|||
protected override void AddGlobalFilters(List<FilterDefinition<TEntity>> filters) |
|||
{ |
|||
if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity)) |
|||
&& DataFilter.IsEnabled<IIsActive>()) |
|||
{ |
|||
filters.Add(Builders<TEntity>.Filter.Eq(e => ((IIsActive)e).IsActive, true)); |
|||
} |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> 参阅[MongoDb集成文档](MongoDB.md)的 "设置默认仓储" 部分了解如何使用自定义类替换默认仓储. |
|||
@ -0,0 +1,3 @@ |
|||
# Setting Management Module |
|||
|
|||
TODO |
|||
@ -1,3 +1,255 @@ |
|||
## Object To Object Mapping |
|||
# 对象到对象映射 |
|||
|
|||
TODO |
|||
将对象映射到另一个对象是常用并且繁琐重复的工作,大部分情况下两个类都具有相同或相似的属性. 例如下面的 [应用服务](Application-Services.md)方法: |
|||
|
|||
```csharp |
|||
public class UserAppService : ApplicationService |
|||
{ |
|||
private readonly IRepository<User, Guid> _userRepository; |
|||
|
|||
public UserAppService(IRepository<User, Guid> userRepository) |
|||
{ |
|||
_userRepository = userRepository; |
|||
} |
|||
|
|||
public void CreateUser(CreateUserInput input) |
|||
{ |
|||
//Manually creating a User object from the CreateUserInput object |
|||
var user = new User |
|||
{ |
|||
Name = input.Name, |
|||
Surname = input.Surname, |
|||
EmailAddress = input.EmailAddress, |
|||
Password = input.Password |
|||
}; |
|||
|
|||
_userRepository.Insert(user); |
|||
} |
|||
} |
|||
``` |
|||
|
|||
`CreateUserInput` 和 `User` 是一个简单的[DTO](Data-Transfer-Objects.md)和[实体](Entities.md)类. 上面的代码使用input对象创建了一个 `User` 实体. 上面的代码很简单,但在实际应用程序中 `User` 实体会拥有很多属性,手动创建实体乏味且容易出错. `User` 和 `CreateUserInput` 添加新属性时还需要再去修改代码. |
|||
|
|||
我们需要一个库自动处理类到类的映射. ABP提供了对象到对象映射的抽象并集成了[AutoMapper](http://automapper.org/)做为对象映射器. |
|||
|
|||
## IObjectMapper |
|||
|
|||
`IObjectMapper` 接口 (在 [Volo.Abp.ObjectMapping](https://www.nuget.org/packages/Volo.Abp.ObjectMapping)包中) 定义了一个简单的 `Map` 方法. 上面的手动映射示例可以用以下方式重写: |
|||
|
|||
````csharp |
|||
public class UserAppService : ApplicationService |
|||
{ |
|||
private readonly IRepository<User, Guid> _userRepository; |
|||
|
|||
public UserAppService(IRepository<User, Guid> userRepository) |
|||
{ |
|||
_userRepository = userRepository; |
|||
} |
|||
|
|||
public void CreateUser(CreateUserInput input) |
|||
{ |
|||
//Automatically creating a new User object using the CreateUserInput object |
|||
var user = ObjectMapper.Map<CreateUserInput, User>(input); |
|||
|
|||
_userRepository.Insert(user); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> 示例中的 `ObjectMapper` 属性在 `ApplicationService` 基类中属性注入. 在其他地方也可以直接注入 `IObjectMapper` 接口. |
|||
|
|||
Map方法有两个泛型参数: 第一个是源对象类型,第二个是目标对象类型. |
|||
|
|||
如果想要设置现有对象属性,可以使用 `Map` 的重载方法: |
|||
|
|||
````csharp |
|||
public class UserAppService : ApplicationService |
|||
{ |
|||
private readonly IRepository<User, Guid> _userRepository; |
|||
|
|||
public UserAppService(IRepository<User, Guid> userRepository) |
|||
{ |
|||
_userRepository = userRepository; |
|||
} |
|||
|
|||
public async Task UpdateUserAsync(Guid id, UpdateUserInput input) |
|||
{ |
|||
var user = await _userRepository.GetAsync(id); |
|||
|
|||
//Automatically set properties of the user object using the UpdateUserInput |
|||
ObjectMapper.Map<UpdateUserInput, User>(input, user); |
|||
|
|||
await _userRepository.UpdateAsync(user); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
必须先定义映射,然后才能映射对象. 请参阅AutoMapper集成部分了解如何定义映射. |
|||
|
|||
## AutoMapper 集成 |
|||
|
|||
[AutoMapper](http://automapper.org/) 是最流行的对象到对象映射库之一. [Volo.Abp.AutoMapper](https://www.nuget.org/packages/Volo.Abp.AutoMapper)程序包使用AutoMapper实现了 `IObjectMapper`. |
|||
|
|||
定义了以下部分的映射后就可以使 `IObjectMapper` 接口. |
|||
|
|||
### 定义映射 |
|||
|
|||
AutoMapper提供了多种定义类之间映射的方法. 有关详细信息请参阅[AutoMapper的文档](https://docs.automapper.org). |
|||
|
|||
其中定义一种映射的方法是创建一个[Profile](https://docs.automapper.org/en/stable/Configuration.html#profile-instances) 类. 例如: |
|||
|
|||
````csharp |
|||
public class MyProfile : Profile |
|||
{ |
|||
public MyProfile() |
|||
{ |
|||
CreateMap<User, UserDto>(); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
然后使用`AbpAutoMapperOptions`注册配置文件: |
|||
|
|||
````csharp |
|||
[DependsOn(typeof(AbpAutoMapperModule))] |
|||
public class MyModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<AbpAutoMapperOptions>(options => |
|||
{ |
|||
//Add all mappings defined in the assembly of the MyModule class |
|||
options.AddMaps<MyModule>(); |
|||
}); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`AddMaps` 注册给定类的程序集中所有的配置类,通常使用模块类. 它还会注册 [attribute 映射](https://docs.automapper.org/en/stable/Attribute-mapping.html). |
|||
|
|||
### 配置验证 |
|||
|
|||
`AddMaps` 使用可选的 `bool` 参数控制[模块](Module-Development-Basics.md)的[配置验证](https://docs.automapper.org/en/stable/Configuration-validation.html): |
|||
|
|||
````csharp |
|||
options.AddMaps<MyModule>(validate: true); |
|||
```` |
|||
|
|||
如果此选项默认是 `false` , 但最佳实践建议启用. |
|||
|
|||
可以使用 `AddProfile` 而不是 `AddMaps` 来控制每个配置文件类的配置验证: |
|||
|
|||
````csharp |
|||
options.AddProfile<MyProfile>(validate: true); |
|||
```` |
|||
|
|||
> 如果你有多个配置文件,并且只需要为其中几个启用验证,那么首先使用`AddMaps`而不进行验证,然后为你想要验证的每个配置文件使用`AddProfile`. |
|||
|
|||
## 高级主题 |
|||
|
|||
### IObjectMapper<TContext> 接口 |
|||
|
|||
假设你已经创建了一个**可重用的模块**,其中定义了AutoMapper配置文件,并在需要映射对象时使用 `IObjectMapper`. 根据[模块化](Module-Development-Basics.md)的性质,你的模块可以用于不同的应用程序. |
|||
|
|||
`IObjectMapper` 是一个抽象,可以由最终应用程序替换使用另一个映射库. 这里的问题是你的可重用模块设计为使用AutoMapper,因为它为其定义映射配置文件. 这种情况下即使最终应用程序使用另一个默认对象映射库,你也要保证模块始终使用AutoMapper. |
|||
|
|||
`IObjectMapper<TContext>`将对象映射器上下文化,你可以为不同的 模块/上下文 使用不同的库. |
|||
|
|||
用法示例: |
|||
|
|||
````csharp |
|||
public class UserAppService : ApplicationService |
|||
{ |
|||
private readonly IRepository<User, Guid> _userRepository; |
|||
|
|||
private readonly IObjectMapper<MyModule> _objectMapper; |
|||
|
|||
public UserAppService( |
|||
IRepository<User, Guid> userRepository, |
|||
IObjectMapper<MyModule> objectMapper) //Inject module specific mapper |
|||
{ |
|||
_userRepository = userRepository; |
|||
_objectMapper = objectMapper; |
|||
} |
|||
|
|||
public async Task CreateUserAsync(CreateUserInput input) |
|||
{ |
|||
//Use the module specific mapper |
|||
var user = _objectMapper.Map<CreateUserInput, User>(input); |
|||
|
|||
await _userRepository.InsertAsync(user); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`UserAppService` 注入 `IObjectMapper<MyModule>`, 它是模块的特定对象映射器,用法与 `IObjectMapper` 完全相同. |
|||
|
|||
上面的示例代码未使用 `ApplicationService` 中定义的 `ObjectMapper` 属性,而是注入了 `IObjectMapper<MyModule>`. 但是 `ApplicationService` 定义了可以在类构造函数中设置的 `ObjectMapperContext` 属性, 因此仍然可以使用基类属性. 示例可以进行以下重写: |
|||
|
|||
````csharp |
|||
public class UserAppService : ApplicationService |
|||
{ |
|||
private readonly IRepository<User, Guid> _userRepository; |
|||
|
|||
public UserAppService(IRepository<User, Guid> userRepository) |
|||
{ |
|||
_userRepository = userRepository; |
|||
//Set the object mapper context |
|||
ObjectMapperContext = typeof(MyModule); |
|||
} |
|||
|
|||
public async Task CreateUserAsync(CreateUserInput input) |
|||
{ |
|||
var user = ObjectMapper.Map<CreateUserInput, User>(input); |
|||
|
|||
await _userRepository.InsertAsync(user); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
虽然使用上下文化的对象映射器与普通的对象映射器相同, 但是也应该在模块的 `ConfigureServices` 方法中注册上下文化的映射器: |
|||
|
|||
````csharp |
|||
[DependsOn(typeof(AbpAutoMapperModule))] |
|||
public class MyModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
//Use AutoMapper for MyModule |
|||
context.Services.AddAutoMapperObjectMapper<MyModule>(); |
|||
|
|||
Configure<AbpAutoMapperOptions>(options => |
|||
{ |
|||
options.AddMaps<MyModule>(validate: true); |
|||
}); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
`IObjectMapper<MyModule>`是可重用模块的一项基本功能,可在多个应用程序中使用,每个模块可以使用不同的库进行对象到对象的映射. 所有预构建的ABP模块都在使用它. 但是对于最终应用程序,你可以忽略此接口,始终使用默认的 `IObjectMapper` 接口. |
|||
|
|||
### IObjectMapper<TSource, TDestination> 接口 |
|||
|
|||
ABP允许自定义特定类的映射代码. 假设你要创建一个自定义类从 `User` 映射到 `UserDto`. 这种情况下,你可以创建一个实现 `IObjectMapper<User,UserDto>`的类 : |
|||
|
|||
````csharp |
|||
public class MyCustomUserMapper : IObjectMapper<User, UserDto>, ITransientDependency |
|||
{ |
|||
public UserDto Map(User source) |
|||
{ |
|||
//TODO: Create a new UserDto |
|||
} |
|||
|
|||
public UserDto Map(User source, UserDto destination) |
|||
{ |
|||
//TODO: Set properties of an existing UserDto |
|||
return destination; |
|||
} |
|||
} |
|||
```` |
|||
|
|||
ABP会自动发现注册 `MyCustomUserMapper`, 在你使用IObjectMapper将用户映射到UserDto时会自动使用自定义映射. |
|||
|
|||
一个类可以为不同的对象实现多个 `IObjectMapper<TSource,TDestination>`. |
|||
|
|||
> 这种方法功能强大, `MyCustomUserMapper`可以注入任何其他服务并在Map方法中使用. |
|||
@ -0,0 +1,3 @@ |
|||
# Options |
|||
|
|||
TODO! |
|||
@ -0,0 +1,180 @@ |
|||
# Settings |
|||
|
|||
[配置系统](Configuration.md) 是在启动时配置应用程序很好的方式. 除了配置之外, ABP提供了另外一种设置和获取应用程序设置的方式. |
|||
|
|||
设置存储在动态数据源(通常是数据库)中的键值对. 设置系统预构建了用户,租户,全局和默认设置方法并且可以进行扩展. |
|||
|
|||
## 定义设置 |
|||
|
|||
使用设置之前需要定义它. ABP是 [模块化](Module-Development-Basics.md)的, 不同的模块可以拥有不同的设置. 模块中派生 `SettingDefinitionProvider` 类定义模块内的配置. 示例如下: |
|||
|
|||
````csharp |
|||
public class EmailSettingProvider : SettingDefinitionProvider |
|||
{ |
|||
public override void Define(ISettingDefinitionContext context) |
|||
{ |
|||
context.Add( |
|||
new SettingDefinition("Smtp.Host", "127.0.0.1"), |
|||
new SettingDefinition("Smtp.Port", "25"), |
|||
new SettingDefinition("Smtp.UserName"), |
|||
new SettingDefinition("Smtp.Password", isEncrypted: true), |
|||
new SettingDefinition("Smtp.EnableSsl", "false") |
|||
); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
ABP会自动发现并注册设置的定义. |
|||
|
|||
### SettingDefinition |
|||
|
|||
`SettingDefinition` 类具有以下属性: |
|||
|
|||
* **Name**: 应用程序中设置的唯一名称. 是**具有约束的唯一属性**, 在应用程序获取/设置此设置的值 (设置名称定义为常量而不是`magic`字符串是个好主意). |
|||
* **DefaultValue**: 设置的默认值. |
|||
* **DisplayName**: 本地化的字符串,用于在UI上显示名称. |
|||
* **Description**: 本地化的字符串,用于在UI上显示描述. |
|||
* **IsVisibleToClients**: 布尔值,表示此设置是否在客户端可用. 默认为false,避免意外暴漏内部关键设置. |
|||
* **IsInherited**: 布尔值,此设置值是否从其他提供程序继承. 如果没有为请求的提供程序设置设定值,那么默认值是true并回退到下一个提供程序 (参阅设置值提供程序部分了解更多). |
|||
* **IsEncrypted**: 布尔值,表示是否在保存值是加密,读取时解密. 在数据库中存储加密的值. |
|||
* **Providers**: 限制可用于特定的设置值提供程序(参阅设置值提供程序部分了解更多). |
|||
* **Properties**: 设置此值的自定义属性 名称/值 集合,可以在之后的应用程序代码中使用. |
|||
|
|||
## 读取设置值 |
|||
|
|||
### ISettingProvider |
|||
|
|||
`ISettingProvider` 用于获取指定设置的值或所有设置的值. 示例用法: |
|||
|
|||
````csharp |
|||
public class MyService |
|||
{ |
|||
private readonly ISettingProvider _settingProvider; |
|||
|
|||
//Inject ISettingProvider in the constructor |
|||
public MyService(ISettingProvider settingProvider) |
|||
{ |
|||
_settingProvider = settingProvider; |
|||
} |
|||
|
|||
public async Task FooAsync() |
|||
{ |
|||
//Get a value as string. |
|||
string userName = await _settingProvider.GetOrNullAsync("Smtp.UserName"); |
|||
|
|||
//Get a bool value and fallback to the default value (false) if not set. |
|||
bool enableSsl = await _settingProvider.GetAsync<bool>("Smtp.EnableSsl"); |
|||
|
|||
//Get a bool value and fallback to the provided default value (true) if not set. |
|||
bool enableSsl = await _settingProvider.GetAsync<bool>( |
|||
"Smtp.EnableSsl", defaultValue: true); |
|||
|
|||
//Get a bool value with the IsTrueAsync shortcut extension method |
|||
bool enableSsl = await _settingProvider.IsTrueAsync("Smtp.EnableSsl"); |
|||
|
|||
//Get an int value or the default value (0) if not set |
|||
int port = (await _settingProvider.GetAsync<int>("Smtp.Port")); |
|||
|
|||
//Get an int value or null if not provided |
|||
int? port = (await _settingProvider.GetOrNullAsync("Smtp.Port"))?.To<int>(); |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> `ISettingProvider` 是非常常用的服务,一些基类中(如`IApplicationService`)已经将其属性注入. 这种情况下可以直接使用`SettingProvider`. |
|||
|
|||
### 在客户端读取设置值 |
|||
|
|||
如果允许在客户端显示某个设置,可以使用 JavaScript 代码读取设置值. 示例: |
|||
|
|||
````js |
|||
//Gets a value as string. |
|||
var language = abp.setting.get('Abp.Localization.DefaultLanguage'); |
|||
|
|||
//Gets an integer value. |
|||
var requiredLength = abp.setting.getInt('Abp.Identity.Password.RequiredLength'); |
|||
|
|||
//Gets a boolean value. |
|||
var requireDigit = abp.setting.getBoolean('Abp.Identity.Password.RequireDigit'); |
|||
```` |
|||
|
|||
使用 `abp.setting.values` 可以读取所有设置值的字典. |
|||
|
|||
## 设置值提供程序 |
|||
|
|||
设置系统是可扩展的, 你可以定义设置值提供程序扩展它,根据任何条件从任何来源获取设置值. |
|||
|
|||
`ISettingProvider` 使用设置值提供程序来获取设置值. 如果值提供程序无法获取设置值,则会回退到下一个值提供程序. |
|||
|
|||
有五个预构建设置值提供程序按以下顺序注册: |
|||
|
|||
* `DefaultValueSettingValueProvider`: 从设置定义的默认值中获取值(参见上面的SettingDefinition部分). |
|||
* `ConfigurationSettingValueProvider`: 从[IConfiguration服务](Configuration.md)中获取值. |
|||
* `GlobalSettingValueProvider`: 获取设置的全局(系统范围)值. |
|||
* `TenantSettingValueProvider`: 获取当前租户的设置值(参阅 [多租户](Multi-Tenancy.md)文档). |
|||
* `UserSettingValueProvider`: 获取当前用户的设置值(参阅 [当前用户](CurrentUser.md) 文档). |
|||
|
|||
> 设置回退系统从底部 (用户) 到 (默认) 方向起用用. |
|||
|
|||
全局,租户和用户设置值提供程序使用 `ISettingStore` 从数据源读取值(参见下面的小节). |
|||
|
|||
### 自定义设置值提供程序 |
|||
|
|||
扩展设置系统的方式是定义一个派生自 `SettingValueProvider` 的类. 示例: |
|||
|
|||
````csharp |
|||
public class CustomSettingValueProvider : SettingValueProvider |
|||
{ |
|||
public override string Name => "Custom"; |
|||
|
|||
public CustomSettingValueProvider(ISettingStore settingStore) |
|||
: base(settingStore) |
|||
{ |
|||
} |
|||
|
|||
public override Task<string> GetOrNullAsync(SettingDefinition setting) |
|||
{ |
|||
/* Return the setting value or null |
|||
Use the SettingStore or another data source */ |
|||
} |
|||
} |
|||
```` |
|||
|
|||
> 或者你直接可以实现 `ISettingValueProvider` 接口. 这时需要记得将其注册到 [依赖注入](Dependency-Injection.md). |
|||
|
|||
每一个提供程序都应该具有唯一的名称 (这里的名称是 "Custom" ). 内置提供程序使用给定的名称: |
|||
|
|||
* `DefaultValueSettingValueProvider`: "**D**". |
|||
* `ConfigurationSettingValueProvider`: "**C**". |
|||
* `GlobalSettingValueProvider`: "**G**". |
|||
* `TenantSettingValueProvider`: "**T**". |
|||
* `UserSettingValueProvider`: "**U**". |
|||
|
|||
最好使用一个字母的名称来减少数据库中的数据大小(提供者名称在每行中重复). |
|||
|
|||
定义自定义设置值提供程序后,需要将其显式注册到 `AbpSettingOptions`: |
|||
|
|||
````csharp |
|||
Configure<AbpSettingOptions>(options => |
|||
{ |
|||
options.ValueProviders.Add<CustomSettingValueProvider>(); |
|||
}); |
|||
```` |
|||
|
|||
本示例将其添加到最后一项,因此它将成为`ISettingProvider`使用的第一个值提供程序. 你也可以将其添加到`options.ValueProviders`列表的另一个位置. |
|||
|
|||
### ISettingStore |
|||
|
|||
尽管设置值提供程序可以自由使用任何来源来获取设置值,但 `ISettingStore` 服务是设置值的默认来源. 全局,租户和用户设置值提供者都使用它. |
|||
|
|||
## ISettingEncryptionService |
|||
|
|||
`ISettingEncryptionService` 用于在设置定义的 `isencryption` 属性设置为 `true` 时加密/解密设置值. |
|||
|
|||
你可以在依赖项入系统中替换此服务,自定义实现加密/解密过程. 默认实现 `StringEncryptionService` 使用AES算法(参见字符串[加密文档](String-Encryption.md)学习更多). |
|||
|
|||
## 设置管理模块 |
|||
|
|||
设置系统核心是相当独立的,不做任何关于如何管理(更改)设置值的假设. 默认的`ISettingStore`实现也是`NullSettingStore`,它为所有设置值返回null. |
|||
|
|||
设置管理模块通过管理数据库中的设置值来完成逻辑(实现`ISettingStore`).有关更多信息参阅[设置管理模块](Modules/Setting-Management.md)学习更多. |
|||
@ -0,0 +1,15 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling |
|||
{ |
|||
public interface IBundleContributor |
|||
{ |
|||
Task PreConfigureBundleAsync(BundleConfigurationContext context); |
|||
|
|||
Task ConfigureBundleAsync(BundleConfigurationContext context); |
|||
|
|||
Task PostConfigureBundleAsync(BundleConfigurationContext context); |
|||
|
|||
Task ConfigureDynamicResourcesAsync(BundleConfigurationContext context); |
|||
} |
|||
} |
|||
@ -1,11 +1,12 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling |
|||
{ |
|||
public interface IBundleManager |
|||
{ |
|||
IReadOnlyList<string> GetStyleBundleFiles(string bundleName); |
|||
Task<IReadOnlyList<string>> GetStyleBundleFilesAsync(string bundleName); |
|||
|
|||
IReadOnlyList<string> GetScriptBundleFiles(string bundleName); |
|||
Task<IReadOnlyList<string>> GetScriptBundleFilesAsync(string bundleName); |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification |
|||
{ |
|||
public interface IMinifier |
|||
{ |
|||
string Minify(string source, string fileName = null); |
|||
} |
|||
} |
|||
@ -1,13 +0,0 @@ |
|||
using NUglify; |
|||
using Volo.Abp.AspNetCore.Mvc.UI.Minification.Styles; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification.NUglify |
|||
{ |
|||
public class NUglifyCssMinifier : NUglifyMinifierBase, ICssMinifier |
|||
{ |
|||
protected override UglifyResult UglifySource(string source, string fileName) |
|||
{ |
|||
return Uglify.Css(source, fileName); |
|||
} |
|||
} |
|||
} |
|||
@ -1,26 +0,0 @@ |
|||
using System.Collections.Generic; |
|||
using System.Runtime.Serialization; |
|||
using NUglify; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification.NUglify |
|||
{ |
|||
public class NUglifyException : AbpException |
|||
{ |
|||
public List<UglifyError> Errors { get; set; } |
|||
|
|||
public NUglifyException(string message, List<UglifyError> errors) |
|||
: base(message) |
|||
{ |
|||
Errors = errors; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Constructor for serializing.
|
|||
/// </summary>
|
|||
public NUglifyException(SerializationInfo serializationInfo, StreamingContext context) |
|||
: base(serializationInfo, context) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -1,15 +0,0 @@ |
|||
using NUglify; |
|||
using NUglify.JavaScript; |
|||
using Volo.Abp.AspNetCore.Mvc.UI.Minification.Scripts; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification.NUglify |
|||
{ |
|||
public class NUglifyJavascriptMinifier : NUglifyMinifierBase, IJavascriptMinifier |
|||
{ |
|||
protected override UglifyResult UglifySource(string source, string fileName) |
|||
{ |
|||
return Uglify.Js(source, fileName); |
|||
} |
|||
} |
|||
} |
|||
@ -1,31 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using NUglify; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification.NUglify |
|||
{ |
|||
public abstract class NUglifyMinifierBase : IMinifier, ITransientDependency |
|||
{ |
|||
private static void CheckErrors(UglifyResult result) |
|||
{ |
|||
if (result.HasErrors) |
|||
{ |
|||
throw new NUglifyException( |
|||
$"There are some errors on uglifying the given source code!{Environment.NewLine}{result.Errors.Select(err => err.ToString()).JoinAsString(Environment.NewLine)}", |
|||
result.Errors |
|||
); |
|||
} |
|||
} |
|||
|
|||
public string Minify(string source, string fileName = null) |
|||
{ |
|||
var result = UglifySource(source, fileName); |
|||
CheckErrors(result); |
|||
return result.Code; |
|||
} |
|||
|
|||
protected abstract UglifyResult UglifySource(string source, string fileName); |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification.Scripts |
|||
{ |
|||
public interface IJavascriptMinifier : IMinifier |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Minification.Styles |
|||
{ |
|||
public interface ICssMinifier : IMinifier |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -1,14 +1,18 @@ |
|||
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Themes.Basic.Components.Toolbar.LanguageSwitch |
|||
@using System.Linq |
|||
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Themes.Basic.Components.Toolbar.LanguageSwitch |
|||
@model LanguageSwitchViewComponentModel |
|||
<div class="dropdown"> |
|||
<a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> |
|||
@Model.CurrentLanguage.DisplayName |
|||
</a> |
|||
@if (Model.OtherLanguages.Any()) |
|||
{ |
|||
<div class="dropdown"> |
|||
<a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> |
|||
@Model.CurrentLanguage.DisplayName |
|||
</a> |
|||
|
|||
<div class="dropdown-menu dropdown-menu-right border-0 shadow-sm" aria-labelledby="dropdownMenuLink"> |
|||
@foreach (var language in Model.OtherLanguages) |
|||
{ |
|||
<a class="dropdown-item" href="/Abp/Languages/Switch?culture=@(language.CultureName)&uiCulture=@(language.UiCultureName)&returnUrl=@Context.Request.Path">@language.DisplayName</a> |
|||
} |
|||
</div> |
|||
</div> |
|||
<div class="dropdown-menu dropdown-menu-right border-0 shadow-sm" aria-labelledby="dropdownMenuLink"> |
|||
@foreach (var language in Model.OtherLanguages) |
|||
{ |
|||
<a class="dropdown-item" href="/Abp/Languages/Switch?culture=@(language.CultureName)&uiCulture=@(language.UiCultureName)&returnUrl=@Context.Request.Path">@language.DisplayName</a> |
|||
} |
|||
</div> |
|||
</div> |
|||
} |
|||
|
|||
@ -0,0 +1,14 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared |
|||
{ |
|||
public class AbpErrorPageOptions |
|||
{ |
|||
public readonly IDictionary<string, string> ErrorViewUrls; |
|||
|
|||
public AbpErrorPageOptions() |
|||
{ |
|||
ErrorViewUrls = new Dictionary<string, string>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Localization.Resources.AbpUi; |
|||
using Microsoft.AspNetCore.Diagnostics; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Localization; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.AspNetCore.ExceptionHandling; |
|||
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Views.Error; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Controllers |
|||
{ |
|||
public class ErrorController : AbpController |
|||
{ |
|||
private readonly IExceptionToErrorInfoConverter _errorInfoConverter; |
|||
private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; |
|||
private readonly IStringLocalizer<AbpUiResource> _localizer; |
|||
private readonly AbpErrorPageOptions _abpErrorPageOptions; |
|||
|
|||
public ErrorController( |
|||
IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, |
|||
IHttpExceptionStatusCodeFinder httpExceptionStatusCodeFinder, |
|||
IOptions<AbpErrorPageOptions> abpErrorPageOptions, |
|||
IStringLocalizer<AbpUiResource> localizer) |
|||
{ |
|||
_errorInfoConverter = exceptionToErrorInfoConverter; |
|||
_statusCodeFinder = httpExceptionStatusCodeFinder; |
|||
_localizer = localizer; |
|||
_abpErrorPageOptions = abpErrorPageOptions.Value; |
|||
} |
|||
|
|||
public IActionResult Index(int httpStatusCode) |
|||
{ |
|||
var exHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>(); |
|||
|
|||
var exception = exHandlerFeature != null |
|||
? exHandlerFeature.Error |
|||
: new Exception(_localizer["UnhandledException"]); |
|||
|
|||
var errorInfo = _errorInfoConverter.Convert(exception); |
|||
|
|||
if (httpStatusCode == 0) |
|||
{ |
|||
httpStatusCode = (int)_statusCodeFinder.GetStatusCode(HttpContext, exception); |
|||
} |
|||
|
|||
HttpContext.Response.StatusCode = httpStatusCode; |
|||
|
|||
var page = GetErrorPageUrl(httpStatusCode); |
|||
|
|||
return View(page, new AbpErrorViewModel |
|||
{ |
|||
ErrorInfo = errorInfo, |
|||
HttpStatusCode = httpStatusCode |
|||
}); |
|||
} |
|||
|
|||
private string GetErrorPageUrl(int statusCode) |
|||
{ |
|||
var page = _abpErrorPageOptions.ErrorViewUrls.GetOrDefault(statusCode.ToString()); |
|||
|
|||
if (string.IsNullOrWhiteSpace(page)) |
|||
{ |
|||
return "~/Pages/Error/Default.cshtml"; |
|||
} |
|||
|
|||
return page; |
|||
} |
|||
} |
|||
} |
|||
@ -1,41 +0,0 @@ |
|||
@page |
|||
@using System.Linq |
|||
@using System.Collections.Generic |
|||
@using Localization.Resources.AbpUi |
|||
@using Microsoft.AspNetCore.Mvc.Localization |
|||
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
|||
@model IndexModel |
|||
@inject IHtmlLocalizer<AbpUiResource> L |
|||
@{ |
|||
var errorMessage = Model.ErrorInfo.Message; |
|||
var errorDetails = Model.ErrorInfo.Details; |
|||
if (errorDetails.IsNullOrEmpty()) |
|||
{ |
|||
errorDetails = errorMessage; |
|||
errorMessage = L["Error"].Value + "!"; |
|||
} |
|||
} |
|||
|
|||
<h1> |
|||
[@Model.HttpStatusCode] @errorMessage |
|||
</h1> |
|||
|
|||
<div> |
|||
<p> |
|||
@errorDetails |
|||
</p> |
|||
<p> |
|||
@if (!Model.ErrorInfo.ValidationErrors.IsNullOrEmpty()) |
|||
{ |
|||
foreach (var validationError in Model.ErrorInfo.ValidationErrors) |
|||
{ |
|||
<text>* </text>@(validationError.Message) |
|||
if (validationError.Members != null && validationError.Members.Any()) |
|||
{ |
|||
<text>(@string.Join(", ", validationError.Members))</text> |
|||
} |
|||
<br /> |
|||
} |
|||
} |
|||
</p> |
|||
</div> |
|||
@ -1,69 +0,0 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Diagnostics; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.AspNetCore.Mvc.RazorPages; |
|||
using Volo.Abp.AspNetCore.ExceptionHandling; |
|||
using Volo.Abp.Http; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
|||
{ |
|||
public class IndexModel : PageModel |
|||
{ |
|||
public RemoteServiceErrorInfo ErrorInfo { get; set; } |
|||
|
|||
[BindProperty(SupportsGet = true)] |
|||
public int HttpStatusCode { get; set; } |
|||
|
|||
private readonly IExceptionToErrorInfoConverter _errorInfoConverter; |
|||
private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder; |
|||
|
|||
public IndexModel(IExceptionToErrorInfoConverter errorInfoConverter, IHttpExceptionStatusCodeFinder statusCodeFinder) |
|||
{ |
|||
_errorInfoConverter = errorInfoConverter; |
|||
_statusCodeFinder = statusCodeFinder; |
|||
} |
|||
|
|||
public void OnGet() |
|||
{ |
|||
HandleError(); |
|||
} |
|||
|
|||
public void OnPost() |
|||
{ |
|||
HandleError(); |
|||
} |
|||
|
|||
public void OnPut() |
|||
{ |
|||
HandleError(); |
|||
} |
|||
|
|||
public void OnDelete() |
|||
{ |
|||
HandleError(); |
|||
} |
|||
|
|||
public void OnPatch() |
|||
{ |
|||
HandleError(); |
|||
} |
|||
|
|||
private void HandleError() |
|||
{ |
|||
var exHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>(); |
|||
|
|||
var exception = exHandlerFeature != null |
|||
? exHandlerFeature.Error |
|||
: new Exception("Unhandled exception!"); //TODO: Localize?
|
|||
|
|||
ErrorInfo = _errorInfoConverter.Convert(exception); |
|||
|
|||
if (HttpStatusCode == 0) |
|||
{ |
|||
HttpStatusCode = (int)_statusCodeFinder.GetStatusCode(HttpContext, exception); |
|||
} |
|||
|
|||
HttpContext.Response.StatusCode = HttpStatusCode; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using Volo.Abp.Http; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Views.Error |
|||
{ |
|||
public class AbpErrorViewModel |
|||
{ |
|||
public RemoteServiceErrorInfo ErrorInfo { get; set; } |
|||
|
|||
public int HttpStatusCode { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
@using System.Linq |
|||
@using System.Collections.Generic |
|||
@using Localization.Resources.AbpUi |
|||
@using Microsoft.AspNetCore.Mvc.Localization |
|||
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Error |
|||
@model AbpErrorPageModel |
|||
@inject IHtmlLocalizer<AbpUiResource> L |
|||
@{ |
|||
var errorMessage = Model.ErrorInfo.Message; |
|||
var errorDetails = Model.ErrorInfo.Details; |
|||
if (errorDetails.IsNullOrEmpty()) |
|||
{ |
|||
errorDetails = errorMessage; |
|||
errorMessage = L["Error"].Value + "!"; |
|||
} |
|||
} |
|||
|
|||
<h1> |
|||
[@Model.HttpStatusCode] @errorMessage |
|||
</h1> |
|||
|
|||
<div> |
|||
<p> |
|||
@errorDetails |
|||
</p> |
|||
<p> |
|||
@if (!Model.ErrorInfo.ValidationErrors.IsNullOrEmpty()) |
|||
{ |
|||
foreach (var validationError in Model.ErrorInfo.ValidationErrors) |
|||
{ |
|||
<text>* </text>@(validationError.Message) |
|||
if (validationError.Members != null && validationError.Members.Any()) |
|||
{ |
|||
<text>(@string.Join(", ", validationError.Members))</text> |
|||
} |
|||
<br /> |
|||
} |
|||
} |
|||
</p> |
|||
</div> |
|||
@ -0,0 +1,49 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.ModelBinding |
|||
{ |
|||
public class AbpDateTimeModelBinder : IModelBinder |
|||
{ |
|||
private readonly Type _type; |
|||
private readonly SimpleTypeModelBinder _simpleTypeModelBinder; |
|||
private readonly IClock _clock; |
|||
|
|||
public AbpDateTimeModelBinder(ModelBinderProviderContext context) |
|||
{ |
|||
_type = context.Metadata.ModelType; |
|||
_clock = context.Services.GetRequiredService<IClock>(); |
|||
_simpleTypeModelBinder = new SimpleTypeModelBinder(context.Metadata.ModelType, |
|||
context.Services.GetRequiredService<ILoggerFactory>()); |
|||
} |
|||
|
|||
public async Task BindModelAsync(ModelBindingContext bindingContext) |
|||
{ |
|||
await _simpleTypeModelBinder.BindModelAsync(bindingContext); |
|||
|
|||
if (!bindingContext.Result.IsModelSet) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (_type == typeof(DateTime)) |
|||
{ |
|||
var dateTime = (DateTime) bindingContext.Result.Model; |
|||
bindingContext.Result = ModelBindingResult.Success(_clock.Normalize(dateTime)); |
|||
} |
|||
else |
|||
{ |
|||
var dateTime = (DateTime?) bindingContext.Result.Model; |
|||
if (dateTime != null) |
|||
{ |
|||
bindingContext.Result = ModelBindingResult.Success(_clock.Normalize(dateTime.Value)); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.ModelBinding |
|||
{ |
|||
public class AbpDateTimeModelBinderProvider : IModelBinderProvider |
|||
{ |
|||
public IModelBinder GetBinder(ModelBinderProviderContext context) |
|||
{ |
|||
if (context.Metadata.ModelType != typeof(DateTime) && |
|||
context.Metadata.ModelType != typeof(DateTime?)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
if (context.Metadata.ContainerType == null) |
|||
{ |
|||
if (context.Metadata is DefaultModelMetadata defaultModelMetadata && |
|||
defaultModelMetadata.Attributes.Attributes.All(x => x.GetType() != typeof(DisableDateTimeNormalizationAttribute))) |
|||
{ |
|||
return new AbpDateTimeModelBinder(context); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
var dateNormalizationDisabledForClass = |
|||
context.Metadata.ContainerType.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true); |
|||
|
|||
var dateNormalizationDisabledForProperty = context.Metadata.ContainerType |
|||
.GetProperty(context.Metadata.PropertyName) |
|||
?.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true); |
|||
|
|||
if (!dateNormalizationDisabledForClass && |
|||
dateNormalizationDisabledForProperty != null && |
|||
!dateNormalizationDisabledForProperty.Value) |
|||
{ |
|||
return new AbpDateTimeModelBinder(context); |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue