mirror of https://github.com/abpframework/abp.git
csharpabpc-sharpframeworkblazoraspnet-coredotnet-coreaspnetcorearchitecturesaasdomain-driven-designangularmulti-tenancy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Tree:
5d019d9ef1
10.0-ai-docs
2024-06-21-Use_User-Defined_Function_Mapping_For_Global_Filter
8.0-prevent-stackoverflow-blazor
9.0-clear-breadcrumbs
BackgroundJob-ApplicationName-Test
Blazorise-Form-Submit
ExtensionPropertyPolicy-Test
IApplicationConfigurationContributor
IGuidGenerator
IReadOnlyAppService
InboxProcessor-OutboxSender
InteractiveServerRenderMode(prerender-true)
Issue-#23338
LockoutEnd-Time
NullAbpDistributedLock-patch
Operation-Rate-Limiting-Test
RandomHelper
Shared-User-Account
Testcontainers
Theme-Async
TimezoneHelpText
X509KeyStorageFlags
abp-plugin
abp-release-post
abp-studio/issue/2891
abp-united-pre-merge
add-loading-component
advanced-features
article/angular-multithreading
auto-merge/prerel-8-2/2729
auto-merge/prerel-9-0/3134
auto-merge/prerel-9-0/3181
auto-merge/rel-0-0/42712
auto-merge/rel-0-0/42712-1
auto-merge/rel-10-0/4007
auto-merge/rel-10-0/4064
auto-merge/rel-10-0/4133
auto-merge/rel-10-1/4237
auto-merge/rel-10-2/4454
auto-merge/rel-10-2/4463
auto-merge/rel-10-4/4526
auto-merge/rel-10-4/4577
auto-merge/rel-10-4/4578
auto-merge/rel-10-4/4579
auto-merge/rel-7-4/2239
auto-merge/rel-7-4/2263
auto-merge/rel-7-4/2276
auto-merge/rel-8-0/2472
auto-merge/rel-8-0/2715
auto-merge/rel-8-0/2741
auto-merge/rel-8-1/2527
auto-merge/rel-8-2/2851
auto-merge/rel-8-3/3151
auto-merge/rel-9-0/3296
auto-merge/rel-9-0/3304
auto-merge/rel-9-1/3656
auto-merge/rel-9-2/3571
auto-merge/rel-9-2/3652
aws-service-url
before-net10
blazor-full-stack-web-ui-interactiveauto
blazor-full-stack-web-ui-interactiveauto-modified
clear-breadcrumbs
cmskit-cache
copilot/allow-adding-public-website
copilot/sub-pr-24469
copilot/sub-pr-24636
copilot/sub-pr-24842
copilot/sub-pr-24889
copilot/sub-pr-24949
data-collection-code-review-2
data-collection-review-3
data-collection-review-5
dev
devnot-demo
dgnyldrm7-patch-1
docs-fix
docs-preview-8.3
docs-test-1
docs-unification
docs-unification-1-enhancements
docs/studio-2.2.1
docs/studio-2.2.2
docs/studio-2.2.3
docs/studio-2.2.4
document-abp-run-watch-command
document-index-backgroundjob
documentation/#20827-modular-monolith-application
documentation/20827
documentation/abp-extensible-form-doc
ebicoglu-patch-1
ebicoglu-patch-2
enhance-link-user
enisn-patch-1
enisn/blobstoring-bunnycdn
feat/#23955
feat/ai-assistant-article-enhancements
feat/dynamic-form-component
feat/mudblazor-mix
feat/typescript-on-mvc
feature/#19594
feature/#23180
feature/data-collection
feature/logo-provider
feature/ssr-migration-schematic
fix-admin-docs-test
fix/#20241
fix/#23795
fix/21899-resource-permission-refresh
fix/feature-allowed-providers-respected-by-management
fullstackblazor
gizem/HTML-encode-TagHelper-xss-test
gizem/security-logs
halil-presentation-2024-05
hybrid-ui
hybrid-ui-embedding
ingress-nginx/ingress-nginx
issue-#23231
issue-#3417
issue-#3591
issue-#3596
issue-#6679
issue-#6768
issue-18421
issue-19307
issue-20436
issue-20924-2
issue-21015
issue-22408
issue-23113
issue-23231
issue-23231-v2
issue-23286
issue-24272
issue-24438-test
issue-24635-test
issue-24672
issue-24674
issue-24918-2
issue-24918-bg-workers
issue-2884
issue-3384/L
issue-3596
issue-3870/L
issue-5131
issue-5384
issue-codemirror
issue-volo-21285
issue-vs-7809
issue/22675
issue/abp-ng-upgrade
issue/assign-available-port-for-suite
issue/updateAngularInstallers
jquery-form-extensions.js-v2
leptonx-versions
license-banner-localization
license-banner-localization-1
local-ref-basic-theme
maliming-patch-1
maliming-patch-2
maliming-patch-4
maliming/ParameterApiDescriptionModel
maliming/ShouldValidatePredicates
maliming/fix-blazor-wasm-stackoverflow-logging
maliming/issue-25457-layered-diagrams
maliming/mud-qa-fixes
maliming/suite-mudblazor-docs
mansur-vs-internal-#7864
mauiglobalbundle
migrating-abp-packages/core
migrating-abp-packages/identity
migrating-abp-packages/setting-management
migrating-abp-packages/tenant-management
migrating-abp-packages/theme-shared
oauth2-redirect.html
pr/2526
pr/6814
prerel-9.0
rel-1.0
rel-1.1
rel-10.0
rel-10.1
rel-10.2
rel-10.3
rel-10.4
rel-2.0
rel-2.1
rel-2.2
rel-2.3
rel-2.4
rel-2.5
rel-2.6
rel-2.7
rel-2.8
rel-2.9
rel-3.0
rel-3.1
rel-3.2
rel-3.3
rel-4.0
rel-4.1
rel-4.2
rel-4.3
rel-4.4
rel-5.0
rel-5.1
rel-5.2
rel-5.3
rel-6.0
rel-7.0
rel-7.1
rel-7.2
rel-7.3
rel-7.4
rel-8.0
rel-8.1
rel-8.2
rel-8.3
rel-9.0
rel-9.1
rel-9.2
rel-9.3
remove-deprecateds
return-code-vs-exception-article
revert-18876-blazor-full-stack-web-ui
revert-20264-EngincanV/cli-old-vs-new
revert-23436-Mapperly-maliming
salihozkara-patch-1
salihozkara-patch-2
salihozkara/ImproveBundleFile
salihozkara/blazorise-validation-message
salihozkara/complex-property
salihozkara/fix-image
salihozkara/fixDatePicker
salihozkara/mapperly-test
scrapstation/dev
social-login-usages
studio-custom-command-doc
studio-document-start-and-wait
template-config-provider
templates-version
test-branch-24299
test-fix/resource-permission-definition-bugs
test/distribution-batch-state-checker-projection-on-10.4
update-bundle
update-cli-docs
update-doc
update-latest-versions-7.4.3
update-latest-versions-8.1.2
update-latest-versions-8.1.5
update-latest-versions-8.2.3
update-latest-versions-9.0.7
update-latest-versions-9.1.0
update-upgrade-cod-23-08-2024
upgrade-command-doc
vs-internal-issue-#7727
vsInternal-viewcChangeHistory-localization
vsinternal-7565
zh-hans.json
ìssue-17361
0.10.0
0.11.0
0.12.0
0.13.0
0.14.0
0.15.0
0.16.0
0.17.0.0
0.18.0
0.18.1
0.19.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3.2
9.3.3
9.3.4
9.3.5
9.3.6
9.3.7
v0.3.1
v0.3.2
v0.3.2.1
v0.3.2.2
v0.3.3
v0.3.3.1
v0.3.3.2
v0.3.4
v0.3.5
v0.3.6
v0.6.2
v1.1.1
v9.2.0-preview
${ noResults }
abp/docs/zh-Hans/Best-Practices/Repositories.md
3.3 KiB
3.3 KiB
仓储最佳实践 & 约定
仓储接口
- 推荐 在领域层中定义仓储接口.
- 推荐 为每个聚合根定义仓储接口(如
IIdentityUserRepository)并创建相应的实现.- 推荐 在应用代码中使用仓储时应该注入仓储接口.
- 不推荐 在应用代码中使用泛型仓储接口(如
IRepository<IdentityUser, Guid>). - 不推荐 在应用代码(领域, 应用... 层)中使用
IQueryable<TEntity>特性.
聚合根的示例:
public class IdentityUser : AggregateRoot<Guid>
{
//...
}
定义仓储接口, 如下所示:
public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
{
//...
}
- 不推荐 仓储接口继承
IRepository<TEntity, TKey>接口. 因为它继承了IQueryable而仓储不应该将IQueryable暴漏给应用. - 推荐 通常仓储接口继承自
IBasicRepository<TEntity, TKey>或更低级别的接口, 如IReadOnlyRepository<TEntity, TKey>(在需要的时候). - 不推荐 为实体定义仓储接口,因为它们不是聚合根.
仓储方法
- 推荐 所有的仓储方法定义为 异步.
- 推荐 为仓储的每个方法添加 可选参数
cancellationToken. 例:
Task<IdentityUser> FindByNormalizedUserNameAsync(
[NotNull] string normalizedUserName,
CancellationToken cancellationToken = default
);
- 推荐 为仓储的每个异步方法创建一个 同步扩展 方法. 示例:
public static class IdentityUserRepositoryExtensions
{
public static IdentityUser FindByNormalizedUserName(
this IIdentityUserRepository repository,
[NotNull] string normalizedUserName)
{
return AsyncHelper.RunSync(
() => repository.FindByNormalizedUserNameAsync(normalizedUserName)
);
}
}
对于同步方法而言, 这会让它们更方便的调用仓储方法.
- 推荐 为仓储中返回单个实体的方法添加一个可选参数
bool includeDetails = true(默认值为true). 示例:
Task<IdentityUser> FindByNormalizedUserNameAsync(
[NotNull] string normalizedUserName,
bool includeDetails = true,
CancellationToken cancellationToken = default
);
该参数由ORM实现, 用来加载实体子集合.
- 推荐 为仓储中返回实体列表的方法添加一个可选参数
bool includeDetails = false(默认值为false). 示例:
Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
string normalizedRoleName,
bool includeDetails = false,
CancellationToken cancellationToken = default
);
- 不推荐 创建复合类通过调用仓储单个方法返回组合实体. 比如: UserWithRoles, UserWithTokens, UserWithRolesAndTokens. 相反, 正确的使用
includeDetails选项, 在需要时加载实体所有的详细信息. - 避免 为了从仓储中获取实体的部分属性而为实体创建投影类. 比如: 避免通过创建BasicUserView来选择所需的一些属性. 相反可以直接使用聚合根类. 不过这条规则有例外情况:
- 性能对于用例来说非常重要,而且使用整个聚合根对性能的影响非常大.