jeudi 14 novembre 2019

Validations and passing ModelState from Service Layer to Controller WebAPI

I was following the below link to get the validations done at Service Layer https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/models-data/validating-with-a-service-layer-cs

I ended up having cyclic dependency. So remove dependency injection of validation/ModelStateWrapper and used Initialize static way.

Brief Explanation about the Project: I have API, Services Layer, Repository, Database. I am using Automapper to map DTO's to Entities. Controller:

public StudentController(IStudentService studentService, IMapper mapper)
        {
            _studentService = studentService;
            _studentService.Initialize(new ModelStateWrapper(this.ModelState));
            _mapper = mapper;
        } 

    public IActionResult CreateStudent([FromBody] StudentCreationDto student)
            {    
                if (student== null)
                {
                    return BadRequest();
                }                         
                if (!ModelState.IsValid)
                {
                    return new UnprocessableEntityObjectResult(ModelState);
                }
                var studentEnitty = _mapper.Map<Student>(student);
                var createSuccessful = _studentService.CreateStudent(studentEnitty);
                if (!createSuccessful)
                {                
                    return new UnprocessableEntityObjectResult(ModelState);
                }  
            }

Service:

public void Initialize(IValidationDictionary validationDictionary)
                {
                    _validationDictionary = validationDictionary;
                }

public bool CreateStudent(Student student)
            {
                //Check if Student already exists
                if (!ValidateStudentToCreate(student))
                {                
                    return false;
                }

                //Create Portal 
                var createSuccessful = _studentRepository.CreateStudent(student);
                if (!createSuccessful)
                {
                    return false;
                }
                return true;
            }

    private bool ValidateStudentToCreate(Student student)
    {
        //throw new Exception();            
        if (_studentRepository.StudentEmailExists(student.Email))
        {
            _validationDictionary.AddError("Email", "Student already exists");
        }             

        bool isValid = _validationDictionary.IsValid;
        return isValid;
    }

I have IValidation dictionary with AddError and IsValid. ModelStateWrapper Implements IValidationDictionary.

I am trying to get the data model validations at Controller level and Business logic such as Student exists at Service level. I am able to execute the validations however, i am unable to get the model state back to Controller as it says valid. Can you please let me know what I am missing?

Aucun commentaire:

Enregistrer un commentaire