斗爷

导航

ABP框架系列之二:(Entity Framework Core-实体核心框架)

Introduction(介绍

Abp.EntityFrameworkCore nuget package is used to integrate to Entity Framework (EF) Core ORM framework. After installing this package, we should also add a DependsOn attribute for AbpEntityFrameworkCoreModule.

abp.entityframeworkcore NuGet包是用来整合实体框架(EF)核心的ORM框架。安装此包后,我们还应该加上一个AbpEntityFrameworkCoreModule依赖属性。

DbContext

EF Core requires to define a class derived from DbContext. In ABP, we should derive from AbpDbContext as shown below:

EF核心需要定义来自DbContext类。在ABP,我们应该从abpdbcontext获得,如下所示:

public class MyDbContext : AbpDbContext
{
    public DbSet<Product> Products { get; set; }

    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }
}

Constructor should get a DbContextOptions<T> as shown above. Parameter name must be options. It's not possible to change it because ABP provides it as anonymous object parameter.

构造函数必须得到 DbContextOptions<T> 如上面所示. 参数名称必须是选项。无法改变它,因为ABP将它作为匿名对象参数提供。

Configuration(配置

In Startup Class(启动类

Use AddAbpDbContext method on service collection in ConfigureServices method as shown below:

services.AddAbpDbContext<MyDbContext>(options =>
{
    options.DbContextOptions.UseSqlServer(options.ConnectionString);
});

For non web projects, we will not have a Startup class. In that case, we can use Configuration.Modules.AbpEfCore().AddDbContext method in our module class to configure DbContext, as shown below:

对于非Web项目,我们将不会有一个启动类。在这种情况下,我们可以使用配置模块。abpefcore().adddbcontext方法在我们的模块配置DbContext类,如下图所示:

Configuration.Modules.AbpEfCore().AddDbContext<MyDbContext>(options =>
{
    options.DbContextOptions.UseSqlServer(options.ConnectionString);
});

We used given connection string and used Sql Server as database provider. options.ConnectionString is the default connection string (see next section) normally. But ABP uses IConnectionStringResolver to determine it. So, this behaviour can be changed and connection string can be determined dynamically. The action passed to AddDbContext is called whenever a DbContext instance will be created. So, you also have a chance to return different connection string conditionally.

我们使用给定的连接字符串,并使用SQL Server作为数据库提供程序。options.connectionstring是默认的连接字符串(见下一节)正常。但ABP采用iconnectionstringresolver去确定。因此,可以改变这种行为,并可以动态地确定连接字符串。行动通过adddbcontext时调用DbContext的实例将被创建。因此,您还可以有条件地返回不同的连接字符串。

So, where to set default connection string?

In Module PreInitialize

You can do it in PreInitialize of your module as shown below:

public class MyEfCoreAppModule : AbpModule
{
    public override void PreInitialize()
    {
        Configuration.DefaultNameOrConnectionString = GetConnectionString("Default");
        ...
    }
}

So, you can define GetConnectionString method simply returns the connection string from a configuration file (generally from appsettings.json).

所以,你可以定义getconnectionstring方法只返回一个配置文件中的连接字符串(一般从appSettings.JSON)。

Repositories(仓库

Repositories are used to abstract data access from higher layers. See repository documentation for more. 

存储库用于从高层抽象数据访问。更多信息请参见库文档。

Default Repositories(默认的仓库

Abp.EntityFrameworkCore implements default repositories for all entities defined in your DbContext. You don't have to create repository classes to use predefined repository methods.

abp.entityframeworkcore实现在你定义的所有实体的默认库DbContext。您不必创建存储库类以使用预定义的存储库方法。Example:

public class PersonAppService : IPersonAppService
{
    private readonly IRepository<Person> _personRepository;

    public PersonAppService(IRepository<Person> personRepository)
    {
        _personRepository = personRepository;
    }

    public void CreatePerson(CreatePersonInput input)
    {        
        person = new Person { Name = input.Name, EmailAddress = input.EmailAddress };

        _personRepository.Insert(person);
    }
}

PersonAppService contructor-injects IRepository<Person> and uses the Insert method. In this way, you can easily inject IRepository<TEntity> (or IRepository<TEntity, TPrimaryKey>) and use predefined methods.

personappservice构造函数注入IRepository <person>采用插入方法。这样,你可以很容易地注入IRepository < tentity >(或< tentity IRepository,tprimarykey >),使用预定义的方法。

Custom Repositories(自定义的仓库

If standard repository methods are not sufficient, you can create custom repository classes for your entities.

如果标准存储库方法还不够,您可以为实体创建自定义存储库类。

Application Specific Base Repository Class

ASP.NET Boilerplate provides a base class EfCoreRepositoryBase to implement repositories easily. To implement IRepository interface, you can just derive your repository from this class. But it's better to create your own base class that extens EfRepositoryBase. Thus, you can add shared/common methods to your repositories easily. An example base class all for repositories of a SimpleTaskSystem application:

ASP.NET boilerplate 提供了基本的类efcorerepositorybase 库很容易实现。实现irepository接口,你可以获得这个类的任何库。但它是更好的创建你自己的类,扩展efrepositorybase。因此,你可以添加到你的库共享/普通的方法很容易。在所有的库类的例子:一个simpletasksystem应用

//Base class for all repositories in my application
public class SimpleTaskSystemRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<SimpleTaskSystemDbContext, TEntity, TPrimaryKey>
    where TEntity : class, IEntity<TPrimaryKey>
{
    public SimpleTaskSystemRepositoryBase(IDbContextProvider<SimpleTaskSystemDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }

    //add common methods for all repositories
}

//A shortcut for entities those have integer Id
public class SimpleTaskSystemRepositoryBase<TEntity> : SimpleTaskSystemRepositoryBase<TEntity, int>
    where TEntity : class, IEntity<int>
{
    public SimpleTaskSystemRepositoryBase(IDbContextProvider<SimpleTaskSystemDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }

    //do not add any method here, add to the class above (because this class inherits it)
}

Notice that we're inheriting from EfCoreRepositoryBase<SimpleTaskSystemDbContext, TEntity, TPrimaryKey>. This declares to ASP.NET Boilerplate to use SimpleTaskSystemDbContext in our repositories.

注意,我们从efcorerepositorybase < simpletasksystemdbcontext,TEntity,tprimarykey >继承。这表明ASP.NET  Boilerplate 在我们的库中使用simpletasksystemdbcontext。

By default, all repositories for your given DbContext (SimpleTaskSystemDbContext in this example) is implemented using EfCoreRepositoryBase. You can replace it to your own repository base repository class by addingAutoRepositoryTypes attribute to your DbContext as shown below:

默认情况下,你指定的DbContext所有仓库(simpletasksystemdbcontext这个例子)是用efcorerepositorybase。你可以换到你自己的库库的类中添加属性到你autorepositorytypes DbContext如下所示:

[AutoRepositoryTypes(
    typeof(IRepository<>),
    typeof(IRepository<,>),
    typeof(SimpleTaskSystemEfRepositoryBase<>),
    typeof(SimpleTaskSystemEfRepositoryBase<,>)
)]
public class SimpleTaskSystemDbContext : AbpDbContext
{
    ...
}
Custom Repository Example

To implement a custom repository, just derive from your application specific base repository class we created above.

Assume that we have a Task entity that can be assigned to a Person (entity) and a Task has a State (new, assigned, completed... and so on). We may need to write a custom method to get list of Tasks with some conditions and with AssisgnedPerson property pre-fetched (included) in a single database query. See the example code:

要实现自定义存储库,只需从上面创建的应用程序特定的基础存储库类中获得。

假设我们有一个任务实体可以分配给person(实体),任务有一个状态(新的,分配的,完成的)…等等)。我们需要写一个获得有条件的任务列表的自定义方法和属性assisgnedperson预取(包括)在一个单一的数据库查询。请参见示例代码:

public interface ITaskRepository : IRepository<Task, long>
{
    List<Task> GetAllWithPeople(int? assignedPersonId, TaskState? state);
}

public class TaskRepository : SimpleTaskSystemRepositoryBase<Task, long>, ITaskRepository
{
    public TaskRepository(IDbContextProvider<SimpleTaskSystemDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }

    public List<Task> GetAllWithPeople(int? assignedPersonId, TaskState? state)
    {
        var query = GetAll();

        if (assignedPersonId.HasValue)
        {
            query = query.Where(task => task.AssignedPerson.Id == assignedPersonId.Value);
        }

        if (state.HasValue)
        {
            query = query.Where(task => task.State == state);
        }

        return query
            .OrderByDescending(task => task.CreationTime)
            .Include(task => task.AssignedPerson)
            .ToList();
    }
}

We first defined ITaskRepository and then implemented it. GetAll() returns IQueryable<Task>, then we can add some Where filters using given parameters. Finally we can call ToList() to get list of Tasks.

我们首先定义itaskrepository然后实现它。getall()返回IQueryable <task>,然后我们可以添加一些过滤器使用给定的参数。最后,我们可以称tolist()得到任务列表。

You can also use Context object in repository methods to reach to your DbContext and directly use Entity Framework APIs. 

Note: Define the custom repository interface in the domain/core layer, implement it in the EntityFrameworkCore project for layered applications. Thus, you can inject the interface from any project without referencing to EF Core.

你也可以在库的方法使用上下文对象到你的DbContext和直接使用实体框架的API。

注:定义域/核心层的自定义库的接口,实现其在EntityFrameworkCore的项目分层中的应用。因此,您可以在不引用EF内核的情况下从任何项目注入接口。

Replacing Default Repositories

Even you have created a TaskRepository as shown above, any class can still inject IRepository<Task, long> and use it. That's not a problem in most cases. But, what if you overrided a base method in your custom repository? Say that you have overrided Delete method in your custom repository to add a custom behaviour on delete. If a class injects IRepository<Task, long> and use the default repository to Delete a task, your custom behaviour will not work. To overcome this issue, you can replace your custom repository implementation with the default one like shown below:

即使你已经创建了一个taskrepository如上所示,任何类仍然可以注入IRepository <task,long>,并使用它。在大多数情况下这都不是问题。但是,如果你在你的自定义库基础之上的方法吗?说你已经超过了删除自定义库的方法添加一个自定义的行为上删除。如果一个类注入IRepository <task,long>,并使用默认的存储库中删除任务,自定义的行为将不工作。为了克服这个问题,您可以用如下所示的默认选项替换您的自定义存储库实现:

Configuration.ReplaceService<IRepository<Task, Guid>>(() =>
{
    IocManager.IocContainer.Register(
        Component.For<IRepository<Task, Guid>, ITaskRepository, TaskRepository>()
            .ImplementedBy<TaskRepository>()
            .LifestyleTransient()
    );
});

We registered TaskRepository for IRepository<Task, Guid>, ITaskRepository and TaskRepository. So, any one of these can be injected to use the TaskRepository.

Repository Best Practices(仓库的最佳实践)

  • Use default repositories wherever it's possible. You can use default repository even you have a custom repository for an entity (if you will use standard repository methods).
  • Always create repository base class for your application for custom repositories, as defined above.
  • Define interfaces for your custom repositories in domain layer (.Core project in startup template), custom repository classes in .EntityFrameworkCore project if you want to abstract EF Core from your domain/application.
  • 在可能的地方使用默认存储库。您可以使用默认存储库,即使您有实体的自定义存储库(如果您使用标准存储库方法)。
    始终为您的自定义存储库应用程序创建存储库基类,如上所述。
    定义自定义库在领域层的接口(在启动模板核心项目),自定义库类的。如果你想从你的摘要EF核心域/应用项目entityframeworkcore。

posted on 2017-11-24 10:52  斗哥哥  阅读(1852)  评论(0编辑  收藏  举报