了解APB服务和存储库

时间:2019-05-24 11:14:40

标签: aspnetboilerplate

我正在学习APB,并且正在分析和扩展引言第2部分教程(ASP.NET Core,EF)中的代码。 https://aspnetboilerplate.com/Pages/Documents/Articles/Introduction-With-AspNet-Core-And-Entity-Framework-Core-Part-2/index.html

首先,我介绍了一个新实体-团队。团队是一群人。我将外键添加到了Person实体。

[Table("AppPersons")]
public class Person : AuditedEntity<Guid>
{
    public const int MaxNameLength = 32;

    [Required]
    [StringLength(MaxNameLength)]
    public string Name { get; set; }

    public Guid TeamId { get; set; }

    [ForeignKey(nameof(TeamId))]
    public Team Team { get; set; }
}

我想创建一个ApplicationService,它将返回具有指定任务数量的特定团队的人员列表。我不知道应该如何结合存储库和映射基础结构来实现这一目标。

public class FooAppService : ApplicationService, IFooAppService
{
    private readonly IRepository<Task, Guid> _taskRepository;

    public FooAppService(IRepository<Task, Guid> taskRepository)
    {
        _taskRepository = taskRepository;
    }

    public ListResultDto<PersonWithNumberOfTasksAssignedDto> FooMethod(Guid teamId)
    {
        ...
    }

}

我也不知道设计DTO对象的最佳方法是什么。此服务方法是否需要新的DTO对象,还是应该使用元组?我确实有一个PersonDto对象,用于添加/编辑Person。我应该用count属性包装它吗?

public class PersonWithNumberOfTasksAssignedDto : EntityDto<Guid>
{
    public PersonDto Person { get; set; }
    public int NumberOfAssignedTasks { get; set; }
}

1 个答案:

答案 0 :(得分:-1)

您可以从Abp免费启动模板中获取参考。

public async Task<PersonDto> Create(PersonCreateInput input)
{
    var person = ObjectMapper.Map<Person>(input);

    await CurrentUnitOfWork.SaveChangesAsync();

    return MapToEntityDto(person);
}

例如,UserAppService.Create() https://github.com/aspnetboilerplate/module-zero-core-template/blob/164a5c9e28cb29383551d0f3310986ab43d0ceed/aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Users/UserAppService.cs#L55-L76

要检索项目列表,您可以利用AsyncCrudAppService基类,该基类提供开箱即用的排序和分页功能(通过AsyncCrudAppService.GetAll()。 参见https://github.com/aspnetboilerplate/aspnetboilerplate/blob/14c4fe5a5408a66e913a434688b951815247827d/src/Abp/Application/Services/AsyncCrudAppService.cs#L112-L129

相关问题