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
    public string Name { get; set; }

    public string Surname { get; set; }

    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.


In this document