Translate the Repositories document.

pull/542/head
梁士伟 7 years ago
parent b110214d57
commit 79432c9cdd

@ -50,7 +50,7 @@ namespace MyCompany.MyProject
### 将DbContext注册到依赖注入
在module中的ConfigureServices方法使用 `AddAbpDbContext` 在[依赖注入](Dependency-Injection.cn.md)系统注册DbContext类.
在module中的ConfigureServices方法使用 `AddAbpDbContext` 在[依赖注入](Dependency-Injection.md)系统注册DbContext类.
````C#
using Microsoft.Extensions.DependencyInjection;

@ -1,6 +1,6 @@
## 本地化
ABP的本地化系统与`Microsoft.Extensions.Localization`无缝集成,并与[AspnetCore的本地化文档](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/localization?view=aspnetcore-2.1)兼容. 它添加了一些实用功能和增强功能使其更易于在实际开发中应用.
ABP的本地化系统与`Microsoft.Extensions.Localization`无缝集成,并与[AspnetCore的本地化文档](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/localization?view=aspnetcore-2.1)兼容. 它添加了一些实用功能和增强功能, 使其更易于在实际开发中应用.
### Volo.Abp.Localization Package
@ -65,7 +65,7 @@ public class MyModule : AbpModule
* 添加了一个新的本地化资源, 使用"en"(英语)作为默认的本地化.
* 用JSON文件存储本地化字符串.
* 使用[虚拟文件系统](Virtual-File-System.cn.md) 将JSON文件嵌入到程序集中.
* 使用[虚拟文件系统](Virtual-File-System.md) 将JSON文件嵌入到程序集中.
JSON文件位于 "/Localization/Resources/Test" 项目文件夹下, 如下图所示:
@ -172,7 +172,7 @@ public class MyService
<h1>@Localizer["HelloWorld"]</h1>
````
有关在服务器端使用本地化的详细使用方法请参阅[AspNetCore的本地化文档](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/localization)
有关在服务器端使用本地化的详细使用方法, 请参阅[AspNetCore的本地化文档](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/localization)
##### 客户端

