Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Project

Creating a New Person

Next step is to create a modal to add a new item to phone book.

Adding CreatePerson Method to PersonAppService

We first define CreatePerson method in IPersonAppService interface:

Task CreatePerson(CreatePersonInput input);

Then we create CreatePersonInput DTO that defines parameters of the method:

public class CreatePersonInput
{
    [Required]
    [MaxLength(PersonConsts.MaxNameLength)]
    public string Name { get; set; }

    [Required]
    [MaxLength(PersonConsts.MaxSurnameLength)]
    public string Surname { get; set; }

    [EmailAddress]
    [MaxLength(PersonConsts.MaxEmailAddressLength)]
    public string EmailAddress { get; set; }
}

And create mapping in CustomDtoMapper.cs:

configuration.CreateMap<CreatePersonInput, Person>();

All properties are decorated with data annotation attributes to provide automatic validation. Notice that we use same consts defined in Person entity for MaxLength properties.

Here, the implementation of CreatePerson method:

public async Task CreatePerson(CreatePersonInput input)
{
    var person = ObjectMapper.Map<Person>(input);
    await _personRepository.InsertAsync(person);
}

A Person entity is created by mapping given input, then inserted to database. We used async/await pattern here. All methods in ASP.NET Zero startup project is async. It's advised to use async/await wherever possible.

Next

Contributors


Last updated: March 22, 2019 Edit this page on GitHub
In this document