@ -1,12 +1,12 @@
## Repositories
## 仓储
"*Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects*" (Martin Fowler).
"*在领域层和数据映射层之间进行中介,使用类似集合的接口来操作领域对象.*" (Martin Fowler).
Repositories, in practice, are used to perform database operations for domain objects (see [Entities](Entities.md)). Generally, a separated repository is used for each **aggregate root** or entity.
实际上,仓储用于领域对象在数据库(参阅[实体](Entities.md))中的操作, 通常每个 **聚合根** 或不同的实体创建对应的仓储.
### Generic Repositories
### 通用(泛型)仓储
ABP can provide a **default generic repository** for each aggregate root or entity. You can [inject](Dependency-Injection.md) `IRepository<TEntity, TKey>` into your service and perform standard **CRUD** operations. Example usage:
ABP为每个聚合根或实体提供了 **默认的通用(泛型)仓储** . 你可以在服务中[注入](Dependency-Injection.md) `IRepository<TEntity, TKey>` 使用标准的**CRUD**操作. 用法示例:
````C#
public class PersonAppService : ApplicationService
@ -38,49 +38,49 @@ public class PersonAppService : ApplicationService
}
````
In this example;
在这个例子中;
* `PersonAppService` simply injects `IRepository<Person, Guid>` in it's constructor.
* `Create` method uses `InsertAsync` to save a newly created entity.
* `GetList` method uses the standard LINQ `Where` and `ToList` methods to filter and get a list of people from the data source.
* `PersonAppService` 在它的构造函数中注入了 `IRepository<Person, Guid>` .
* `Create` 方法使用了 `InsertAsync` 创建并保存新的实体.
* `GetList` 方法使用标准LINQ `Where``ToList` 方法在数据源中过滤并获取People集合.
> The example above uses hand-made mapping between [entities](Entities.md) and [DTO](Data-Transfer-Objects.md)s. See [object to object mapping document](Object-To-Object-Mapping.md) for an automatic way of mapping.
> 上面的示例在[实体](Entities.md)与[DTO](Data-Transfer-Objects.md)之间使用了手动映射. 参阅 [对象映射](Object-To-Object-Mapping.md) 了解自动映射的使用方式.
Generic Repositories provides some standard CRUD features out of the box:
通用仓储提供了一些开箱即用的标准CRUD功能:
* Providers `Insert` method to save a new entity.
* Providers `Update` and `Delete` methods to update or delete an entity by entity object or it's id.
* Provides `Delete` method to delete multiple entities by a filter.
* Implements `IQueryable<TEntity>`, so you can use LINQ and extension methods like `FirstOrDefault`, `Where`, `OrderBy`, `ToList` and so on...
* Have **sync** and **async** versions for all methods.
* 提供 `Insert` 方法用于保存新实体.
* 提供 `Update``Delete` 方法通过实体或实体id更新或删除实体.
* 提供 `Delete` 方法使用条件表达式过滤删除多个实体.
* 实现了 `IQueryable<TEntity>`, 所以你可以使用LINQ和扩展方法 `FirstOrDefault`, `Where`, `OrderBy`, `ToList`...
* 所有方法都具有 **sync(同步)****async(异步)** 版本.
#### Generic Repository without a Primary Key
#### 无主键的通用(泛型)仓储
If your entity does not have an Id primary key (it may have a composite primary key for instance) then you cannot use the `IRepository<TEntity, TKey>` defined above. In that case, you can inject and use `IRepository<TEntity>` for your entity.
如果你的实体没有id主键 (例如, 它可能具有复合主键) 那么你不能使用上面定义的 `IRepository<TEntity, TKey>`, 在这种情况下你可以仅使用实体(类型)注入 `IRepository<TEntity>`.
> `IRepository<TEntity>` has a few missing methods those normally works with the `Id` property of an entity. Because of the entity has no `Id` property in that case, these methods are not available. One example is the `Get` method that gets an id and returns the entity with given id. However, you can still use `IQueryable<TEntity>` features to query entities by standard LINQ methods.
> `IRepository<TEntity>` 有一些缺失的方法, 通常与实体的 `Id` 属性一起使用. 由于实体在这种情况下没有 `Id` 属性, 因此这些方法不可用. 比如 `Get` 方法通过id获取具有指定id的实体. 不过, 你仍然可以使用`IQueryable<TEntity>`的功能通过标准LINQ方法查询实体.
### Basic Repositories
### 基础仓储
Standard `IRepository<TEntity, TKey>` interface extends standard `IQueryable<TEntity>` and you can freely query using standard LINQ methods. However, some ORM providers or database systems may not support standard `IQueryable` interface.
`IRepository<TEntity, TKey>` 接口扩展了标准 `IQueryable<TEntity>` 你可以使用标准LINQ方法自由查询.但是,某些ORM提供程序或数据库系统可能不支持`IQueryable`接口.
ABP provides `IBasicRepository<TEntity, TPrimaryKey>` and `IBasicRepository<TEntity>` interfaces to support such scenarios. You can extend these interfaces (and optionally derive from `BasicRepositoryBase`) to create custom repositories for your entities.
ABP提供了 `IBasicRepository<TEntity, TPrimaryKey>``IBasicRepository<TEntity>` 接口来支持这样的场景. 你可以扩展这些接口(并可选择性地从`BasicRepositoryBase`派生)为你的实体创建自定义存储库.
Depending on `IBasicRepository` but not depending on `IRepository` has an advantage to make possible to work with all data sources even if they don't support `IQueryable`. But major vendors, like Entity Framework, NHibernate or MongoDb already support `IQueryable`.
依赖于 `IBasicRepository` 而不是依赖 `IRepository` 有一个优点, 即使它们不支持 `IQueryable` 也可以使用所有的数据源, 但主要的供应商, 像 Entity Framework, NHibernate 或 MongoDb 已经支持了 `IQueryable`.
So, working with `IRepository` is the **suggested** way for typical applications. But reusable module developers may consider `IBasicRepository` to support a wider range of data sources.
因此, 使用 `IRepository` 是典型应用程序的 **建议方法**. 但是可重用的模块开发人员可能会考虑使用 `IBasicRepository` 来支持广泛的数据源.
### Custom Repositories
### 自定义仓储
Default generic repositories will be sufficient for most cases. However, you may need to create a custom repository class for your entity.
对于大多数情况, 默认通用仓储就足够了. 但是, 你可能会需要为实体创建自定义仓储类.
#### Custom Repository Example
#### 自定义仓储示例
ABP does not force you to implement any interface or inherit from any base class for a repository. It can be just a simple POCO class. However, it's suggested to inherit existing repository interface and classes to make your work easier and get the standard methods out of the box.
ABP不会强制您实现任何接口或从存储库的任何基类继承. 它可以只是一个简单的POCO类. 但是建议继承现有的仓储接口和类, 获得开箱即用的标准方法使你的工作更轻松.
##### Custom Repository Interface
##### 自定义仓储接口
First, define an interface in your domain layer:
首先在领域层定义一个仓储接口:
```c#
public interface IPersonRepository : IRepository<Person, Guid>
@ -89,11 +89,11 @@ public interface IPersonRepository : IRepository<Person, Guid>
}
```
This interface extends `IRepository<Person, Guid>` to take advantage of pre-built repository functionality.
此接口扩展了 `IRepository<Person, Guid>` 以使用已有的通用仓储功能.
##### Custom Repository Implementation
##### 自定义仓储实现
A custom repository is tightly coupled to the data access tool type you are using. In this example, we will use Entity Framework Core:
自定义存储库依赖于你使用的数据访问工具. 在此示例中, 我们将使用Entity Framework Core:
````C#
public class PersonRepository : EfCoreRepository<MyDbContext, Person, Guid>, IPersonRepository
@ -113,5 +113,4 @@ public class PersonRepository : EfCoreRepository<MyDbContext, Person, Guid>, IPe
}
````
You can directly access the data access provider (`DbContext` in this case) to perform operations. See [entity framework integration document](Entity-Framework-Core.md) for more about custom repositories based on EF Core.
你可以直接使用数据库访问提供程序 (本例中是 `DbContext` ) 来执行操作. 有关基于EF Core的自定义仓储的更多信息, 请参阅[EF Core 集成文档](Entity-Framework-Core.md).
Loading…
Cancel
Save